JPA JOIN查询问题

时间:2011-11-20 20:08:22

标签: java jpa

我正在尝试使用JPA查询,但在以下查询中不断出现错误

SELECT p
FROM usertype_permissions up
JOIN permissions p ON up.PERMISSION_ID = p.ID
WHERE up.USERTYPE_ID = 1

错误:

Caused by: java.lang.IllegalArgumentException: An exception occurred while
creating a query in EntityManager: 

Exception Description: Syntax error parsing the query
[SELECT p
FROM usertype_permissions up
JOIN permissions p ON up.PERMISSION_ID = p.ID
WHERE up.USERTYPE_ID = 1].

1 个答案:

答案 0 :(得分:0)

在JPQL中你必须使用实体名称(默认情况下它是Java类的名称),对我来说,usertype_permissions是实体名称/ Java类看起来很奇怪......

看起来您正在使用表名,并且连接后的单词必须引用类的属性,例如l.books

示例:

@Entity
public class Lib implements Serializable {
      @OneToMany
      private Set<Book> books;
}

@Entity
public class Book implements Serializable {
      @OneToOne
      private Lib lib;
}

JPQL查询如下所示:

SELECT l FROM Lib JOIN l.books books