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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32874384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:14:35  来源:igfitidea点击:

Spring Data JPA Specification using CriteriaBuilder with a one to many relationship

javaspringhibernatejpaspring-data-jpa

提问by Andrew Mairose

I have a Userentity, a UserToApplicationentity, and an Applicationentity.

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

A single Usercan have access to more than one Application. And a single Applicationcan be used by more than one User.

一个人User可以访问多个Application。而且一个人Application可以多人使用User

Here is the Userentity.

这是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 */
}

Here is the UserToApplicationentity.

这是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;
    }
}

And here is the Applicationentity.

这是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 */
}

I have the following Specificationthat I use to search for a Userby firstNm, lastNm, and email.

我有以下的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);
            }
        };
    }

}

And here is the User_metamodel that I have so far.

这是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;
}

Now, I would like to also pass in a list of application IDs to the Specification, such that its method signature would be:

现在,我还想将应用程序 ID 列表传递给Specification,使其方法签名为:

public static Specification<User> findByFirstNmLastNmEmailApp(String firstNm, String lastNm, String email, Collection<Long> appIds)

So, my question is, if I add the @OneToManymapping to the User_metamodel for the Collection<Application> applicationsfield of my Userentity, then how would I reference it in the Specification?

所以,我的问题是,如果我将@OneToMany映射添加到实体字段的User_元模型中,那么我将如何在?Collection<Application> applicationsUserSpecification

My current Specificationwould be similar to the following SQL query:

我的当前Specification将类似于以下 SQL 查询:

select * from user u
where lower(first_nm) like '%firstNm%'
and lower(last_nm) like '%lastNm%'
and lower(email) like '%email%';

And what I would like to achieve in the new Specificationwould be something like this:

我想在新版本中实现的目标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);

Is it possible to do this kind of mapping in the metamodel, and how could I achieve this result in my Specification?

是否可以在元模型中进行这种映射,我如何在我的Specification?

采纳答案by Andrew Mairose

I found a solution. To map a one to many attribute, in the metamodel I added the following:

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

public static volatile CollectionAttribute<User, Application> applications;

I also needed to add a metamodel for the Applicationentity.

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

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

Then in my Specification, I could access the applicationsfor a user, using the .join()method on the Root<User>instance. Here is the PredicateI formed.

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

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

Also, it is worth noting that my Specificationas it is written in the question will not work if any of the input values are empty. A null Predicatepassed to the .and()method of CriteriaBuilderwill cause a NullPointerException. So, I created an ArrayListof type Predicate, then added each Predicateto the list if the corresponding parameter was non-empty. Finally, I convert the ArrayListto an array to pass it to the .and()function of the CriteriaBuilder. Here is the final Specification:

另外,值得注意的是,Specification如果任何输入值为空,则问题中所写的my将不起作用。Predicate传递给 的.and()方法的nullCriteriaBuilder将导致NullPointerException. 因此,我创建了一个ArrayListof type Predicate,然后Predicate如果相应的参数非空,则将每个都添加到列表中。最后,我转换ArrayList到一个数组将它传递到.and()的功能CriteriaBuilder。这是决赛Specification

public class UserSpecification {

    public static Specification<User> findByFirstNmLastNmEmailApp(String firstNm, String lastNm, String email, Collection<Long> appIds) {
        return new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                final Collection<Predicate> predicates = new ArrayList<>();
                if (!StringUtils.isEmpty(firstNm)) {
                    final Predicate firstNmPredicate = cb.like(cb.lower(root.get(User_.firstNm), firstNm));
                    predicates.add(firstNmPredicate);
                }
                if (!StringUtils.isEmpty(lastNm)) {
                    final Predicate lastNmPredicate = cb.like(cb.lower(root.get(User_.lastNm), lastNm));
                    predicates.add(lastNmPredicate);
                }
                if (!StringUtils.isEmpty(email)) {
                    final Predicate emailPredicate = cb.like(cb.lower(root.get(User_.email), email));
                    predicates.add(emailPredicate);
                }
                if (!appIds.isEmpty()) {
                    final Predicate appPredicate = root.join(User_.applications).get(Application_.applicationId).in(appIds);
                    predicates.add(appPredicate);
                }

                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
    }

}