Lets suppose we have some entity, which has 10 fields. And let's suppose that almost all these fields have very large data. And we want to load entity (not the set of fields!) and at runtime define which fields to load. The solution I found https://stackoverflow.com/a/24710759/5057736 suggests using constructor. But in case of 10 fields and that it is necessary to define fields at runtime is not possible solution. Is there a way how to solve this problem using jpa 2.1?
-
Let's suppose you use JPA EntityGraphs?– Neil StocktonMay 5, 2016 at 11:28
-
@Neil Stockton I thought that EntityGraphs is used only for showing what Entities to load, but not what their fields.– Pavel_KMay 5, 2016 at 11:46
-
Nope. Defines what fields to load. datanucleus.org/products/accessplatform_5_0/jpa/… It has weaknesses however, in that you can't explicitly say don't load field X– Neil StocktonMay 5, 2016 at 12:29
-
@Neil Stockton Thanks. Could you show an example how to use unnamed entity graph with JPQL query? I would accept your answer.– Pavel_KMay 5, 2016 at 14:28
3 Answers
Use a JPA 2.1 EntityGraph to define the fields to be retrieved by the query. So if you have a class MyClass
, and want to retrieve particular fields dynamically, something like this could suffice
EntityGraph<MyClass> eg = em.createEntityGraph(MyClass.class);
eg.addAttributeNodes("id");
eg.addAttributeNodes("name");
eg.addAttributeNodes("relation");
Query q = em.createQuery("SELECT b FROM MyClass b");
q.setHint("javax.persistence.fetchgraph", eg);
List<MyClass> results = q.getResultList();
-
However, all fields are loaded by default. So, how can I disable loading by default all the fields?– Pavel_KMay 5, 2016 at 14:42
-
That is what "fetchgraph" does different to "loadgraph", see that link in the comments ... and see JPA spec section 3.7.4 May 5, 2016 at 14:45
-
If that is the case then your JPA provider is unilaterally deciding to load that field against your wishes (i.e just taking the fetchgraph as a recommendation). The JPA provider I use (DataNucleus) respects my wishes, which is really what an EntityGraph should do IMHO. May 5, 2016 at 15:00
-
-
Upvoted it, not that its particularly interestng to me ;-) I came across way too many places where Hibernate does its own thing rather than follow JPA's intent, and have no plan of going back to it ever. Good luck ! May 5, 2016 at 15:13
The Fetch graphs are mostly targeted to fetching associations, not for individual fields.
Even if the JPA spec says that fields should be lazy by default, LAZY is just a hint for the JPA providers, which might choose to ignore it. Hibernate does not use lazy loading for fields by default. Only one-to-many and many-to-many associations are LAZY by default.
To have lazy fields, you need to enable bytecode enhancement and maybe use @LazyGroup as well.
Anyway, maybe a DTO projection query is what you needed in the first place.
With a Hibernate session this can be gained through using a result transformer. Hibernate doesn't support the result transformers for JPA.
HHH-8196 Custom ResultTransformer for JPA criteria queries
You can use unwrap(Session.class)
to apply a result transformer to the session.
List<Person> persons = entityManager.unwrap(Session.class).
createQuery("select name as name from Person").
setResultTransformer(
Transformers.aliasToBean(Person.class)
).list();