JPA - forced lazy loading only for the given request

How do I apply a lazy loading strategy only for a given NamedQuery.

eg. Consider the pseudo code below (just to explain the case) I have an entity

@Entity class Xyz { int a; int b; @Fetch = EAGER Set<ABC> listOfItems; } 

In this case, we declared listOfItems EAGERLY.

Now suppose I have NamedQuery (query="getXyz" , name="select x from Xyz x where a=?") For this query, I just need the result to be lazy. If I do not want listOfItems to be restored.

How can I cover them? ps: 1. I do not want listOfItems to be Lazy in the Entity 2 class. I do not want to select specific fields in a query like name="select a,b from Xyz z where a = ? "

Thanks in advance for the suggestions.

+6
source share
2 answers

If you do not want to receive Set , you must define it as lazy. However, keep in mind that execution is allowed to look impatient when you indicate lazy.

Specification Specification:

public enum FetchType extends java.lang.Enum

Defines strategies for retrieving data from a database. The EAGER strategy is a prerequisite for the continuity provider runtime environment that data should look forward to. The LAZY strategy is a hint at the runtime of the persistence provider, in which data should be retrieved lazily at first access. Implementations are allowed to readily retrieve data for which the LAZY strategy is specified.

If you do not want to receive such a Set , I would alternatively create a small class that suits your needs:

 @Entity @Table(name = "XYZ") public class XyzStub { int a; int b; } 

You can query this with TypedQuery:

 EntityManager em; //.... TypedQuery<XyzStub> q = em.createNamedQuery("select x from XyzStub x where xa = :a", XyzStub.class) q.setParameter("a", a); 
+1
source

If you use EclipseLink, you can use select groups,

http://wiki.eclipse.org/EclipseLink/Examples/JPA/AttributeGroup

0
source

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


All Articles