-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate ignores javax.persistence.fetchgraph
PostPosted: Fri May 06, 2016 1:14 am 
Newbie

Joined: Tue May 03, 2016 11:02 am
Posts: 2
This is my entity:

Code:
public class PersonItem implements Serializable{
    @Id
    @Column(name="col1")
    private String guid;

    @Column(name="col2")
    private String name;

    @Column(name="col3")
    private String surname;

    @Column(name="col4")
    private Date birthDate;
//+getters and setters
}


This is how I get the list of persons:

Code:
Query query = em.createQuery("Select p from PersonItem p WHERE p.guid IN (:guids)");
EntityGraph<PersonItem> eg = em.createEntityGraph(PersonItem.class);
eg.addAttributeNodes("guid");
eg.addAttributeNodes("name");
eg.addAttributeNodes("surname");
query.setHint("javax.persistence.fetchgraph", eg);
query.setParameter("guids", guids);
List<PersonItem> list=query.getResultList();
em.close();


// And now I iterate result AFTER EM CLOSE
....iterate

If I understand fetch graph correcly it must load only those fields, which I specified. However, the field "birthDate" is also loaded. Besides I see that in hibernate sql query 4 columns are selected.

How to fix it? I use hibernate 5.1.0 as JPA provider. People say that according to JPA this must work http://stackoverflow.com/a/37053402/5057736


Top
 Profile  
 
 Post subject: Re: Hibernate ignores fetchgraph
PostPosted: Mon May 16, 2016 10:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
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.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.