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