Hibernate Criteria many to many same object
I have a User entity that is linked to itself with many to many relation. Basically every User can be a parent or a child and I would like to create a criteria object in order to allow pagination and other filtering. In the user I keep reference for both a list of its parents and a list of its children, so the middle table created will have two entries for every user couple. Basically I do that for being able to have bidirectional relation.
My current Criteria object is:
Criteria c = getSession().createCriteria(User.class)
.createAlias("parents", "p")
.add(Restrictions.eq("p.id", this.getId()))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
and then I apply the filtering on that criteria.
But this query will only return a list of all the children that has this user as a parent. I would like to get both the parent and the children. for example the desired response would be something like this:
{
next:XXX,
previous{xxx},
data[
{parentuser},
{child1},
{child2}
....
]
}
is that possible with criteria?
Ok so the simplest solution was to just add the parent as a child to himself since this is a many to many relation this worked fine. Not a clean solution but it works as expected.
链接地址: http://www.djcxy.com/p/37080.html