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

Posted

技术标签:

【中文标题】org.postgresql.util.PSQLException:错误:关系不存在 PreparedStatement.executeQuery ()【英文标题】:org.postgresql.util.PSQLException : ERROR : relation does NOT exist PreparedStatement.executeQuery () 【发布时间】:2017-04-11 15:08:15 【问题描述】:

由于某种奇怪的原因,hibernate 生成的查询不能与 postgres 一起使用,它告诉它找不到关系/表,即使 dvdrental 架构中有有效的表 film_actor?这个answer 没有帮助。

例外:

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;

基于Film实体注解的映射:

@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"*/ 后问题就消失了。

【问题讨论】:

目录不是模式,它是更上一层楼。见What's the difference between a catalog and a schema in a relational database? 【参考方案1】:

您必须指定架构,而不是目录。 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;

【讨论】:

以上是关于org.postgresql.util.PSQLException:错误:关系不存在 PreparedStatement.executeQuery ()的主要内容,如果未能解决你的问题,请参考以下文章