gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:14:33 25 4
gpt4 key购买 nike

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

我的对象:

@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 方法有效,因此与数据库的连接正常。

非常感谢。

堆栈跟踪:

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

最佳答案

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

因此给出的错误与我关注的领域无关:“registration_id”,而是与另一个领域相关。

感谢大家的回答。

关于javax.persistence.PersistenceException : org. hibernate.exception.GenericJDBCException:无法执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32943251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com