ManyToOne reference loading with JPA/Hibernate
In this example an InvestigationType has many SampleType’s. If I load an InvestigationType via em (entity manager) find it also loads the samples. InvestigationType inv2 = (InvestigationType) em.find( InvestigationType.class, inv.getId()); System.out.println(“Inv2 ID="+inv2.getId()); System.out.println(“Inv2 Title="+inv2.getTitle()); assertTrue(inv2.getSample().size() == 2); However, if I load a SampleType the investigation field is null, unless I refresh the object. // Get a sample SampleType s3 = (SampleType) em.find( SampleType.class, inv2.getSample().get(0).getId()); em.refresh(s3); System.out.println(convertDataToXmlString(s3)); InvestigationType inv3 = s3.getInvestigation(); assertNotNull(inv3); From my understanding I should not have to do this as the fetching should be the same in both instances, ie resolving the parent by default. Similarly, if I query for both objects the references are correctly loaded. ...