简体   繁体   English

JPA标准-where子句

[英]JPA Criteria - where clause

I have a Entity class which contain some primitive data type and contain some collection of object which have OneToMany relation and ManyToOne relation. 我有一个Entity类,其中包含一些原始数据类型,并包含一些具有OneToMany关系和ManyToOne关系的对象的集合。

And when I am Fetching data by using JPA Criteria then I am getting all the data but I want to filter the result by using JPA Criteria where clause , I can apply in its primitive data type and its work fine , I have to apply where clause in collection of object which have OneToMany relation some have ManyToOne relation but how to apply where clause in these case , can you tell me ? 当我使用JPA Criteria提取数据时,我会获取所有数据,但是我想使用JPA Criteria where clause过滤结果,我可以应用其原始数据类型并且可以正常工作,我必须应用where子句在具有OneToMany关系的对象集合中,有些具有ManyToOne关系,但是在这种情况下如何应用where子句,您能告诉我吗?

I did that on this way: 我这样做是这样的:

Repository 资料库

@Query(value = "SELECT e FROM Employee e JOIN e.user u JOIN u.role r JOIN r.grants g where g = :grant")
List<Employee> findByGrant(@Param("grant") Grant grant);

Grant is related ManyToMany with Role wich is related OneToOne with employee. Grant与Role关联到ManyToMany,而OneToOne与员工关联。

So you just need to Join the Object and filter with an object setting the PKEY (ID). 因此,您只需要加入对象并使用设置PKEY(ID)的对象进行过滤。

Entities 实体

@Entity
public class Employee {

  @OneToOne
  @JoinColumn(name = "user_id")
  private User user;
}

@Entity
public class User implements UserDetails {

  /**
   * 
   */
  private static final long serialVersionUID = 7854391295707311278L;

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;

 @OneToOne
 @JoinColumn(name = "role_id")
 private Role role;

}

@Entity
public class Role {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.MERGE)
  @JoinTable(
    name = "role_has_grant", 
    joinColumns = {
      @JoinColumn(name = "role_id", referencedColumnName = "id") 
    },
     inverseJoinColumns = {
      @JoinColumn(name = "grant_id", referencedColumnName = "id") 
     })
   @Fetch(FetchMode.SELECT)
   private List<Grant> grants;
}

@Entity
public class Grant implements GrantedAuthority {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
}

Another relation 另一个关系

@Query(value = "SELECT a FROM Activity a JOIN a.task t WHERE t.project = :project AND a.start BETWEEN :start AND :end") 

@Entity
public class Task {

@ManyToOne
@JoinColumn(name = "project_id")
private Project project;

@OneToMany(mappedBy = "task")
private List<Activity> activities;

} }

Use a .join() . 使用.join() Here is an example from the Criteria API documentation. 这是Criteria API文档中的示例

For queries that navigate to related entity classes, the query must define a join to the related entity by calling one of the From.join methods on the query root object or another join object. 对于导航到相关实体类的查询,查询必须通过调用查询根对象或另一个连接对象上的From.join方法之一来定义与相关实体的连接。 The join methods are similar to the JOIN keyword in JPQL. 连接方法类似于JPQL中的JOIN关键字。

The target of the join uses the Metamodel class of type EntityType to specify the persistent field or property of the joined entity. 联接的目标使用EntityType类型的Metamodel类来指定联接实体的持久字段或属性。

The join methods return an object of type Join<X, Y>, where X is the source entity and Y is the target of the join. 连接方法返回类型为Join <X,Y>的对象,其中X是源实体,Y是连接的目标。 In the following code snippet, Pet is the source entity, Owner is the target, and Pet_ is a statically generated metamodel class: 在以下代码段中,Pet是源实体,Owner是目标,Pet_是静态生成的元模型类:
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class);

Root<Pet> pet = cq.from(Pet.class);
Join<Pet, Owner> owner = pet.join(Pet_.owners);
Joins can be chained together to navigate to related entities of the target entity without having to create a Join instance for each join: 可以将联接链接在一起以导航到目标实体的相关实体,而不必为每个联接创建Join实例:
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class);

Root<Pet> pet = cq.from(Pet.class);
Join<Owner, Address> address = cq.join(Pet_.owners).join(Owner_.addresses);

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

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