由于某种奇怪的原因,hibernate生成的查询不能与postgres一起工作,它告诉它即使在film_actor
模式中有有效的表dvdrental
也找不到关系/表?这个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;
基于胶片实体注释的映射:
@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;
}
解决方案/黑客:
在评论之后,问题就消失了。
最佳答案
必须指定架构,而不是目录。
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;
}