org.postgresql.util.PSQLException:错误:关系不存在PreparedStatement.executeQuery()

时间:2016-11-27 23:23:26

标签: java spring postgresql hibernate

由于某些奇怪的原因,hibernate生成的查询无法使用postgres,它告诉它找不到关系/表,即使film_actor模式中有有效的表dvdrentalanswer没有帮助。

例外:

org.postgresql.util.PSQLException : ERROR : relation "dvdrental.film_actor" does NOT exist PreparedStatement.executeQuery () FAILED ! 

HQL

Query searchQuery = session.createQuery("select film from Film as film " +
                    "inner join film.actors  as a " +
                    "inner join film.categories as c " +
                    "where c.categoryId=:categoryId " +
                    "and film.language.id=:languageId " +
                    "and film.releaseYear=:releaseYear " +
                    "and a.actorId=:actorId");

生成SQL

SELECT
    film0_.film_id AS film_id1_8_,
    film0_.description AS descript2_8_,
    film0_.language_id AS languag12_8_,
    film0_.last_update AS last_upd3_8_,
    film0_. LENGTH AS length4_8_,
    film0_.rating AS rating5_8_,
    film0_.release_year AS release_6_8_,
    film0_.rental_duration AS rental_d7_8_,
    film0_.rental_rate AS rental_r8_8_,
    film0_.replacement_cost AS replacem9_8_,
    film0_.special_features AS special10_8_,
    film0_.title AS title11_8_
FROM
    dvdrental. PUBLIC .film film0_
INNER JOIN dvdrental.film_actor actors1_ ON film0_.film_id = actors1_.film_id
INNER JOIN dvdrental. PUBLIC .actor actor2_ ON actors1_.actor_id = actor2_.actor_id
INNER JOIN dvdrental.film_category categories3_ ON film0_.film_id = categories3_.film_id
INNER JOIN dvdrental. PUBLIC .category category4_ ON categories3_.category_id = category4_.category_id
WHERE
    category4_.category_id = 1
AND film0_.language_id = 1
AND film0_.release_year = 2016
AND actor2_.actor_id = 2;

此查询正常工作:

SELECT
    *
FROM
film f
INNER JOIN film_actor fa ON f.film_id = fa.film_id
LIMIT 100;

基于电影实体注释的映射:

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "film_actor", catalog = "dvdrental", joinColumns = {
            @JoinColumn(name = "film_id", nullable = false, updatable = false) },
            inverseJoinColumns = { @JoinColumn(name = "actor_id",
                    nullable = false, updatable = false) })
    public Set<Actor> getActors() {
        return actors;
    }

    public void setActors(Set<Actor> actors) {
        this.actors = actors;
    }

解决方案/哈克:

评论/*catalog = "dvdrental"*/后,问题就消失了。

1 个答案:

答案 0 :(得分:2)

您必须指定架构,而不是目录。 Link

在生成的SQL查询中,您搜索表dvdrental.PUBLIC.film,我假设只存在dvdrental.film

更新了基于电影实体注释的映射:

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "film_actor", schema= "dvdrental", joinColumns = {
        @JoinColumn(name = "film_id", nullable = false, updatable = false) },
        inverseJoinColumns = { @JoinColumn(name = "actor_id",
                nullable = false, updatable = false) })
public Set<Actor> getActors() {
    return actors;
}

public void setActors(Set<Actor> actors) {
    this.actors = actors;
}