我们可以在一个地方定义所有的命名查询,而不是写在实体类中 [英] Can we define all the Named Queries at one place like some property file instead of writing in the entity class
问题描述
如何在一个地方(如类)中定义所有命名查询,而不是在实体类顶部编写 NamedQuery 。
例如我有一个基类。在这个类中,我可以定义所有命名查询。
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 {
}
现在假设我有两个实体,例如Student和Employee。现在我想从基类访问命名查询。可以访问。如果可能,那么怎么做。
class Employee {
}
班学生{
}
在员工类中,学生我想访问来自Base类的命名查询。有可能的。如果可能的话我可以访问。
我们在一个项目上做了类似的事情。我们有一个包含所有命名查询的实体,并且(如@jimfromsa已经说过的),只要你需要它们,你的代码就可以访问它们,因为你用它们的唯一名称来调用它们。
@Entity
@NamedQueries({
@NamedQuery(name =Student.findAll,query =SELECT s FROM Student's),
@NamedQuery(name =Employee.findAll,query =SELECT e FROM Employee e),
...
@NamedQuery( name =SomeEntity.someQuery,query =SELECT se FROM SomeEntity se)
})
public class NamedQueryHolder implements Serializable {
//实体需要一个id
@I $
私人整数ID;
// getter and setter for id
}
注意只要您不在代码中的任何位置使用此实体,就不需要将其映射到现有的表。至少这种方法与EclipseLink一起工作,从来没有用Hibernate尝试过。
在你的情况下,如果你不想要这个额外的查询持有者实体,你可以把所有命名查询放在 Base $ c $它会工作。但是,我认为从实体类访问命名查询并不是一个好主意,因为某种原因它们被称为POJO。再说一遍,这完全是设计问题(请看贫血域模型)。
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.
@Entity
@NamedQueries({
@NamedQuery(name="Student.findAll", query="SELECT s FROM Student s"),
@NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e"),
...
@NamedQuery(name="SomeEntity.someQuery", query="SELECT se FROM SomeEntity se")
})
public class NamedQueryHolder implements Serializable {
// entity needs to have an id
@Id
private Integer id;
// getter and setter for id
}
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).
这篇关于我们可以在一个地方定义所有的命名查询,而不是写在实体类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!