I use spring data jpa, hibernate I have a complex structure. I search to get samples with some object fetched
@Entity
@IdClass(SamplingsPK.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Samplings {
@Id
private int year;
@Id
@GeneratedValue
private Integer sequenceId;
@OneToOne
private Products product;
@OneToOne
private Machines machine;
@OneToOne
private Dimensions dimension;
@OneToOne
private Colors color;
@OneToMany(mappedBy = "sampling", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Samples> samples = new ArrayList<>();
...
}
@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;
...
}
@Entity
public class TestSamples {
@Id
@SequenceGenerator(name = "test_samples_id_seq", sequenceName = "test_samples_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "test_samples_id_seq")
private Integer id;
@OneToOne(fetch = FetchType.LAZY)
private Samples sample;
@OneToOne(mappedBy = "testSample", fetch = FetchType.LAZY)
private Compressions compressionTest;
@OneToOne(mappedBy = "testSample", fetch = FetchType.LAZY)
private Durabilities durabilityTest;
@OneToOne(mappedBy = "testSample", fetch = FetchType.LAZY)
private Scalings scalingTest;
@OneToOne(mappedBy = "testSample", fetch = FetchType.LAZY)
private Granulometries granulometryTest;
@OneToOne(fetch = FetchType.LAZY)
private Absorptions absorptionTest;
...
}
When I save a new samplings, i do
Samplings sampling = new Samplings();
..
Samples sample = new Samples();
...
TestSamples testSamples = new TestSamples();
testSamples.setSample(sample);
..
sample.setTestSamples(testSamples);
samplings.addSample(sample);
samplings = samplingsRepository.save(sampling);
After saving, If i try to get a samples with testSample, I run this query
@Query(
value = "select s from Samples s Join fetch s.sampling sp Left Join fetch sp.machine m Join fetch sp.product p Join fetch p.productType pt Join Fetch s.testSamples",
countQuery = "select count(s) from Samples s Join s.sampling sp Left Join sp.machine m Join sp.product p Join p.productType Join s.testSamples")
public Page<Samples> findAllFullSample(Pageable pageable);
When I check sample, TestSamples is always null
I found a very bad workaround…
Page<Samples> pageSamples = samplesRepository.findAllFullSample(pageable);
List<Samples> samples = pageSamples.getContent();
for (Samples sample : samples) {
TestSamples testSample= testSamplesRepository.findSamplesWithFullProductAndCompressionTest(sample.getSampling().getSequenceId(),sample.getSampling().getYear(), sample.getSampleLetter());
sample.setTestSamples(testSample);
}
return pageSamples;
Query
@Query(value = "select ts from TestSamples ts Join ts.sample s left Join Fetch ts.compressionTest where s.sequenceId=:id and s.year=:year and s.sampleLetter=:sampleLetter")
public TestSamples findSamplesWithFullProductAndCompressionTest(@Param("id") Integer id, @Param("year") int year, @Param("sampleLetter") String sampleLetter);
So why TestSample is null when I get it from Sample