Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关联查询时,每使用关联表的一个条件,就会多一个join #40

Open
hankuikuide opened this issue Aug 13, 2019 · 4 comments

Comments

@hankuikuide
Copy link

比如下面的例子
Specification specification = Specifications.and()
.between("age", 10, 35)
.eq("addresses.street", "Chengdu")
.eq("addresses.city", "china")
.build();
用到了addresses的两个条件:street,city,在生成Sql时addresses关联了两次:
如:select * from person left join addresses on x=x left join addresses on x=x ******
这是什么问题呢

@wenhao
Copy link
Owner

wenhao commented Aug 13, 2019

有空就看

@SoberFu
Copy link

SoberFu commented Oct 22, 2019

比如下面的例子
Specification specification = Specifications.and()
.between("age", 10, 35)
.eq("addresses.street", "Chengdu")
.eq("addresses.city", "china")
.build();
用到了addresses的两个条件:street,city,在生成Sql时addresses关联了两次:
如:select * from person left join addresses on x=x left join addresses on x=x ******
这是什么问题呢

这是因为,将addresses加到join中时没有过滤重复导致的,把AbstractSpecification类按下面这么改就可以了

abstract class AbstractSpecification<T> implements Specification<T>, Serializable {
    public String getProperty(String property) {
        if (property.contains(".")) {
            return StringUtils.split(property, ".")[1];
        }
        return property;
    }

    public From getRoot(String property, Root<T> root) {
        if (property.contains(".")) {
            String joinProperty = StringUtils.split(property, ".")[0];
            Set<Join<T, ?>> joins = root.getJoins();
            for (Join<T, ?> join : joins) {
                if(join.getAttribute().getName().equals(joinProperty)) {
                    return root;
                }
            }
            return root.join(joinProperty, JoinType.LEFT);
        }
        return root;
    }
}

@Mr-Chen-RM
Copy link

比如下面的例子
Specification specification = Specifications.and()
.between("age", 10, 35)
.eq("addresses.street", "Chengdu")
.eq("addresses.city", "china")
.build();
用到了addresses的两个条件:street,city,在生成Sql时addresses关联了两次:
如:select * from person left join addresses on x=x left join addresses on x=x ******
这是什么问题呢

这是因为,将addresses加到join中时没有过滤重复导致的,把AbstractSpecification类按下面这么改就可以了

abstract class AbstractSpecification<T> implements Specification<T>, Serializable {
    public String getProperty(String property) {
        if (property.contains(".")) {
            return StringUtils.split(property, ".")[1];
        }
        return property;
    }

    public From getRoot(String property, Root<T> root) {
        if (property.contains(".")) {
            String joinProperty = StringUtils.split(property, ".")[0];
            Set<Join<T, ?>> joins = root.getJoins();
            for (Join<T, ?> join : joins) {
                if(join.getAttribute().getName().equals(joinProperty)) {
                    return root;
                }
            }
            return root.join(joinProperty, JoinType.LEFT);
        }
        return root;
    }
}

老哥,这么做这个条件直接没了啊

@wenhao
Copy link
Owner

wenhao commented Feb 16, 2020

好的,我看看

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants