HQL Join - 加入的路径!过冬 [英] HQL Join - Path expected for join! hibernate

查看:94
本文介绍了HQL Join - 加入的路径!过冬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是hibernate的新手,遇到以下问题:
当我尝试运行此查询时,出现了期望加入的路径!异常。

String hql =从票证中选择avg(t.price)t JOIN Flight f WHERE f.number ='+ flightNumber +';
查询查询= this.session.createQuery(hql);
列表< Double> list = query.list();

我想选择某个航班已售出机票的平均价格。



我检查了这些链接,但我没有解决我的问题:
HQL左连接:加入的路径
hql内部联接加入的路径!错误



我的代码是:

Flight.hbm.xml

<?xml version =1.0?><!DOCTYPE hibernate-mapping PUBLIC
- // Hibernate / Hibernate映射DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping>
< class name =pck.Flighttable =flightcatalog =airbook>
< id name =idtype =java.lang.Integer>
< column name =id/>
< generator class =identity/>
< / id>
< column name =sourceairportid/>
< /多对一>
< column name =destinationairportid/>
< /多对一>
< property name =numbertype =string>
< column name =numberlength =30/>
< / property>
< property name =datetype =timestamp>
< column name =datelength =19/>
< / property>
< property name =milestype =java.lang.Integer>
< column name =miles/>
< / property>
< property name =numberofseatstype =java.lang.Integer>
< column name =numberofseats/>
< / property>
< property name =airplanetype =string>
< column name =airplanelength =30/>
< / property>
< set name =ticketstable =ticketinverse =truelazy =truefetch =select>
< key>
< column name =flightid/>
< / key>
<一对多课程=pck.Ticket/>
< / set>
< / class> < /休眠映射>

Ticket.hbm.xml

 <?xml version =1.0?> <!DOCTYPE hibernate-mapping PUBLIC -  // Hibernate / Hibernate映射DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping>
< class name =pck.Tickettable =ticketcatalog =airbook>
< id name =idtype =java.lang.Integer>
< column name =id/>
< generator class =identity/>
< / id>
<多对一名称=飞行class =pck.Flightfetch =select>
< column name =flightid/>
< /多对一>
<多对一名称=乘客class =pck.Passengerfetch =select>
< column name =passengerid/>
< /多对一>
< property name =pricetype =java.lang.Double>
< column name =priceprecision =22scale =0/>
< / property>
< / class>
< / hibernate-mapping>

所有其他没有JOIN的查询都可以正常工作。我不知道问题出在哪里。






正确的查询是:

 从Ticket中选择avg(t.price)t join t.flight f其中f.number =:flightNumber 

并且一起执行查询:

 事务tx = session.beginTransaction(); 
String hql =从Ticket中选择avg(t.price)t join t.flight f where f.number =:flightNumber;
查询查询= this.session.createQuery(hql).setString(flightNumber,flightNumber);
列表< Double> list = query.list();
tx.commit();


解决方案

正如您所链接到的问题所解释的, Hibernate文档,连接使用实体之间的关联。所以正确的查询是:

 从Ticket中选择avg(t.price)t join t.flight f where f.number =: flightNumber 

另外请注意,使用参数比直接在查询中连接值要好得多。它自动处理引用和转义,并且没有任何HQL注入风险。


I am new to hibernate and I met a following problem: I got "Path expected for join!" exception when I tried to run this query:

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();

I wanted to select average price of tickets that have been sold for a given flight.

I have checked these links, but I did not solve my problem: HQL left join: Path expected for join hql inner join Path expected for join! error

My code is:

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>

All the other queries without JOIN work fine. I do not know where the problem is.


The correct query is:

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

And altogether with query execution:

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();

解决方案

As explained in the question you linked to, and in the Hibernate documentation, joins use associations between entities. So the correct query is

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

Also note that using parameters is a much better solution than concatenating values directly in the query. It handles quoting and escaping automatically, and doesn't have any risk of HQL injection.

这篇关于HQL Join - 加入的路径!过冬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