小编典典

HQL加入-预期加入的路径!冬眠的

sql

我刚进入休眠状态,遇到了以下问题:我收到了 “希望加入的路径!” 当我尝试运行此查询时出现异常:

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

我的代码是:

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

阅读 154

收藏
2021-03-23

共1个答案

小编典典

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

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

还要注意,与直接在查询中串联值相比,使用参数是一种更好的解决方案。它可以自动处理报价和转义,并且没有注入HQL的风险。

2021-03-23