Java JPA中具有继承性的JPQL
我拥有这些实体:Java JPA中具有继承性的JPQL,java,jpa,mapping,jpql,Java,Jpa,Mapping,Jpql,我拥有这些实体: @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Item implements Serializable{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(nullable = false) @NotNull private
@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;
}
我的问题是:
对于Namedquery,我必须将它们放在item实体或book实体中
例如,如果我必须用price=30搜索图书,那么这个namedquery必须在超类或子类中
我们知道在数据库项中有一个表,即如何使用此体系结构管理jpql
编辑:
如果我有这个问题:搜索价格为30的书
我可以用这个名称回答查询:
@NamedQuery(name=Book.FIND_BY_PRICE,query="select b from Book b where b.price = :bprice")
或:
您可以将命名查询放入所需的任何实体类中。因为命名查询是关于查找书籍的,所以我将它放在Book实体中 关于JPQL查询,您可以像编写任何其他JPQL查询一样编写它们:使用实体名称和字段,并让JPA实现生成适当的SQL,因为它知道它们的映射和继承关系
不过,我要做的第一件事是将item类重命名为item,以遵守Java命名约定。为了回答您的问题,我已将问题编辑为Clear item类没有数据类型字段。所以不,你不能用第二个。第一个是您想要的,并且将生成正确的SQL,因为JPA知道books是数据类型列中具有“Book”的item表的行。查询书籍的方式与查询其他实体的方式完全相同。顺便说一句,你可以很容易地尝试自己。
@NamedQuery(name=Book.FIND_BY_PRICE,query="select i from Item i where i.dtype='book' and i.price = :bprice")