iteye_14864
2009-08-14 10:00
浏览 463
已采纳

Hibernate多对多集合对象关联查询的问题

同志们,问一个问题,在hibernate里面.我有一个group类表示群组,person类表示联系人,它们是双向多对多关系 ,如果我想查询联系人姓名包括‘tom’的群组列表,那么hql怎么写?


public class Person implements Comparable{
private int id;
private String name;
private String phone;
private String address;
private Set groups;
}

public class Group implements java.lang.Comparable{
private int id;
private String name;
private String description;
private Set persons;
}

groups=session.createQuery("from Group as g inner join g.persons as p where p.name=?").setString(0, name).list();


这种方法返回的将是一个 Object列表..
问题补充
Blithe 写道
groups.get(0)

首先谢谢,经过调试发现,
List result=session.createQuery("from Group as g  inner join g.persons as p  where p.name=?").setString(0, name).list();

result中的每一个条目是一个Object数组,此Object数组有两个元素,Group和Person,所以解决的办法是

List result=session.createQuery("from Group as g inner join g.persons as p where p.name=?").setString(0, name).list();

groups=new ArrayList();
int size=result.size();
for(int i=0;i<size;i++){
Object[] item=(Object[])result.get(i);
groups.add(item[0]);
}

不过显然这种办法的性能和效率太低了,有没有更好的办法?还是直接用SQL?
问题补充
试了试,我觉得正解应该是这样的

List crs=session.createCriteria(Group.class).createAlias("persons", "p").add(Restrictions.eq("p.name", name)).list();

用HQL实现我做不出来了,不知道有没有高手能够用HQL把上面的条件查询翻译下。
问题补充:
上面的条件查询翻译成hql应该是

from Group where persons.name=?

但是执行时会报错

org.hibernate.QueryException: illegal attempt to dereference collection [{synthetic-alias}{non-qualified-property-ref}persons] with element property reference [name] [from com.iflysse.addressbook.domain.Group where persons.name=?]
org.hibernate.hql.ast.tree.DotNode$1.buildIllegalCollectionDereferenceException(DotNode.java:46)
org.hibernate.hql.ast.tree.DotNode.checkLhsIsNotCollection(DotNode.java:513)
org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:221)
org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:94)
org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:90)
org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:728)
org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1216)
org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4041)
org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3525)
org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1762)
org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)
org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
com.iflysse.addressbook.dao.GroupDao.search(GroupDao.java:68)
com.iflysse.addressbook.action.GroupAction.test(GroupAction.java:180)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.ilfysse.utils.HibernateSessionInViewFilter.doFilter(HibernateSessionInViewFilter.java:38)



看来是不是persons别名的问题?
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • wanghaolovezlq 2009-08-24 22:18
    已采纳

    select p.groups from Person p where p.name=?

    已采纳该答案
    打赏 评论
  • pp263020776 2009-08-14 10:00

    groups.get(0)

    打赏 评论
  • ylb_robin 2009-08-14 11:40

    为什么不建立双向联系,从person对象里查询该人的所属联系组呢?

    打赏 评论
  • wanghaolovezlq 2009-08-24 22:16

    这当然是错误的
    哪能给set集合直接这么赋值的
    from Group where [color=red]persons.name[/color]=?

    打赏 评论

相关推荐 更多相似问题