sql - HQL Join - Path expected for join! hibernate -
i new hibernate , met following problem: got "path expected join!" exception when tried run query:
string hql = "select avg(t.price) ticket t join flight f f.number = '" + flightnumber + "'"; query query = this.session.createquery(hql); list<double> list = query.list(); i wanted select average price of tickets have been sold given flight.
i have checked these links, did not solve problem: hql left join: path expected join hql inner join path expected 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 other queries without join work fine. not know problem is.
the correct query is:
select avg(t.price) ticket t join t.flight f f.number = :flightnumber and altogether query execution:
transaction tx = session.begintransaction(); string hql = "select avg(t.price) ticket t join t.flight f f.number = :flightnumber"; query query = this.session.createquery(hql).setstring("flightnumber", flightnumber); list<double> list = query.list(); tx.commit();
as explained in question linked to, , in hibernate documentation, joins use associations between entities. correct query is
select avg(t.price) ticket t join t.flight f f.number = :flightnumber also note using parameters better solution concatenating values directly in query. handles quoting , escaping automatically, , doesn't have risk of hql injection.
Comments
Post a Comment