简体   繁体   English

Java Hibernate Criteria选择子类

[英]Java Hibernate Criteria select subclass

I want to use the Criteria API to select entities by taking the input from a search value. 我想使用Criteria API通过从搜索值中获取输入来选择实体。 A document can have more recipients. 一个文档可以有更多的收件人。 A recipient has many subclasses 收件人有许多子类

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


@Entity
public class RecipientAccount extends Recipient {
  String name;

How can i select all documents which have a ReciepientAccount with a certain name? 如何选择所有带有特定名字的ReciepientAccount的文档? I need to do search all subclasses and connect them with an OR. 我需要搜索所有子类并将它们与OR连接。 Is there an elegant way? 有优雅的方法吗?

greetings m 问候m

The following should work: 以下应该工作:

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

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

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