如上面是Java文件和配置文件所示,TObject是提供基本属性(id、name等)及属性的get\set方法的父类,一个TA可以有多个TB,一个TB只能有一个TA。 我的查询语句是“from TA pojo where pojo.tbs.name='tb1'”,可结果报错。 我在网上看到,有人说这样写是正确的,也有人说这样写是错误的, 所以,我想请教下大侠们,这样写到底是正确的还是错误的啊? 如果是正确的,怎么样才能不报错并显示正常查询结果呢? 小弟先谢谢了! [最优解释]
高版本的Hibernate不能这样查Set了, 要改成这样:
from TA pojo inner join fetch pojo.tbs tb where tb.name='tb1' [其他解释] 网上这种资料那是相当多的,lz还不如自己查下原因咧! 当初我学的时候也这样,你要懂得原理,具体写法自己百度下就行了。 [其他解释] 我的查询语句是“from TA pojo where pojo.tbs.name='tb1'”,可结果报错。?
应该把错误贴出来 ,不然怎么给你定位 [其他解释] 你自己先测试一下,在你的TA 和TB里都设置哥name属性,看看行不行,如果行的话就说明是继承映射的问题,如果不行的话,那就是配置文件的问题 [其他解释] String hql = "from TA pojo where pojo.tbs.name= :name"; Query q = session.createQuery(hql); q.setParameter("name", name); return q.list(); [其他解释] set.name ... 看你这个查询还不如用sql select * from ta where ta.taName='';
另外咨询个问题,对于有一对多关系的两个表TA和TB,TB里有一个TA的实例ta, 这种情况下,用join连接查询“select ta from TA ta join ta.tbSet tb where tb.name=?”和 用where连接“select ta from TA ta ,TB tb where ta.id=tb.ta and tb.name=?” 这两种哪个效率高、执行快啊?! [其他解释]
javax.servlet.ServletException: org.springframework.orm.hibernate3.HibernateQueryException: illegal attempt to dereference collection [ta0_.ta_id.tcSet] with element property reference [vv] [from test.TA pojo where pojo.tcSet.vv='tb1']; nested exception is org.hibernate.QueryException: illegal attempt to dereference collection [ta0_.ta_id.tcSet] with element property reference [vv] [from test.TA pojo where pojo.tcSet.vv='tb1'] 上面就是错误。 另外问下,