我试图通过提供完全限定的名称来从db中选择特定的列到复合类DTO中。
@Data
public class Temp {
String dName;
Temp2 value;
public Temp( String dName, Temp2 value) {
this.dName = dName;
this.value = value;
}
@Data
public static class Temp2 {
Integer day;
public Temp2(Integer day) {
this.day = day;
}
}
}
查询:SELECT new com.pojo.Temp(t1.displayName,new com.pojo.Temp.Temp2(t3.day))FROM table1 t1 JOIN table2 t2 ON t1.bId = t2.id和左联接table3 t3在t1.g_id = t2.id
Error: `[2018-11-06 12:02:54] [main] ERROR o.h.hql.internal.ast.ErrorCounter.reportError - [ ] line 1:64: unexpected token: ,
[2018-11-06 12:02:54] [main] ERROR o.h.hql.internal.ast.ErrorCounter.reportError - [ ] line 1:64: unexpected token: ,
antlr.NoViableAltException: unexpected token: ,
at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:1009)
at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3549)
at org.hibernate.hql.internal.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3401)
at org.hibernate.hql.internal.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3273)
at org.hibernate.hql.internal.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2930)
at org.hibernate.hql.internal.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:615)
at org.hibernate.hql.internal.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2697)
at org.hibernate.hql.internal.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2558)
at org.hibernate.hql.internal.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2522)
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2438)
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2403)
at org.hibernate.hql.internal.antlr.HqlBaseParser.expression(HqlBaseParser.java:2116)
at org.hibernate.hql.internal.antlr.HqlBaseParser.aliasedExpression(HqlBaseParser.java:2357)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectedPropertiesList(HqlBaseParser.java:1390)
at org.hibernate.hql.internal.antlr.HqlBaseParser.newExpression(HqlBaseParser.java:1434)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectClause(HqlBaseParser.java:1306)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1040)
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:748)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:319)
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:198)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:186)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103)
`
我无法找到使用jpql表达式将数据放入复合类dto的任何相关答案。由于我是新手,所以我可能会缺少一些东西。
任何帮助将不胜感激。
答案 0 :(得分:1)
嵌套这样的构造函数表达式不是AFAIK。我知道有一种方法可以做到,但是这是一个肮脏而丑陋的工作。我将所有参数放在一个构造函数中,然后实例化该构造函数中的其他类对象。
示例:
public CommentDTO(Long id, String body, LocalDateTime datePosted,
LocalDateTime lastModifiedDate, Long userId,
String login, String avatarUrl, boolean hireable) {
this.id = id;
this.body = body;
this.datePosted = datePosted;
this.lastModifiedDate = lastModifiedDate;
this.author = new UserDTO(userId, login, avatarUrl, hireable);
因此JPQL查询必须具有所有这些参数。
同样,不建议这样做,因为它变得100%无法维护。但这是我发现使用构造函数表达式执行此操作的唯一方法。
我只是重构为使用基于接口的投影。如果您使用的是Spring Data JPA,则为嵌套投影设置它非常容易。