使用INNER JOIN和HQL

问题描述 投票:2回答:1

我正在尝试在HQl查询中使用内部联接,但它不起作用。

关于SQL的查询是:

SELECT A.first_name, F.title 
FROM film_actor X INNER JOIN actor A ON X.actor_id=A.actor_id
INNER JOIN film F ON F.film_id=X.film_id

它工作得很好,但是当我在HQL中使用它时:

SELECT A.firstName, F.title 
FROM FilmActor X INNER JOIN Actor A ON X.actorId=A.actorId 
INNER JOIN Film F ON F.filmId=X.filmId

它显示了这个错误:

org.hibernate.QueryException: outer or full join must be followed by path 
expression [SELECT A.firstName, F.title 
FROM sakila.entity.FilmActor X INNER JOIN Actor A ON X.actorId=A.actorId 
INNER JOIN Film F ON F.filmId=X.filmId]

另一个HQL:

SELECT A.firstName, F.title 
FROM FilmActor as X INNER JOIN X.actorId as actor
INNER  JOIN X.filmId as film

错误:

org.hibernate.QueryException: could not resolve property: actorId of: 
sakila.entity.FilmActor [SELECT A.firstName, F.title 
FROM sakila.entity.FilmActor as X INNER JOIN X.actorId as actor
INNER  JOIN X.filmId as film]

我尝试了一个新的查询

SELECT A.firstName, F.title 
FROM Actor A, Film F, FilmActor as X  INNER JOIN X.actor as actor 
INNER JOIN X.film as film

这不是我需要的,但它的连接查询工作,所以它的东西开始......

演员映射:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD     3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 22-feb-2018 15:04:57 by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="sakila.entity.Actor" table="actor" catalog="sakila"     optimistic-lock="version">
    <id name="actorId" type="java.lang.Short">
        <column name="actor_id" />
        <generator class="identity" />
    </id>
    <property name="firstName" type="string">
        <column name="first_name" length="45" not-null="true" />
    </property>
    <property name="lastName" type="string">
        <column name="last_name" length="45" not-null="true" />
    </property>
    <property name="lastUpdate" type="timestamp">
        <column name="last_update" length="19" not-null="true" />
    </property>
    <set name="filmActors" table="film_actor" inverse="true" lazy="true"         fetch="select">
        <key>
            <column name="actor_id" not-null="true" />
        </key>
        <one-to-many class="sakila.entity.FilmActor" />
    </set>
</class>
</hibernate-mapping>

电影映射:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD     3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 22-feb-2018 15:04:57 by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="sakila.entity.Film" table="film" catalog="sakila"     optimistic-lock="version">
    <id name="filmId" type="java.lang.Short">
        <column name="film_id" />
        <generator class="identity" />
    </id>
    <many-to-one name="languageByLanguageId" class="sakila.entity.Language"         fetch="select">
        <column name="language_id" not-null="true" />
    </many-to-one>
    <many-to-one name="languageByOriginalLanguageId"     class="sakila.entity.Language" fetch="select">
        <column name="original_language_id" />
    </many-to-one>
    <property name="title" type="string">
        <column name="title" not-null="true" />
    </property>
    <property name="description" type="string">
        <column name="description" length="65535" />
    </property>
    <property name="releaseYear" type="date">
        <column name="release_year" length="0" />
    </property>
    <property name="rentalDuration" type="byte">
        <column name="rental_duration" not-null="true" />
    </property>
    <property name="rentalRate" type="big_decimal">
        <column name="rental_rate" precision="4" not-null="true" />
    </property>
    <property name="length" type="java.lang.Short">
        <column name="length" />
    </property>
    <property name="replacementCost" type="big_decimal">
        <column name="replacement_cost" precision="5" not-null="true" />
    </property>
    <property name="rating" type="string">
        <column name="rating" length="5" />
    </property>
    <property name="specialFeatures" type="string">
        <column name="special_features" length="54" />
    </property>
    <property name="lastUpdate" type="timestamp">
        <column name="last_update" length="19" not-null="true" />
    </property>
    <set name="filmActors" table="film_actor" inverse="true" lazy="true"     fetch="select">
        <key>
            <column name="film_id" not-null="true" />
        </key>
        <one-to-many class="sakila.entity.FilmActor" />
    </set>
</class>
</hibernate-mapping>

FilmActor映射:

 <?xml version="1.0"?>
      <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 <!-- Generated 22-feb-2018 15:04:57 by Hibernate Tools 4.3.1 -->
 <hibernate-mapping>
<class name="sakila.entity.FilmActor" table="film_actor" catalog="sakila" optimistic-lock="version">
    <composite-id name="id" class="sakila.entity.FilmActorId">
        <key-property name="actorId" type="short">
            <column name="actor_id" />
        </key-property>
        <key-property name="filmId" type="short">
            <column name="film_id" />
        </key-property>
    </composite-id>
    <many-to-one name="actor" class="sakila.entity.Actor" update="false"      insert="false" fetch="select">
        <column name="actor_id" not-null="true" />
    </many-to-one>
    <many-to-one name="film" class="sakila.entity.Film" update="false" insert="false" fetch="select">
        <column name="film_id" not-null="true" />
    </many-to-one>
    <property name="lastUpdate" type="timestamp">
        <column name="last_update" length="19" not-null="true" />
    </property>
</class>
 </hibernate-mapping>
java hibernate hql
1个回答
0
投票

使用这句话:SELECT Distinct F.title FROM Film F,FilmActor X INNER join X.actor inner join X.film WHERE X.actor ='1'它显示所有电影的列表,但不仅仅是演员的电影与actorId = 1有什么不对?

尝试:

SELECT F.title, A.firstName FROM Film F
JOIN f.film_actors fa
JOIN fa.actor ac
WHERE ac.id = '1'
© www.soinside.com 2019 - 2024. All rights reserved.