两个不同实体中的相同@NamedQueries 会发出警告
Posted
技术标签:
【中文标题】两个不同实体中的相同@NamedQueries 会发出警告【英文标题】:Same @NamedQueries in two different entities gives warning 【发布时间】:2019-02-12 13:15:48 【问题描述】:我在两个实体中有相同的@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】:@NamedQuery
的范围是整个持久性单元。
在不同实体上定义它们并不重要。
当您定义重复名称时,很可能在构建持久性单元期间其中一个名称将被覆盖。
好的做法是在命名查询前加上实体名称:
@NamedQuery(name = "Student.getStudentById"..
@NamedQuery(name = "Marks.getStudentById"...
【讨论】:
以上是关于两个不同实体中的相同@NamedQueries 会发出警告的主要内容,如果未能解决你的问题,请参考以下文章