Java JPA TypedQuery错误
我发现错误,无法使用请求的结果类型为具有多个返回的查询创建TypedQuery 对于下面使用Glassfish上的JPA进行的查询,有什么想法吗?我想获取具有特定借方状态的最新借方记录Java JPA TypedQuery错误,java,hibernate,jpa,Java,Hibernate,Jpa,我发现错误,无法使用请求的结果类型为具有多个返回的查询创建TypedQuery 对于下面使用Glassfish上的JPA进行的查询,有什么想法吗?我想获取具有特定借方状态的最新借方记录 entityManager.createQuery("select dd, MAX(dd.createdMillis) from T_DEBIT dd" + " where dd.debitStatus in (:debitStatus)" +
entityManager.createQuery("select dd, MAX(dd.createdMillis) from T_DEBIT dd" +
" where dd.debitStatus in (:debitStatus)" +
" and dd.account = :account" , Debit.class)
.setParameter("debitStatus", false)
.setParameter("account", account)
.getSingleResult();
通常为TypedQuery指定泛型参数。如果您声明了TypedQuery,您将使用对象[]作为TypedQuery的通用参数,因为您正在投影列而不是返回完整的实体 但是,由于您没有使用简洁的编码样式声明TypedQuery,因此需要将Debit.class更改为Object[].class,因为您没有选择对象,而只选择了两个字段
Object[] result = entityManager.createQuery("select dd, MAX(dd.createdMillis) from T_DEBIT dd" +
" where dd.debitStatus in (:debitStatus)" +
" and dd.account = :account" , Object[].class) //Notice change
.setParameter("debitStatus", false)
.setParameter("account", account)
.getSingleResult();
执行此查询将返回一个对象[],其中对象[]中的每个索引与select语句中的一个字段相对应。例如:
result[0] = dd
result[1] = max(dd.createdMillis)
为了避免使用对象[],可以创建一个新类,以更强类型的方式检索这些值。比如:
public class Result {
String dd;
Date createdMillis;
public Result(String dd, Date createdMillis) {
super();
this.dd = dd;
this.createdMillis = createdMillis;
}
public String getDd() {
return dd;
}
public void setDd(String dd) {
this.dd = dd;
}
public Date getCreatedMillis() {
return createdMillis;
}
public void setCreatedMillis(Date createdMillis) {
this.createdMillis = createdMillis;
}
}
然后在JPQL语句中,可以调用构造函数:
Result result = entityManager.createQuery("select NEW fully.qualified.Result(dd, MAX(dd.createdMillis)) from T_DEBIT dd" +
" where dd.debitStatus in (:debitStatus)" +
" and dd.account = :account" , Result.class)
.setParameter("debitStatus", false)
.setParameter("account", account)
.getSingleResult();
最近,我写了一篇关于这个话题的博客。我鼓励您观看我创建的视频教程:通常为TypedQuery指定一个通用参数。如果您声明了TypedQuery,您将使用对象[]作为TypedQuery的通用参数,因为您正在投影列而不是返回完整的实体 但是,由于您没有使用简洁的编码样式声明TypedQuery,因此需要将Debit.class更改为Object[].class,因为您没有选择对象,而只选择了两个字段
Object[] result = entityManager.createQuery("select dd, MAX(dd.createdMillis) from T_DEBIT dd" +
" where dd.debitStatus in (:debitStatus)" +
" and dd.account = :account" , Object[].class) //Notice change
.setParameter("debitStatus", false)
.setParameter("account", account)
.getSingleResult();
执行此查询将返回一个对象[],其中对象[]中的每个索引与select语句中的一个字段相对应。例如:
result[0] = dd
result[1] = max(dd.createdMillis)
为了避免使用对象[],可以创建一个新类,以更强类型的方式检索这些值。比如:
public class Result {
String dd;
Date createdMillis;
public Result(String dd, Date createdMillis) {
super();
this.dd = dd;
this.createdMillis = createdMillis;
}
public String getDd() {
return dd;
}
public void setDd(String dd) {
this.dd = dd;
}
public Date getCreatedMillis() {
return createdMillis;
}
public void setCreatedMillis(Date createdMillis) {
this.createdMillis = createdMillis;
}
}
然后在JPQL语句中,可以调用构造函数:
Result result = entityManager.createQuery("select NEW fully.qualified.Result(dd, MAX(dd.createdMillis)) from T_DEBIT dd" +
" where dd.debitStatus in (:debitStatus)" +
" and dd.account = :account" , Result.class)
.setParameter("debitStatus", false)
.setParameter("account", account)
.getSingleResult();
最近,我写了一篇关于这个话题的博客。我鼓励您观看我创建的视频教程:发布您的TypedQuery声明我希望我提供的答案有帮助,这里有点早,所以如果您对我的答案有疑问,请告诉我。基本上,您试图返回一个实体,但只选择了两列,这并不构成EntityList您的TypedQuery声明我希望我提供的答案有点早,所以如果您对我的答案有疑问,请告诉我。基本上,当我使用Object[]result=Entity时,您试图返回一个实体,但只选择了两列,这两列不构成entitytnx。。我得到一个错误,它不能convert@Spring是否将createQuery方法中指定的类从Debit.class更改为Object[].class?我在回答中的代码的这一点上添加了注释//注意更改。@Spring是否修复了异常?tnx已解决!一个小问题,它给我的是第一条记录,而不是最新的记录,wtf?我还注意到,对象[1]的长时间值与对象[0]实体的时间值不同,所以这里有些可疑?tnx,当我使用对象[]结果=实体时。。我得到一个错误,它不能convert@Spring是否将createQuery方法中指定的类从Debit.class更改为Object[].class?我在回答中的代码的这一点上添加了注释//注意更改。@Spring是否修复了异常?tnx已解决!一个小问题,它给我的是第一条记录,而不是最新的记录,wtf?我还注意到,对象[1]的长时间值与对象集[0]实体的时间值不同,所以这里有些可疑?