Java Hibernate Criteria选择子类

时间:2012-06-18 11:58:51

标签: java hibernate criteria-api

我想使用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连接它们。有优雅的方式吗?

问候 米

1 个答案:

答案 0 :(得分:6)

以下内容应该有效:

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));