'Querying an embedded attribute in Hibernate
I'm trying to query using Hibernate and running into some issues.
Let's say I'm trying to query the class StoreEntity, which has an embedded attribute StoreInfo. StoreInfo is comprised of a Long StoreNumber and a Long ManagerId.
I have a List<StoreInfo> and I would like to return all of the associated StoreEntity. I am trying to query like this:
Criteria criteria = this.session()
.createCriteria(StoreEntity.class)
.add(Restrictions.in("storeInfo", storeInfoList));
This only returns one result, which will be the first item in the StoreInfoList.
That said, if I break the StoreNumber and ManagerId fields into separate lists and search them with a conjunction, this works correctly.
final Conjunction criterion = Restrictions.conjunction();
criterion.add(Restrictions.in("storeInfo.StoreNumber", storeNumbers));
criterion.add(Restrictions.in("storeInfo.ManagerId", managerIds));
Criteria criteria = this.session()
.createCriteria(StoreEntity.class)
.add(criterion);
Why is this? I would understand if querying an embedded attribute with an in clause was just outright illegal, but why does it return one item? Should I always be using a conjunction when querying an embedded attribute?
I tried logging the actual queries to the console and got the following:
Working:
[junit] from
[junit] store_entity this_
[junit] where
[junit] (
[junit] this_.store_number in (
[junit] ?, ?
[junit] )
[junit] and this_.manager_id in (
[junit] ?, ?
[junit] )
[junit] )
Non-working:
[junit] from
[junit] store_entity this_
[junit] where
[junit] (
[junit] this_.store_number, this_.manager_id
[junit] ) in (
[junit] (
[junit] ?, ?
[junit] ), (
[junit] ?, ?
[junit] )
[junit] )
Seems like those should be the same?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
