robert trudel
robert trudel

Reputation: 5749

Property of @IdClass not found in entity

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

Upvotes: 7

Views: 14338

Answers (2)

jhenrique
jhenrique

Reputation: 868

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

Upvotes: 11

Emil Hotkowski
Emil Hotkowski

Reputation: 2343

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;
}   

Upvotes: 1

Related Questions