-->
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: many-to-one losing id of objects 'one' side (bug ?)
PostPosted: Mon Mar 31, 2008 6:34 am 
Newbie

Joined: Mon Mar 31, 2008 6:14 am
Posts: 2
Hibernate version: 3.2.6

Hi All,

I've experienced some behavior that is looking more and more like a bug but seems too common a use case for it not to be picked up by others.

I have a many to one mapping between a user and organisation - very standard stuff. On the actual many-to-one mapping I have fetch="join" set so that when a user is loaded by it's id, organisation is loaded immediately without the use of a proxy and a second query. This works as expected.

I also have a query that looks up a user by username. This query works as documented and doesn't apply the fetch="join" that is mapped for the organisation mapping against user so a proxy is created for the organisation. As I understand, mapped attributes such as fetch="join" are only applied when an object is loaded by id.

When I call user.getOrganisation(), the proxy hits the db and populates all values in the proxy's target organisation object. I can see all the fields set in the proxy target as expected. What i'm finding though is that when I call the getters for the fields, they all return a correct values except the id field which returns null. I can see this behavior clearly in the debugger, the proxy calls through to the target organisation for every getter except for the id in which case the proxy simply returns without hitting the underlying target. The only thing I can imagine is that because the id is already known at this point because it was used to fetch the organisation, it has a different behavior being applied at the proxy level.

I can get around this problem by structuring my findUserByUsername query to apply a "left join fetch user.organisation" which is actually what I want anyway so all is good. It does leave me wondering however if this is a bug in hibernate.

Any thoughts ?

cheers,
rob


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 12:28 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
the fetch settings also work with criteria API queries, but not with HQL or SQL queries (I think).

As for the behavior that you describe: Just as you said: It is such a commen scenario I'd guess it is not a bug in hibernate, but in your code/mapping.

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


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.