Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property of @IdClass not found in entity

Tags:

hibernate

jpa

I use spring boot 2, jpa and hibernate

with this code

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Samplings {

    @Id
    @GenericGenerator(name = "samplings_id_seq", strategy="com.lcm.model.SamplingSequenceGenerator")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "samplings_id_seq")
    private Integer id;

    @OneToMany(mappedBy = "sampling")
    private List<Samples> samples = new ArrayList<>();

}   

@Entity
@IdClass(SamplesPK.class)
public class Samples  {

    @Id
    private String sampleLetter;

    @Id
    @ManyToOne(optional = false)
    @JoinColumns({
        @JoinColumn(name = "id", referencedColumnName = "id")})
    private Samplings sampling;
}   

public class SamplesPK implements Serializable {

    private Integer id;

    private String sampleLetter;

    public SamplesPK(Integer id, String sampleLetter) {
        this.id = id;
        this.sampleLetter = sampleLetter;
    }

    .... //get / set
}

I get this error:

org.hibernate.AnnotationException: Property of @IdClass not found in entity com.lcm.model.Samples:: id

like image 768
robert trudel Avatar asked Oct 17 '18 18:10

robert trudel


2 Answers

The names of the fields or properties in the primary key class and the primary key fields or properties of the entity must correspond and their types must be the same. From docs here.

Your Samples class should have its id as the same type of your SamplesPK.

You should have a @Id private Integer id in your Samples class

like image 153
jhenrique Avatar answered Nov 16 '22 06:11

jhenrique


It looks like you miss some components of your SamplesPk.class in your Samples entity.

It is called Samples:: samplingId

Here you have an example LINK

EDIT:

So your entity should look like this :

@Entity
@IdClass(SamplesPK.class)
public class Samples  {

    @Id
    private String sampleLetter;

    @Id
    private Integer id;

    @Id
    @ManyToOne(optional = false)
    @JoinColumns({
        @JoinColumn(name = "sampling_id", referencedColumnName = "id")})
    private Samplings sampling;
}   
like image 36
Emil Hotkowski Avatar answered Nov 16 '22 06:11

Emil Hotkowski