-->
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.  [ 5 posts ] 
Author Message
 Post subject: Query result semantics
PostPosted: Wed Aug 17, 2005 2:53 am 
Newbie

Joined: Tue Jan 27, 2004 11:02 am
Posts: 4
Hibernate version:3.0.5

Hi,

I have a basic question about some query semantics of Hibernate.
I have two classes, A and B, with a bidirectional one-to-many association between A and B. In the database, instance a1 is related to instances b1, b2, b3, b4.

First, I execute the following query:
from A as a
left join fetch a.b as b
where a.ID = 1 and b.id = 1

This returns object a1, with an initialized collection of Bs, filled with instance b1.

After this, I'm executing the following query, in the same transaction and using the same session:
from A as a
left join fetch a.b as b
where a.ID = 1 and b.id = 2

This query is executed against the database, and since a1 and b1 in the satisfy the query condition in the where clause, object a1 is returned. However, since a1 is in the Session cache with an already initialized collection for the B instances, the collection is ignored (this is shown in the debug statements), and instance b2 is not added to it. Therefore, the result of the second query is an object a1, with a collection of B instances filled with object b1.

Executing the second query like this gives a result that i would not expect normally. I can imagine that the query wouldn't produce a result (i.e. return a resultset of null) because the object contained in the cache doesn't satisfy the query condition, but this is not the case.
I can imagine that it's not advisable to run a query twice like this in the same session, but currently we're working with a functional design model where a similar situation like the above might occur. In this case, we might have to take some actions to get correct query results.

Basic question that i have is, whether the behaviour outlined above is intended behaviour of Hibernate or can it be considered as a side-effect of collection initialization?

Thx and regards,

Arjan Blokzijl


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 12:23 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
What's odd to me is that even calling Session.evict(a) in between queries doesn't help. I'm as curious as you to know why this happens.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 1:23 pm 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
try Session.flush() between the queries.

_________________
Jason Li
Don't forget to rate:)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 1:17 am 
Newbie

Joined: Tue Jan 27, 2004 11:02 am
Posts: 4
Jason,

Flushing the session doesn't really matter here because there's nothing to flush (no pending updates). The a1 instance remains in cache, with the initialized collection with the b1 instance. Only session.evict(a1) works here (at least for me, I don't know why it doesn't work for JDL) to get a correct result for the second query.

Regards,

Arjan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 1:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You should never, ever, ever write queries like this!

Do not ever use a fetched association in the where clause.

If you want to do ad hoc joins like this, use some other kind of join, do not use a fetch join.


Please read HiA if you have any doubts.


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