-->
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.  [ 7 posts ] 
Author Message
 Post subject: Setting the fetch mode on a projection
PostPosted: Thu Jul 20, 2017 12:00 pm 
Newbie

Joined: Thu Jul 20, 2017 11:56 am
Posts: 4
I have a class that I am querying where I have a single projection that is an association. I am attempting to also eagerly fetch that association and its related properties, however Hibernate always returns the projected properties as lazy. Anyone have any idea how to do this?

I found this thread that looks to be the same issue: viewtopic.php?f=1&t=1003446&start=0

Thanks!


Top
 Profile  
 
 Post subject: Re: Setting the fetch mode on a projection
PostPosted: Thu Jul 20, 2017 12:05 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Try to elaborate it with some code examples because it's not clear what you are asking.


Top
 Profile  
 
 Post subject: Re: Setting the fetch mode on a projection
PostPosted: Thu Jul 20, 2017 12:12 pm 
Newbie

Joined: Thu Jul 20, 2017 11:56 am
Posts: 4
That will be slightly difficult since I am using Hibernate through another abstraction layer, however

UserRole is a class with a composite key of types User and Role
User has a ToOne to itself

Code:
UserRole {
    User user
    Role role
}

User {
    User manager
}

session.createCriteria(UserRole)
criteria.add(Restrictions.eq('role', role))
criteria.setProjection(Projections.property('user'))
criteria.setFetchMode('user', FetchMode.JOIN)
criteria.setFetchMode('user.manager', FetchMode.JOIN)
criteria.list()


Top
 Profile  
 
 Post subject: Re: Setting the fetch mode on a projection
PostPosted: Thu Jul 20, 2017 12:18 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Hibernate Criteria is deprecated, so it's no longer supported.

Try with an HQL or JPA Criteria example instead.


Top
 Profile  
 
 Post subject: Re: Setting the fetch mode on a projection
PostPosted: Thu Jul 20, 2017 12:40 pm 
Newbie

Joined: Thu Jul 20, 2017 11:56 am
Posts: 4
Vlad,

The example provided there isn't even my actual code. The idea was just to give you an example of what I'm trying to do. HQL is not an option for me since some of the code to create the query is dynamic. I'm not familiar with the JPA criteria API, so unfortunately at this time I can't provide you can example with that either.

Thanks for your time


Top
 Profile  
 
 Post subject: Re: Setting the fetch mode on a projection
PostPosted: Thu Jul 20, 2017 3:01 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The reason why I mentioned it is because, even if there's a bug, it will not be fixed anyway.

More, this is not strictly a projection, it's just fetching of an association which you can do with JPQL as follows:

Code:
select ur
from UserRole ur
join fetch ur.user u
join fetch u.manager
where ur.role = :role


That's it.


Top
 Profile  
 
 Post subject: Re: Setting the fetch mode on a projection
PostPosted: Thu Jul 20, 2017 3:05 pm 
Newbie

Joined: Thu Jul 20, 2017 11:56 am
Posts: 4
Vlad,

Thanks for the response and I understand what you mean.

In this case I want User objects to be returned, not UserRole objects.

I was able to work around the issue by query directly to User and using a sub select to ensure the user is in the list

Code:
select *
from User u
join fetch u.manager
where u in (select ur.user from UserRole ur where ur.role = :role)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.