hql在做集联查询的时候通常会遇到根据两个Bean的字段生成一个新的Bean, 但是在使用中却发现这种方式存在一定的问题,导致出现了如下的BUG:
org.springframework.dao.InvalidDataAccessApiUsageException: Cannot create TypedQuery for query with more than one return using requested result type [com.variflight.air.platform.vo.studyCourse.StudyUserShowVO]; nested exception is java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [com.variflight.air.platform.vo.studyCourse.StudyUserShowVO]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:503)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:209)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at com.variflight.air.platform.dao.extend.impl.StudyUserExtDaoImpl$$EnhancerBySpringCGLIB$$e2d41a64.joinStudyCourseAndUserFindByUserId()
at com.variflight.air.platform.dao.extend.impl.StudyUserExtDaoImplTest.joinStudyCourseAndUserFindByUserId(StudyUserExtDaoImplTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [com.variflight.air.platform.vo.studyCourse.StudyUserShowVO]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.resultClassChecking(AbstractEntityManagerImpl.java:361)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:307)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)
at com.sun.proxy.$Proxy112.createQuery(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
at com.sun.proxy.$Proxy112.createQuery(Unknown Source)
at com.variflight.air.platform.dao.extend.impl.StudyUserExtDaoImpl.joinStudyCourseAndUserFindByUserId(StudyUserExtDaoImpl.java:40)
at com.variflight.air.platform.dao.extend.impl.StudyUserExtDaoImpl$$FastClassBySpringCGLIB$$bf319d67.invoke()
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 32 more
以下是导致这次BUG的Bean:
public class StudyUserShowVO extends StudyUser {
private String studyCourseName;
private Date studyCourseStartDate;
private Date studyCourseLateDate;
private String studyCourseState;
private String userNickName;
private String userWorkNumber;
// 忽略getter/setter
}
下面是hql源码:
StringBuffer hql = new StringBuffer("select su.id, su.studyCourseId, su.userId, su.studyTime, sc.name as studyCourseName, sc.startDate as studyCourseStartDate," +
" sc.latestDate as studyCourseLateDate, sc.state as studyCourseState," +
" u.nickName as userNickName, u.workNumber as userWorkNumber" +
" from StudyUser su, StudyCourse sc, User u where su.userId = u.id and su.studyCourseId = sc.id");
if (userId != null) {
hql.append(" and u.id = :userId");
}
Query query = em.createQuery(hql.toString(), StudyUserShowVO.class);
if (userId != null) {
query.setParameter("userId", userId);
}
List list = query.getResultList();
错误原因很简单, Hibernate无法根据你给的字段自动映射到对应的Bean上(也就是StudyUserShowVO),
所以你需要手动告诉Hibernate去实现映射关系,方法如下:
StringBuffer hql = new StringBuffer("select NEW com.variflight.air.platform.vo.studyCourse.StudyUserShowVO(su.id, su.studyCourseId, su.userId, su.studyTime, sc.name as studyCourseName, sc.startDate as studyCourseStartDate, " +
" sc.latestDate as studyCourseLateDate, sc.state as studyCourseState," +
" u.nickName as userNickName, u.workNumber as userWorkNumber)" +
" from StudyUser su, StudyCourse sc, User u where su.userId = u.id and su.studyCourseId = sc.id");
public class StudyUserShowVO extends StudyUser {
private String studyCourseName;
private Date studyCourseStartDate;
private Date studyCourseLateDate;
private String studyCourseState;
private String userNickName;
private String userWorkNumber;
public StudyUserShowVO() {
super();
}
public StudyUserShowVO(Long id, Long studyCourseId, Long userId, Long studyTime, String studyCourseName, Date studyCourseStartDate, Date studyCourseLateDate, String studyCourseState, String userNickName, String userWorkNumber) {
super(id, studyCourseId, userId, studyTime);
this.studyCourseName = studyCourseName;
this.studyCourseStartDate = studyCourseStartDate;
this.studyCourseLateDate = studyCourseLateDate;
this.studyCourseState = studyCourseState;
this.userNickName = userNickName;
this.userWorkNumber = userWorkNumber;
}
}
因为我的Bean之间没有用@OneToMany这样的注解去维护一对一的关系,所以hql可能不是很符合JPA的规范. 这不是此次话题的重点.