Hibernate映射实体到函数结果

时间:2016-02-24 17:28:16

标签: java oracle spring-data jpa-2.1 hibernate-5.x

我需要将我的实体映射到oracle中的函数结果。先决条件:

@Entity
@NamedNativeQuery(name="myFunc" query="{?=call MY_FUNC(:param1)}", 
   resultClass=Test.class, hints={
       @QueryHint(name="org.hibernate.callable", value=true)})
public class Test {
   @Id @GeneratedValue @Column(name="TEST_ID")
   private Long id;

   @OneToOne @JoinColumn(name="TEST_ROLE_ID")
   private TestRole role;   
}

查询:

(List<Test>)entityManager.createNamedQuery("myFunc").setParameter("param1", param1).getResultList();

此外,我还有一个函数,它在Test表(Oracle数据库)上返回REF_CURSOR。

但是当我想添加列

@OneToOne
@JoinTable(name="V_TEST_REF", joinColumns=@JoinColumn(name="TEST_ID"),
   inverseJoinColumns=@JoinColumn(name="REF_ID"))
private TestRef ref;

然后我收到了错误

    Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
                at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.doList(Loader.java:2614) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.doList(Loader.java:2594) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.list(Loader.java:2418) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:336) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1894) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:318) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:125) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:567) ~[hibernate-entitymanager-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:436) ~[hibernate-entitymanager-5.0.0.Final.jar:5.0.0.Final]
                at com.db.rnem.dashboard.persistence.repository.test.impl.TestRepositoryImpl.findAll(TestRepositoryImpl.java:49) ~[rnem-dashboard-persistence-1.0-SNAPSHOT.jar:?]
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_20]
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_20]
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_20]
                at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_20]
                at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE]
                at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE]
                at com.sun.proxy.$Proxy103.findAll(Unknown Source) ~[?:?]
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_20]
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_20]
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_20]
                at java.lang.reflect.Method.invoke(Method.java:483) ~[?:1.8.0_20]
                at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:434) ~[spring-data-commons-1.10.2.RELEASE.jar:?]
                at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:409) ~[spring-data-commons-1.10.2.RELEASE.jar:?]
                at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:391) ~[spring-data-commons-1.10.2.RELEASE.jar:?]
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE]
                at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.10.2.RELEASE.jar:?]
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE]
                at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) ~[spring-tx-4.0.9.RELEASE.jar:4.0.9.RELEASE]
                at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) ~[spring-tx-4.0.9.RELEASE.jar:4.0.9.RELEASE]
                at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) ~[spring-tx-4.0.9.RELEASE.jar:4.0.9.RELEASE]
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.2.RELEASE.jar:4.2.2.RELEASE]
                at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.0.9.RELEASE.jar:4.0.9.RELEASE]
                ... 113 more
Caused by: java.sql.SQLException: Invalid column name
                at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3724) ~[ojdbc6.jar:11.2.0.4.0]
                at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2799) ~[ojdbc6.jar:11.2.0.4.0]
                at oracle.jdbc.driver.OracleResultSet.getLong(OracleResultSet.java:444) ~[ojdbc6-12.1.0.1.jar:11.2.0.4.0]
                at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:56) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:241) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:237) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:227) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:185) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2665) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1728) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1654) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.getRow(Loader.java:1543) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.processResultSet(Loader.java:972) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.doQuery(Loader.java:930) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.doList(Loader.java:2611) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
                at org.hibernate.loader.Loader.doList(Loader.java:2594) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
            at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
            at org.hibernate.loader.Loader.list(Loader.java:2418) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
            at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:336) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]
            at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1894) ~[hibernate-core-5.0.0.Final.jar:5.0.0.Final]

我该如何解决这个问题?将列添加到函数结果中还是有其他方法吗?

0 个答案:

没有答案