JPA eagerly downloads FetchType.LAZY child collections

I saw several similar questions with 0 good answers.

It should be very simple. I use Java JPA and I sometimes want to load a list of child objects, but not all the time. Unfortunately, the JPA doesn't seem to be listening to me when I say to get it lazily. I made 100% certain that the childEntities attribute is missing in my code. However, all child objects are still loaded immediately after calling JPA.em (). Find (..). This is how I declare a link to annotations.

@Entity @Table(name = "parentEntities") public class ParentEntity implements Serializable { .... @OneToMany(mappedBy = "entityPropertyName", fetch = FetchType.LAZY) public List<ChildEntity> childEntities; ... } 

And this is how I load the parent:

 ParentEntity parentEntity = JPA.em().find(ParentEntity.class, id); 

In addition, I hoped that sometimes she would want to get this collection and be able to dynamically tell JPA when to do it. However, this is step 2. Step 1 is only for this to work correctly.

+6
source share
2 answers

I have done so. Please refer to this, it will work just as well:

 @Entity @Table(name = "member_information") public class MemberInformation implements Serilizable{ @OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE},optional = false) private MemberInformationDetails memberInformationDetailsId; @LazyCollection(LazyCollectionOption.TRUE) @OneToMany(mappedBy = "memberInformationId", cascade = CascadeType.ALL) private Collection<AccountOpening> accountOpeningCollection; } 

Use @OneToOne(fetch = FetchType.LAZY....) to match one to one and to use the @LazyCollection(LazyCollectionOption.TRUE) collection.

 @Entity @Table(name = "member_info_details") public class MemberInformationDetails implements Serializable{ @OneToOne(mappedBy = "memberInformationDetailsId") private MemberInformation memberInformationId; .......//getter and setters } @Entity @Table(name = "account_opening") public class AccountOpening implements Serializable { @JoinColumn(name = "member_information_id", referencedColumnName = "id", nullable = false) @ManyToOne(optional = false) private MemberInformation memberInformationId; ..........//getters and setters } 

If you want to access the collection, merge it to and get an object:

 @Stateless public class MemberInformationFacade{ .............. public MemberInformation getMemberWithMemberDetails(MemberInformation m) { m = getEntityManager().merge(m); try { m.getMemberInformationDetailsId().getId(); m.getMemberInformationDetailsId().accountOpeningCollection().size(); } catch (Exception e) { e.printStackTrace(); } return m; } } 
+4
source

I am actively looking for an answer to your # 2: getting JPA to lazy loads sometimes and with impatience of other times.

As for your question # 1, it seems to me that you want to use getReference () instead of find (). If you use Hibernate, then it will create a link to the object, and then only get it when it is needed.

I am sure this will help: When to use EntityManager.find () vs EntityManager.getReference ()

+2
source

Source: https://habr.com/ru/post/950436/


All Articles