小编典典

Java Hibernate Criteria选择子类

hibernate

我想使用Criteria API通过从搜索值中获取输入来选择实体。一个文档可以有更多的收件人。收件人有许多子类

@Entity
public class Document implements Serializable {
  @OneToMany(mappedBy="document")
  private List<Recipient> recipients = new ArrayList<Recipient>();


@Entity
public class RecipientAccount extends Recipient {
  String name;

如何选择所有带有特定名字的ReciepientAccount的文档?我需要搜索所有子类并将它们与OR连接。有优雅的方法吗?

问候m


阅读 265

收藏
2020-06-20

共1个答案

小编典典

以下应该工作:

Criteria c = session.createCriteria(Document.class, "document");
c.createAlias("document.recipients", "recipient");
c.add(Restrictions.in("recipient.class", Arrays.asList(SubClass1.class, 
                                                       SubClass2.class,
                                                       SubClass3.class)));
c.add(Restrictions.eq("recipient.name", theName));
2020-06-20