简体   繁体   English

使用标准作为注释的Hibernate / JPA

[英]Hibernate / JPA using criteria as annotations

Does anybody know if it's possible to impose criteria on an object property via annotations. 有人知道是否可以通过注释将标准强加于对象属性。 This would be similar to the @Where clause, however the attribute that I need to filter by is not actually on the table the SELECT is performed on since it is mappedBy. 这将类似于@Where子句,但是我需要过滤的属性实际上不在执行SELECT的表上,因为它已被映射。

Hopefully this example will illustrate: 希望这个例子可以说明:

public class Container {

  @OneToMany(mappedBy = "container")
  private List<Item> items;

  //getter & setter ...

}

public class Item {

  @ManyToOne
  private Container container;

  @OneToOne(mappedBy = "item")
  private User user;

  //getter & setter ...

}

public class User {

  @OneToOne
  private Item item;

  //getter & setter ...

}

I would like to fetch a Container where there is no User associated. 我想获取一个没有User关联的Container If the user attribute was really on the Item table it should be possible to use @Where: 如果user属性确实在Item表上,则应该可以使用@Where:

public class Container {

  @OneToOne(mappedBy = "container")
  @Where(clause = "user is null")
  private List<Item> items;

  //getter & setter ...

}

But since it is mappedBy is there a way of doing this? 但是由于它是mappedBy,有没有办法做到这一点? The join on these tables is obviously already performed in the SQL. 这些表上的联接显然已经在SQL中执行。 Maybe there is a way of referencing the user attribute eg HQL? 也许有一种方法可以引用user属性,例如HQL?

Any help much appreciated 任何帮助表示赞赏

In HQL : HQL

select item.container from item i where i.user is null

Container.java Container.java

@NamedQueries({
    @NamedQuery(
    name = "findContainerWithNoUser",
    query = "select item.container from item i where i.user is null"
    )
})
@Entity
public class Container {

  @OneToOne(mappedBy = "container")
  private List<Item> items;

  //getter & setter ...

}

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

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