简体   繁体   中英

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?

For example if i had to search the book with price=30 , this namedquery must be in the superclass or in the subclass?

we know that we wil have one table in the database Item,how to manage the jpql with this architecture.

EDIT: if i have this question:search the bookwith price= 30 ,

can i respond with this 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.

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.

The first thing I would do though is to rename the item class to Item, to respect the Java naming conventions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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