Java Hibernate Criteria select subclass

大城市里の小女人 提交于 2019-12-19 10:33:40

问题


I want to use the Criteria API to select entities by taking the input from a search value. 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? I need to do search all subclasses and connect them with an OR. Is there an elegant way?

greetings m


回答1:


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


来源:https://stackoverflow.com/questions/11082364/java-hibernate-criteria-select-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!