使用CriteriaBuilder的Spring Data JPA规范具有一对多的关系

时间:2015-09-30 19:45:14

标签: java spring hibernate jpa spring-data-jpa

我有一个User实体,一个UserToApplication实体和一个Application实体。

单个User可以访问多个Application。一个Application可以由多个User使用。

这是User实体。

@Entity
@Table(name = "USER", schema = "UDB")
public class User {
    private Long userId;
    private Collection<Application> applications;
    private String firstNm;
    private String lastNm;
    private String email;

    @SequenceGenerator(name = "generator", sequenceName = "UDB.USER_SEQ", initialValue = 1, allocationSize = 1)
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
    @Column(name = "USER_ID", unique = true, nullable = false)
    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
    public Collection<Application> getApplications() {
        return applications;
    }

    public void setApplications(Collection<Application> applications) {
        this.applications = applications;
    }

    /* Other getters and setters omitted for brevity */
}

这是UserToApplication实体。

@Entity
@Table(name = "USER_TO_APPLICATION", schema = "UDB")
public class Application {
    private Long userToApplicationId;
    private User user;
    private Application application;

    @SequenceGenerator(name = "generator", sequenceName = "UDB.USER_TO_APP_SEQ", initialValue = 0, allocationSize = 1)
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
    @Column(name = "USER_TO_APPLICATION_ID", unique = true, nullable = false)
    public Long getUserToApplicationId() {
        return userToApplicationId;
    }

    public void setUserToApplicationId(Long userToApplicationId) {
        this.userToApplicationId = userToApplicationId;
    }

    @ManyToOne
    @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID", nullable = false)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @ManyToOne
    @JoinColumn(name = "APPLICATION_ID", nullable = false)
    public Application getApplication() {
        return application;
    }
}

这是Application实体。

@Entity
@Table(name = "APPLICATION", schema = "UDB")
public class Application {
    private Long applicationId;
    private String name;
    private String code;

    /* Getters and setters omitted for brevity */
}

我使用以下Specification UserfirstNmlastNmemail搜索public class UserSpecification { public static Specification<User> findByFirstNmLastNmEmail(String firstNm, String lastNm, String email) { return new Specification<User>() { @Override public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) { final Predicate firstNmPredicate = null; final Predicate lastNmPredicate = null; final Predicate emailPredicate = null; if (!StringUtils.isEmpty(firstNm)) { firstNmPredicate = cb.like(cb.lower(root.get(User_.firstNm), firstNm)); } if (!StringUtils.isEmpty(lastNm)) { lastNmPredicate = cb.like(cb.lower(root.get(User_.lastNm), lastNm)); } if (!StringUtils.isEmpty(email)) { emailPredicate = cb.like(cb.lower(root.get(User_.email), email)); } return cb.and(firstNmPredicate, lastNmPredicate, emailPredicate); } }; } }

User_

这是我到目前为止的@StaticMetamodel(User.class) public class User_ { public static volatile SingularAttribute<User, String> firstNm; public static volatile SingularAttribute<User, String> lastNm; public static volatile SingularAttribute<User, String> email; } 元模型。

Specification

现在,我还想将一个应用程序ID列表传递给public static Specification<User> findByFirstNmLastNmEmailApp(String firstNm, String lastNm, String email, Collection<Long> appIds) ,这样它的方法签名就是:

@OneToMany

所以,我的问题是,我可以将User_映射添加到Collection<Application> applications实体的User字段的Specification元模型,然后我将如何引用它在Specification

我当前的select * from user u where lower(first_nm) like '%firstNm%' and lower(last_nm) like '%lastNm%' and lower(email) like '%email%'; 与以下SQL查询类似:

Specification

我希望在新select * from user u join user_to_application uta on uta.user_id = u.user_id where lower(u.first_nm) like '%firstNm%' and lower(u.last_nm) like '%lastNm%' and lower(u.email) like '%email%' and uta.application_id in (appIds); 中实现的目标是:

Specification

是否可以在元模型中执行此类映射,如何在{{1}}中实现此结果?

1 个答案:

答案 0 :(得分:15)

我找到了解决方案。要映射一个到多个属性,在元模型中我添加了以下内容:

public static volatile CollectionAttribute<User, Application> applications;

我还需要为Application实体添加元模型。

@StaticMetamodel(Application.class)
public class Application_ {
    public static volatile SingularAttribute<Application, Long> applicationId;
}

然后在我的Specification中,我可以使用applications实例上的.join()方法为用户访问Root<User>。这是我成立的Predicate

final Predicate appPredicate = root.join(User_.applications).get(Application_.applicationId).in(appIds);

另外,值得注意的是,如果任何输入值为空,我在问题中写的Specification将不起作用。传递给Predicate .and()方法的空CriteriaBuilder将导致NullPointerException。因此,我创建了ArrayList类型Predicate,然后如果相应参数非空,则将每个Predicate添加到列表中。最后,我将ArrayList转换为数组,以将其传递给.and()的{​​{1}}函数。这是最后的CriteriaBuilder

Specification