Search code examples
javajpaopenjpanamed-query

Same @NamedQueries in two different entities gives warning


I've same @NamedQueries in two entities as like below:

@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")
})

While I'm working on the above, I'm getting a warning like below:

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".

What is the reason and how can we get rid of this warning?


Solution

  • The scope of @NamedQuery is the entire persistence unit. It does not matter that they are defined on different entities.

    Most likely when you define a duplicate name, one of them will be overriden during building of the persistence unit.

    Good practice is to prefix your named queries with the entity names:

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