正如蒂莫說,使用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());