Generic querydsl orderBy使用左连接生成动态路径

时间:2013-11-29 13:43:30

标签: java jpa querydsl

我在使用带有Querydsl和Hibernate的JPA进行数据存储管理时遇到了问题。样本模型如下:

@Entity
public class User {
    ....

    @ManyToOne
    @JoinColumn(name = "CATEGORY_ID")
    private Category category;
}

@Entity
public class Category {
    ..
    private String acronym;

    @OneToMany(mappedBy = "category")    
    List<User> userList;    
}

在我的Spring MVC webapp中,我有一个包含User parameters和orderBy select的搜索表单。 orderBy select可以是User属性或Category属性。 orderBy参数存储为Map(fe {&#34; login&#34;:&#34; adm&#34;,{&#34; firstName&#34;:&#34; John&#34;}。函数接收搜索参数(作为字符串)和上面带有订单规范的地图。订购的简化代码如下:

Map<String, String> orderByMap = new HashMap<String, String>();
orderByMap.put("firstName", "asc");
orderByMap.put("unit.acronym", "desc");

PathBuilder<User> pbu = new PathBuilder<User>(User.class, "user");

....

for (Map.Entry<String, String> order : orderByMap.entrySet())
{
    // for simplicity I've omitted asc/desc chooser
    query.orderBy(pbu.getString(order.getKey()).asc());
}

当我想按类别参数引入排序时,问题就出现了,例如{&#34; category.acronym&#34;,&#34; desc&#34;}。正如here所解释的那样,上面的代码将使querydsl使用与Category表的交叉连接,并省略没有类别的用户,这不是预期的行为。

我知道,我必须使用Categories引入左连接并使用别名进行排序以使其正常工作,我正在寻找有效的动态方法。剥离每个字符串寻找类别或任何其他实体(例如&#34; user.category.subcategory.propetry&#34;)将引入许多丑陋的代码,我宁愿不这样做。

我非常感谢您提供更优雅的解决方案。

1 个答案:

答案 0 :(得分:2)

我现在将一个实现原型添加到Querydsl的测试端https://github.com/mysema/querydsl/issues/582

如果这是一个常见的用例,我会考虑直接集成到Querydsl中

public class OrderHelper {

private static final Pattern DOT = Pattern.compile("\\.");

public static PathBuilder<?> join(JPACommonQuery<?> query, PathBuilder<?> builder, Map<String, PathBuilder<?>> joins, String path) {
    PathBuilder<?> rv = joins.get(path);
    if (rv == null) {
        if (path.contains(".")) {
            String[] tokens = DOT.split(path);
            String[] parent = new String[tokens.length - 1];
            System.arraycopy(tokens, 0, parent, 0, tokens.length - 1);
            String parentKey = StringUtils.join(parent, ".");
            builder = join(query, builder, joins, parentKey);
            rv = new PathBuilder(Object.class, StringUtils.join(tokens, "_"));
            query.leftJoin((EntityPath)builder.get(tokens[tokens.length - 1]), rv);
        } else {
            rv = new PathBuilder(Object.class, path);
            query.leftJoin((EntityPath)builder.get(path), rv);
        }
        joins.put(path, rv);
    }
    return rv;
}

public static void orderBy(JPACommonQuery<?> query, EntityPath<?> entity, List<String> order) {
    PathBuilder<?> builder = new PathBuilder(entity.getType(), entity.getMetadata());
    Map<String, PathBuilder<?>> joins = Maps.newHashMap();

    for (String entry : order) {
        String[] tokens = DOT.split(entry);
        if (tokens.length > 1) {
            String[] parent = new String[tokens.length - 1];
            System.arraycopy(tokens, 0, parent, 0, tokens.length - 1);
            PathBuilder<?> parentAlias = join(query, builder, joins, StringUtils.join(parent, "."));
            query.orderBy(parentAlias.getString(tokens[tokens.length - 1]).asc());
        } else {
            query.orderBy(builder.getString(tokens[0]).asc());
        }
    }
}

}