简体   繁体   English

JPQL在JPA中具有继承性

[英]JPQL with inheritence in JPA

I have theses Entity : 我有这些实体:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Item implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(nullable = false)
    @NotNull
    private String title;
    @Column(nullable = false)
    @NotNull
    private Integer price;
}
@Entity
public class Book extends Item implements Serializable{
    @Column(nullable = false)
    @NotNull
    private String isbn;
    @Column(nullable = false)
    @NotNull
    private String language;
}

my questions are : 我的问题是:

for the Namedquery i have to put them in the item Entity or in the book entity? 对于Namedquery,我必须将它们放在项目实体或书本实体中?

For example if i had to search the book with price=30 , this namedquery must be in the superclass or in the subclass? 例如,如果我必须搜索price = 30的书,则此namedquery必须在超类中还是在子类中?

we know that we wil have one table in the database Item,how to manage the jpql with this architecture. 我们知道我们在数据库Item中将只有一个表,如何使用这种架构来管理jpql。

EDIT: if i have this question:search the bookwith price= 30 , 编辑:如果我有这个问题:搜索价格为30的书,

can i respond with this namedquery: 我可以使用此namedquery进行响应吗:

@NamedQuery(name=Book.FIND_BY_PRICE,query="select b from Book b where b.price = :bprice")

or : 要么 :

@NamedQuery(name=Book.FIND_BY_PRICE,query="select i from Item i where i.dtype='book'  and i.price = :bprice")

You put the named query in whatever entity class you desire. 您可以将命名查询放在所需的任何实体类中。 Since the named query is about looking for books, I would put it in the Book entity. 由于命名查询是关于寻找书籍的,因此将其放在Book实体中。

Regarding the JPQL queries, you write them as any other JPQL query: using the entity names and fields, and letting the JPA implementation generate the appropriate SQL thanks to its knowledge of their mapping and inheritance relationship. 关于JPQL查询,您可以将它们与其他任何JPQL查询一样编写:使用实体名称和字段,并借助JPA实现对它们的映射和继承关系的了解,让JPA实现生成适当的SQL。

The first thing I would do though is to rename the item class to Item, to respect the Java naming conventions. 不过,我要做的第一件事是将item类重命名为Item,以遵守Java命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM