加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – HQL Join – 加入的路径!过冬

发布时间:2021-03-20 21:45:17 所属栏目:MsSql教程 来源:网络整理
导读:我是hibernate的新手,遇到了以下问题: 我得到了“期待加入的路径

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

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

解决方法

正如您链接到的问题所解释的那样,在 Hibernate documentation中,连接使用实体之间的关联.所以正确的查询是
select avg(t.price) from Ticket t join t.flight f where f.number = :flightNumber

另请注意,使用参数是比直接在查询中连接值更好的解决方案.它自动处理引用和转义,并且没有任何HQL注入风险.

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读