0

I am new to Spring boot. I am trying to create the below service. Parent class is Artists. Child is Album. I am trying to fetch all the Albums corresponding to particular Artists. While creating custom method in crudRepository I am getting error. Can't able to identify the exact issue, help for the error will be greatly appreciated.

Artists.java (Bean class of Parent)

package com.org.Music_App.Artists;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.org.Music_App.Albums.Album;

@Entity
public class Artists {

    @Id
    private int artists_Id;
    private String artists_Name;
    private int no_of_Albums;
    private String debut_Album;
    @OneToMany
    @JoinColumn(name = "artists_id")
    @Transient
    private List<Album> album;

    public Artists() {

    }

    public Artists(int artists_Id, String artists_Name, int no_of_Albums, String debut_Album) {
        this.artists_Id = artists_Id;
        this.artists_Name = artists_Name;
        this.no_of_Albums = no_of_Albums;
        this.debut_Album = debut_Album;
    }

    public int getArtists_Id() {
        return artists_Id;
    }

    public void setArtists_Id(int artists_Id) {
        this.artists_Id = artists_Id;
    }

    public String getArtists_Name() {
        return artists_Name;
    }

    public void setArtists_Name(String artists_Name) {
        this.artists_Name = artists_Name;
    }

    public int getNo_of_Albums() {
        return no_of_Albums;
    }

    public void setNo_of_Albums(int no_of_Albums) {
        this.no_of_Albums = no_of_Albums;
    }

    public String getDebut_Album() {
        return debut_Album;
    }

    public void setDebut_Album(String debut_Album) {
        this.debut_Album = debut_Album;
    }

    public List<Album> getAlbum() {
        return album;
    }

    public void setAlbum(List<Album> album) {
        this.album = album;
    }

}

Album.java (Bean class of Child)

package com.org.Music_App.Albums;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import com.org.Music_App.Artists.Artists;

@Entity
public class Album {
    @Id
    private int album_Id;
    private int artists_Id;
    private String album_Name;
    private int no_of_Songs;
    private String artists_Name;


    public Album()
    {

    }

    public Album(int album_Id, int artists_Id, String album_Name, int no_of_Songs, String artists_Name) {
        super();
        this.album_Id = album_Id;
        this.artists_Id = artists_Id;
        this.album_Name = album_Name;
        this.no_of_Songs = no_of_Songs;
        this.artists_Name = artists_Name;
    }

    public int getAlbum_Id() {
        return album_Id;
    }

    public void setAlbum_Id(int album_Id) {
        this.album_Id = album_Id;
    }

    public int getArtists_Id() {
        return artists_Id;
    }

    public void setArtists_Id(int artists_Id) {
        this.artists_Id = artists_Id;
    }

    public String getAlbum_Name() {
        return album_Name;
    }

    public void setAlbum_Name(String album_Name) {
        this.album_Name = album_Name;
    }

    public int getNo_of_Songs() {
        return no_of_Songs;
    }

    public void setNo_of_Songs(int no_of_Songs) {
        this.no_of_Songs = no_of_Songs;
    }

    public String getArtists_Name() {
        return artists_Name;
    }

    public void setArtists_Name(String artists_Name) {
        this.artists_Name = artists_Name;
    }


}

Custom method:

package com.org.Music_App.Repository;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import com.org.Music_App.Albums.Album;
import com.org.Music_App.Artists.Artists;


public interface AlbumRepository extends CrudRepository<Album, Integer> {

    public List<Album> findByArtists_Id(Integer artists_id) ;

}

Error:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property artists found for type Album!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.13.6.RELEASE.jar:na]

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'albumRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property artists found for type Album!
user3212324
  • 163
  • 1
  • 6
  • 23
  • 2
    Possible duplicate of [Spring-Data-Jpa Repository - Underscore on Entity Column Name](https://stackoverflow.com/questions/23456197/spring-data-jpa-repository-underscore-on-entity-column-name) – OutOfMind Aug 11 '17 at 12:35
  • Why do you use `super()` in the `Album` class ? Also, because of `@JoinColumn`, the engine tries to figure out something. Maybe there is no artists in you DB with that id (ensur COMMIT was performed) ; maybe you are trying to add an Artist without specifying all his albums i.e. `@OneToMany` enforces the existance of at least one record. I'm not sure `@ZeroToMany` exists so you may have to rethink your approach if that's the issue – Jason Krs Aug 11 '17 at 12:41

3 Answers3

1

Can you retry the same code removing all underscores?

Java naming convention use camelcase and Spring assumes conventions in order to wire things properly.

if you have

@Id
private int albumId;

you have:

public int getAlbumId;
public void setAlbumId(int albumId);

etc.

PS: you don't need to define the artistsId property in the Album entity only because there will be an "artistis_id" column in the "album" table.

Ricardo Piccoli
  • 481
  • 4
  • 14
0

The AlbumRepository's findByArtists_Id method thinks that it needs to look up data based on artists instead of artist_Id, because it seems to be considering the String after "By" upto the underscore.

Try removing underscore and it may solve your issue.

It seems underscores doesn't work with the entity field names. Here is a similar question, where you can find a detailed answer: Spring-Data-Jpa Repository - Underscore on Entity Column Name

Hope that helps!

OutOfMind
  • 874
  • 16
  • 32
0

You have to define your repository here propertly, add @Repository here

  @Repository
    public interface AlbumRepository extends CrudRepository<Album, Integer> {

    public List<Album> findByArtists_Id(Integer artists_id) ;

}

then it will start working

Vipul Jain
  • 1,395
  • 1
  • 14
  • 28