记hibernate的一次报错 Cannot create TypedQuery for query with more than one return using requested result

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(<generated>)
    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(<generated>)
    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<StudyUserShowVO> 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的规范. 这不是此次话题的重点.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值