I have a class samplings
{code:java} @Entity @IdClass(SamplingsPK.class) @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Samplings {
@Id private int year;
@Id @GeneratedValue private Integer sequenceId;
@OneToMany(mappedBy = "sampling", cascade = CascadeType.ALL, orphanRemoval = true) private List<Samples> samples = new ArrayList<>();
} {code}
A class samples
{code:java} @Entity @IdClass(SamplesPK.class) public class Samples extends BaseEntity {
@Id private String sampleLetter;
@Id @ManyToOne(optional = false) @JoinColumns({ @JoinColumn(name = "sampling_id", referencedColumnName = "sequenceId"), @JoinColumn(name = "sampling_year", referencedColumnName = "year")}) private Samplings sampling;
@OneToOne(mappedBy = "sample", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true) private TestSamples testSamples; } {code}
Primary key of samplings is used in samples plus another value.
After saving samplings, If I run this query
{code:java} select s from Samples s Join Fetch s.testSamples Join fetch s.sampling sp Left Join fetch sp.machine m Join fetch sp.product p Join fetch p.productType pt {code}
TestSamples is not loaded
Vlad wrote
@I noticed that the problem is caused because the SamplesPK was not properly initialized:
0 = {NestedCompositeKeyTest$SamplesPK@4647} sampleLetter = "A" sampling = {NestedCompositeKeyTest$SamplingsPK@4649} year = 1 sequenceId = {Integer@4650} "1" value = 1
However, I’m not sure if JPA demands supporting nested IdClasses, so it might not be a bug if it’s not specified to work.
This website said nested is supported since JPA 2.0 http://www.kawoolutions.com/Technology/JPA,_Hibernate,_and_Co./JPA_Composite_Key_Variants
Info thread about this issue https://discourse.hibernate.org/t/object-fetch-in-the-query-but-not-in-the/1212/7
A test has been created and attached to this issue. |
|