2013-05-03 82 views
4

我是新來的休眠,我遇到了以下問題: 我得到了「加入的路徑!」例外,當我試圖運行此查詢:HQL加入 - 加入的路徑!休眠

String hql = "select avg(t.price) from Ticket t JOIN Flight f WHERE f.number = '" + flightNumber + "'"; 
Query query = this.session.createQuery(hql);   
List<Double> list = query.list(); 

我想選擇已賣了給定機票的平均價格。

我已經檢查了這些鏈接,但我並沒有解決我的問題: HQL left join: Path expected for join hql inner join Path expected for join! error

我的代碼是:

Flight.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="pck.Flight" table="flight" catalog="airbook"> 
     <id name="id" type="java.lang.Integer"> 
      <column name="id" /> 
      <generator class="identity" /> 
     </id> 
     <many-to-one name="sourceairport" class="pck.Sourceairport" fetch="select"> 
      <column name="sourceairportid" /> 
     </many-to-one> 
     <many-to-one name="destinationairport" class="pck.Destinationairport" fetch="select"> 
      <column name="destinationairportid" /> 
     </many-to-one> 
     <property name="number" type="string"> 
      <column name="number" length="30" /> 
     </property> 
     <property name="date" type="timestamp"> 
      <column name="date" length="19" /> 
     </property> 
     <property name="miles" type="java.lang.Integer"> 
      <column name="miles" /> 
     </property> 
     <property name="numberofseats" type="java.lang.Integer"> 
      <column name="numberofseats" /> 
     </property> 
     <property name="airplane" type="string"> 
      <column name="airplane" length="30" /> 
     </property> 
     <set name="tickets" table="ticket" inverse="true" lazy="true" fetch="select"> 
      <key> 
       <column name="flightid" /> 
      </key> 
      <one-to-many class="pck.Ticket" /> 
     </set> 
    </class> </hibernate-mapping> 

Ticket.hbm。 xml

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="pck.Ticket" table="ticket" catalog="airbook"> 
     <id name="id" type="java.lang.Integer"> 
      <column name="id" /> 
      <generator class="identity" /> 
     </id> 
     <many-to-one name="flight" class="pck.Flight" fetch="select"> 
      <column name="flightid" /> 
     </many-to-one> 
     <many-to-one name="passenger" class="pck.Passenger" fetch="select"> 
      <column name="passengerid" /> 
     </many-to-one> 
     <property name="price" type="java.lang.Double"> 
      <column name="price" precision="22" scale="0" /> 
     </property> 
    </class> 
</hibernate-mapping> 

所有其他沒有JOIN的查詢都可以正常工作。我不知道問題出在哪裏。


正確的查詢是:

select avg(t.price) from Ticket t join t.flight f where f.number = :flightNumber 

而且完全與查詢執行:

Transaction tx = session.beginTransaction(); 
String hql = "select avg(t.price) from Ticket t join t.flight f where f.number = :flightNumber"; 
Query query = this.session.createQuery(hql).setString("flightNumber", flightNumber); 
List<Double> list = query.list(); 
tx.commit(); 

回答

14

如您鏈接到的問題說明,並在Hibernate documentation,加入之間利用關聯實體。所以正確的查詢是

select avg(t.price) from Ticket t join t.flight f where f.number = :flightNumber 

另外請注意,使用參數比在查詢中直接連接值更好。它自動處理引用和轉義,並且沒有任何HQL注入的風險。

+0

謝謝。這解決了我的問題。正確的查詢實際上是「從Ticket t中選擇avg(t.price)」t join t.flight f其中f.number =:flightNumber「 – user1326050 2013-05-03 09:10:27

+0

糟糕。是。我確定了答案。 – 2013-05-03 09:11:59

+1

在這種情況下,內部連接是可選的,因此它可以以更易讀的形式寫入:'從Ticket t中選擇svg(t.price),其中t.flight.number =:flightNumber' – 2013-05-03 10:30:40