我遇到与this问题中描述的问题非常类似的问题。
A和B实体之间没有关系,我想避免添加一个。获得与下面的查询相同的结果的最佳方法是什么,但使用querydsl?我将对排序和分页结果进行排序,因此子查询不是最好的方法。
@Entity
class A {
String email;
}
@Entity
class B {
String email;
}
SELECT * FROM A a left join B b on a.emial=b.email;
答案 0 :(得分:1)
正如蒂莫所说,如果你愿意,可以使用Querydsl SQL
并做一些动态路径:
PathBuilder<Object> aObj = new PathBuilder<Object>(Object.class, "A");
Path<Object> aAlias = new PathBuilder<Object>(Object.class, "a");
PathBuilder<Object> bObj = new PathBuilder<Object>(Object.class, "B");
Path<Object> bAlias = new PathBuilder<Object>(Object.class, "b");
SQLTemplates templates = MySQLTemplates.builder().printSchema().build();
Configuration configuration = new Configuration(templates);
SQLQueryFactory sQLQueryFactory new SQLQueryFactory(configuration, null);
SQLQuery<?> sqlQuery = sQLQueryFactory.from(aObj.as("a"));
sqlQuery = sqlQuery.innerJoin(bObj, bAlias);
SimpleExpression<T> leftClause = Expressions.stringPath(aAlias, "email");
SimpleExpression<T> rightClause = Expressions.stringPath(bAlias, "email");
sqlQuery = sqlQuery.on(leftClause.eq(rightClause));
sqlQuery = sqlQuery.select(SQLExpressions.all);
// This will give you the required SQL
// select * from A a left join B b on a.emial = b.email;
System.out.println(sqlQuery.getSQL().getSQL());
答案 1 :(得分:0)
您需要在SQL中表达这一点。使用Querydsl SQL或Querydsl JPA本机查询。