How to Define All Named Queries in One place (like class) instead of writing the NamedQuery in top of Entity class.
For Example i have a base class. In this class i can define all named queries.
Base.java
@Entity
@NamedQueries({
@NamedQuery(name="Student.findAll", query="SELECT s FROM Student s"),
@NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e")})
class Base {
}
Assume now i have two entities like Student and Employee. Now i want to access the Named query from Base class. It is possible to access. If is possible then how.
class Employee {
}
class Student {
}
In employee class, Student i want to access the named query from Base class. It is possible. If it is possible how i can access.
We did a similar thing on one project. We had one entity that contained all of our named queries and (as @jimfromsa already said) they are accessible to your code wherever you need them because you are calling them by their unique name.
Note that this entity doesn't need to be mapped to an existing table, as long as you don't use it anywhere in the code. At least this approach worked with EclipseLink, never tried it with Hibernate.
In your case, if you don't want this extra query holder entity, you can put all named queries in
Base
class and it will work. However, I don't think it is a good idea to access named queries from your entity classes, they are called POJOs for a reason. Then again, it is all a matter of design (take a look at Anemic Domain Model).