2

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?

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
sivabalaji
  • 43
  • 1
  • 8

1 Answers1

2

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"...
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • "prefix named queries with entity names" , i thought this should be a MUST, instead of good practice. Cos when i try out without the prefix, my spring boot app fail to boot up succcessfully. I also not sure why we are forced to include the entity name when the entity name can be inferred from the class which the @NamedQuery annotates. – Nick Wills Aug 19 '22 at 05:13