javax.persistence.PersistenceException:org.hibernate.exception.GenericJDBCExcep tion:无法执行查询

时间:2015-10-05 07:25:19

标签: java spring hibernate jpa

您好我已经在这些论坛中对此进行了彻底的研究,很多人都发布了这些内容,但这些都没有帮助我。

我的对象:

@Entity
@Table(name = "ADDRESS")
@NamedQuery(name = "Address.findByRegistrationId", query = "SELECT a FROM Address a WHERE a.registration_id = :registration_id")
public class Address {

@Id
@Column(name = "ADDRESS_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private long address_id;

@Column(name = "REGISTRATION_ID")
private long registration_id;

public long getRegistration_id() {
    return registration_id;
}

public void setRegistration_id(long registration_id) {
    this.registration_id = registration_id;
}

我的存储库实现:

@Repository
public class AddressRepositoryJpa implements AddressRepository {
private final Logger log = Logger.getLogger(AddressRepositoryJpa.class.getName());

@PersistenceContext
private EntityManager entityManager;

@Override
public Address findByID(int id) {
    return entityManager.find(Address.class, id);
}

@Override
public Address findByRegistrationID(Long registration_id) {
    TypedQuery<Address> query = (TypedQuery<Address>) entityManager.createNamedQuery("Address.findByRegistrationId", Address.class);
    query.setParameter("registration_id", registration_id);
    Address address = (Address) query.getSingleResult();
    return address;
}

我的persistence.xml

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="username" value="xxxx"/>
    <property name="password" value="xxxxx"/>
    <property name="url" value="jdbc:oracle:thin:@//xxxxx:1521/XXX.XXX.XXX.XX.XX"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="jpaProperties">
        <map><!--validate | update | create | create-drop-->
            <entry key="hibernate.hbm2ddl.auto" value="update"/>
            <entry key="hibernate.show_sql" value="true"/>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        </map>
    </property>
    <property name="packagesToScan" value="edu.rmit.core.entities"/>
</bean>

<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>

我已尝试过命名查询,甚至没有参数作为列表也没有运气,我想知道我是否需要做任何特殊配置。

entitymanager.find方法有效,因此与db的连接很好。

非常感谢。

堆栈跟踪:

Hibernate: select address0_.ADDRESS_ID as ADDRESS_ID1_0_, address0_.ADDRESS_1 as
 ADDRESS_2_0_, address0_.ADDRESS_2 as ADDRESS_3_0_, address0_.ADDRESS_3 as ADDRE
SS_4_0_, address0_.ADDRESS_TYPE as ADDRESS_TYPE5_0_, address0_.CITY as CITY6_0_,
 address0_.COUNTRY as COUNTRY7_0_, address0_.CREATED_BY as CREATED_BY8_0_, addre
ss0_.CREATED_ON as CREATED_ON9_0_, address0_.MODIFIED_BY as MODIFIED_BY10_0_, ad
dress0_.MODIFIED_ON as MODIFIED_ON11_0_, address0_.POSTCODE as POSTCODE12_0_, ad
dress0_.REGISTRATION_ID as REGISTRATION_ID13_0_, address0_.STATE as STATE14_0_,
address0_.STUDENT_ID as STUDENT_ID15_0_ from ADDRESS address0_ where address0_.R
EGISTRATION_ID=?
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCExcep
tion: could not execute query
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntit
yManagerImpl.java:1763)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntit
yManagerImpl.java:1677)
        at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:5
24)
        at edu.rmit.core.repositories.jpa.AddressRepositoryJpa.findByRegistratio
nID(AddressRepositoryJpa.java:36)
        at edu.rmit.core.services.impl.AddressServiceImpl.findByRegistrationID(A
ddressServiceImpl.java:28)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflecti
on(AopUtils.java:317)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJo
inpoint(ReflectiveMethodInvocation.java:190)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:157)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.
proceedWithInvocation(TransactionInterceptor.java:98)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.
invokeWithinTransaction(TransactionAspectSupport.java:262)
        at org.springframework.transaction.interceptor.TransactionInterceptor.in
