jpa 2条件hibernate 5.2嵌套连接

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jpa 2条件hibernate 5.2嵌套连接相关的知识,希望对你有一定的参考价值。

我正在尝试向我的crud服务添加指定我需要的嵌套关系的可能性,因此我不必从数据库中读取所有内容。

举个例子,我有这些实体

company.Java

private List<Department> departments;
private SalaryCode salaryCode;

department.Java

private List<Employee> employees;
private Company company;
private SalaryCode salaryCode;

employee.Java

private Department department;
private SalaryCode salaryCode

我现在的Criteria查询是这样的:

Session session = sessionFactory.openSession();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = builder.createQuery(clazz);
Root<T> root = criteriaQuery.from(clazz);

//nestedRelationships is a varargs passed as parameters
for(String nestedRelationship : nestedRelationships) {
    root.fetch(nestedRelationship, JoinType.LEFT);
}

List<T> result = session.createQuery(criteriaQuery.select(root)).list();

问题是如果我将“department”指定为nestedLlationship并查询Employee实体它运行良好但是当我尝试指定“department.salaryCode”时它不起作用“无法找到具有给定名称的属性”。当然我首先获取“部门”,然后获取“department.salaryCode”。

是否支持?如果是,它是如何工作的,如果不支持我该怎么办?

答案

是的,它受到支持。你需要使用联接。

    Root<Company> root = criteriaQuery.from(Company.class);
    Join<Company,Department> joinDepartment = root.join( Company_.departments );
    Join<Department,SalaryCode> joinSalaryCode = joinDepartment.join( Department_.salaryCode );

要生成元模型类(例如Department_),请查看here

另一答案

我通过使用Root元素制作算法找到了解决方案

protected void fetch(Root<T> root, String... joins) {
    //Sort the joins so they are like this :
    //A
    //A.F
    //B.E
    //B.E.D
    //B.G
    Arrays.sort(joins);

    Map<String, Fetch> flattenFetches = new HashMap<>();

    for (String join : joins) {
        try {
            if (join.contains(".")) {
                String[] subrelations = join.split("\.");
                Fetch lastRelation = null;
                int i;

                for (i = subrelations.length - 1; i >= 0; i--) {
                    String subJoin = String.join(".", Arrays.copyOf(subrelations, i));

                    if (flattenFetches.containsKey(subJoin)) {
                        lastRelation = flattenFetches.get(subJoin);
                        break;
                    }
                }

                if (lastRelation == null) {
                    lastRelation = root.fetch(subrelations[0], JoinType.LEFT);
                    flattenFetches.put(subrelations[0], lastRelation);
                    i = 1;
                }

                for (; i < subrelations.length; i++) {
                    String relation = subrelations[i];
                    String path = String.join(".", Arrays.copyOf(subrelations, i + 1));

                    if (i == subrelations.length - 1) {
                        Fetch fetch = lastRelation.fetch(relation, JoinType.LEFT);
                        flattenFetches.put(path, fetch);
                    } else {
                        lastRelation = lastRelation.fetch(relation, JoinType.LEFT);
                        flattenFetches.put(path, lastRelation);
                    }
                }
            } else {
                Fetch fetch = root.fetch(join, JoinType.LEFT);
                flattenFetches.put(join, fetch);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

并使用它我只需要做例如:

employeeController.getAll("punches", "currentSchedule.shifts", "defaultDepartment.currentSchedule.shifts",
            "defaultDepartment.company.currentSchedule.shifts", "bankExtras")

我想评论算法,但我没有时间,而且很容易理解

以上是关于jpa 2条件hibernate 5.2嵌套连接的主要内容,如果未能解决你的问题,请参考以下文章

JPA:传递给持久化的分离实体:嵌套异常是 org.hibernate.PersistentObjectException

Mybatis JPA

如何使用 JPA 和 Hibernate 连接两个不相关的实体

标准 JPA 2,Hibernate - 多重连接

JPA 2.0 (Hibernate) 使用 @JoinTable 为 @OneToMany 生成不正确的连接表 PK

使用具有一对多关系的 JPA 映射实体,如何添加连接条件