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
|