简体   繁体   中英

Grails Hibernate Criteria: Get the originial table entry

Solved: Changing the join-type to INNER_JOIN

I'm trying to get the original table-entry of a domain class after executing a hibernate criteria.

For example:
The domain class A got a hasMany association to the domain class B.
The entity of A with the id 1, got two entities of B with the ids 11 and 12.

I'm executing the following criteria:

Criteria criteria = session.createCriteria(A.class)
criteria.createAlias("Bs","B",CriteriaSpecification.LEFT_JOIN)
criteria.add(Restrictions.like("B.property", "%"+something+"%")

def list=criteria.list().unique()

Now I have a result set with all entities of A, which got an entity of B, which fullfilles the criteria.

But the results of A are different to the original entities of A, with regard to the hasMany relation to B. The entities of B, which not fullfilles the criteria are missing.

For example: The entity A with id 1 only got the entitie B with id 11.

Even if I execute a

def newA=A.findById(list[0])

the newA only got the B entity with the id 11. And the 12 is missing.

Any Ideas?

See: http://adhockery.blogspot.com/2009/06/querying-by-association-redux.html and http://jira.grails.org/browse/GRAILS-7087

Try this:

def list = A.withCriteria {
    createAlias("bs", "aliasedBs")
    like("aliasedBs.property", "%something")
}
list.bs // will contain ALL items of the association, independent of the query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM