hibernate3.2多表关联查询常见问题
1.org.hibernate.hql.ast.QuerySyntaxException: user is not mapped [from user]錯誤
今天學習hibernate。用MyEclipse部署。但是出現
org.hibernate.hql.ast.QuerySyntaxException: user is not mapped [from user]
在網上查了原因:
1、hibernate.cfg.xml少了很多映射.為什么會少呢?因為我的都是拷貝過來的。不是自動生成的。另外 hibernate.cfg.xml,要放在根目錄下。
把你要映射的都寫上去。
<mapping resource="....包名.../Companyinfo.hbm.xml" />
<mapping resource="....包名.../Companyinfo.hbm.xml" />
<mapping resource="....包名.../Companyinfo.hbm.xml" />
<mapping resource="....包名.../Companyinfo.hbm.xml" />
<mapping resource="....包名.../Companyinfo.hbm.xml" />
2、現象:
使用hql="from person" 出現" person is not mapped " 錯誤
配置文件如下:
<hibernate-mapping>
<class name="src.Person"
table="person">
<id name="name"/>
<property name="password"/>
</class>
</hibernate-mapping>
原 因:
hql是寫的是PO對象,不是table名
故改為配置文件中的紅色部分的類名即可。
?
我出現的是第二種情況,記錄一下。
2.
org.hibernate.QueryException: could not resolve property
?
一般地,如果涉及到屬性類型無法解析的異常,可能出現問題的地方有:
數據庫字段與持久化類映射文件,以及持久化類文件中屬性名稱或者類型可能不相匹配;
持久化類映射文件中屬性類型可能有問題,比如,如果使用Java類型,注意大寫(如type="java.lang.String"),如果使用Hibernate類型,使用小寫(如type="string")。
?
3.illegal attempt to dereference collection
from Department as d where d.employees.name='Tom';
運行時出現異常:org.hibernate.QueryException: illegal attempt to dereference collection
是因為:在上面的HQL語句中,Department的關聯實體employees是一個集合,而不直接是一個Employee實體。
在Hibernate3.2.2以前的版本,Hibernate會對關聯實體自動使用隱式的inner join,
也就是說如下SQL語句不會有任何問題?:from Department as d where d.employees.name='Tom';
從Hibernate3.2.3以后,Hibernate改變了這種隱式的inner join的策略
對于如下這條語句:
from Department as d where d.employees.name='Tom';
如果employees是普通組件屬性,或單個的關聯實體,則Hibernate會自動生成隱式的inner join
如果myEvents是也一個集合,那么對不起!系統將會出現 org.hibernate.QueryException: illegal attempt to dereference collection異常。
據Hibernate官方說法:?
這樣可以讓這使得隱含關聯更具確定性(原文:This makes implicit joins more deterministic )。
推薦這樣寫:
from Department as d inner join fetch d.employees e where e.name='Tom';
?
轉載于:https://www.cnblogs.com/dandan1224/p/6109319.html
總結
以上是生活随笔為你收集整理的hibernate3.2多表关联查询常见问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 操作系统的实现(0)
- 下一篇: 开发工具的异常现象