voke(TransactionInterceptor.java:95)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynami
cAopProxy.java:207)
        at com.sun.proxy.$Proxy1621.findByRegistrationID(Unknown Source)
        at edu.rmit.rest.mvc.OrderSummaryController.getOrderSummaryMapByID(Order
SummaryController.java:79)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.springframework.web.method.support.InvocableHandlerMethod.invoke(
InvocableHandlerMethod.java:215)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeF
orRequest(InvocableHandlerMethod.java:132)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocabl
eHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingH
andlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingH
andlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapt
er.handle(AbstractHandlerMethodAdapter.java:83)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(Dispatch
erServlet.java:938)
        at org.springframework.web.servlet.DispatcherServlet.doService(Dispatche
rServlet.java:870)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(Frame
workServlet.java:961)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServl
et.java:852)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkSer
vlet.java:837)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52
)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authentica
torBase.java:505)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:103)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:
956)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:423)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp
11Processor.java:1079)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
AbstractProtocol.java:625)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpo
int.java:2522)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoin
t.java:2511)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskTh
read.java:61)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.exception.GenericJDBCException: could not execute query

        at org.hibernate.exception.internal.StandardSQLExceptionConverter.conver
t(StandardSQLExceptionConverter.java:54)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlException
Helper.java:126)
        at org.hibernate.loader.Loader.doList(Loader.java:2556)
        at org.hibernate.loader.Loader.doList(Loader.java:2539)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
        at org.hibernate.loader.Loader.list(Loader.java:2364)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslat
orImpl.java:387)
        at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.
java:231)
        at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
        at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
        at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
        at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:4
95)
        ... 54 more
Caused by: java.sql.SQLException: Fail to convert to internal representation
        at oracle.jdbc.driver.CharCommonAccessor.getInt(CharCommonAccessor.java:
147)
        at oracle.jdbc.driver.T4CVarcharAccessor.getInt(T4CVarcharAccessor.java:
818)
        at oracle.jdbc.driver.OracleResultSetImpl.getInt(OracleResultSetImpl.jav
a:928)
        at oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:434)
        at org.apache.commons.dbcp2.DelegatingResultSet.getInt(DelegatingResultS
et.java:283)
        at org.apache.commons.dbcp2.DelegatingResultSet.getInt(DelegatingResultS
et.java:283)
        at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(I
ntegerTypeDescriptor.java:74)
        at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtract
or.java:64)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStan
dardBasicType.java:267)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStan
dardBasicType.java:263)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStan
dardBasicType.java:253)
        at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandard
BasicType.java:338)
        at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(Abstra
ctEntityPersister.java:2969)
        at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1695)
        at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1627)
        at org.hibernate.loader.Loader.getRow(Loader.java:1514)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:725)
        at org.hibernate.loader.Loader.processResultSet(Loader.java:952)
        at org.hibernate.loader.Loader.doQuery(Loader.java:920)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Lo
ader.java:354)
        at org.hibernate.loader.Loader.doList(Loader.java:2553)
        ... 64 more

3 个答案:

答案 0 :(得分:2)

我发现问题是什么,在我的实体中我有另一个名为“student_id”的字段,该字段在实体中定义为int但在数据库中是varchar。

所以给出的错误与我所关注的领域无关:“registration_id”,而不是另一个。

感谢大家的回答。

答案 1 :(得分:0)

也许这只是一个不正确的套管问题:)

Named Parameters in Queries

  

命名参数区分大小写,可供动态和静态查询使用。

您可以使用此API轻松测试它 javax.persistence.Query#setParameter(int, java.lang.Object)喜欢这样:

Query query = entityManager.createNamedQuery("find address by registration_id");
query.setParameter(1, registration_id);
Address address = (Address) query.getSingleResult();
return address;

答案 2 :(得分:0)

尝试命名您的查询:

@NamedQuery(name = "Address.findByRegistrationId")...