hibernate3.2多表關聯查詢常見問題

來源:互聯網
上載者:User

標籤:持久化類   根目錄   ber   java類   關聯查詢   版本   mil   關聯   lips   

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‘;

 

hibernate3.2多表關聯查詢常見問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.