两个不同实体中的相同@NamedQueries发出警告

时间:2019-02-12 13:15:49

标签: jpa openjpa named-query

我在两个实体中都具有相同的@NamedQueries,如下所示,

@Table(name = "STUDENT_TABLE")
@NamedQueries({ 
@NamedQuery(name = "getStudentById", query = "SELECT s FROM Student s where s.stdId=:stdId"),
@NamedQuery(name = "getStudentByName", query = "SELECT s FROM Student s where s.fName=:fName and s.lName =:lName")
})

@Table(name = "MARKS_TABLE")
@NamedQueries({ 
@NamedQuery(name = "getStudentById", query = "SELECT s FROM Student s where s.stdId=:stdId"),
@NamedQuery(name = "getStudentByName", query = "SELECT s FROM Student s where s.fName=:fName and s.lName =:lName")
})

在我进行上述操作时,我收到如下警告,

openjpa.MetaData: Warn: Ignoring duplicate query "getStudentById" in "class Student". A query with the same name been already declared in "class Marks".
openjpa.MetaData: Warn: Ignoring duplicate query "getStudentByName" in "class Student". A query with the same name been already declared in "class Marks".

原因是什么,我们如何摆脱这个警告?

1 个答案:

答案 0 :(得分:1)

@NamedQuery的范围是整个持久性单元。 不必在不同实体上定义它们。

最有可能在您定义重复名称时,在构建持久性单元时将覆盖其中之一。

优良作法是在命名查询前添加实体名称:

@NamedQuery(name = "Student.getStudentById"..
@NamedQuery(name = "Marks.getStudentById"...