2

Hi I have researched this in these forums thoroughly and many people have posted stuff but none of that helps me.

my object:

@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;
}

my repository implementation:

@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;
}

my 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"/>

I have tried named queries and even one without parameters as a list and no luck, I wonder if there is any special configuration I need to do.

The entitymanager.find method works, so the connection to the db is fine.

Thanks a lot.

stacktrace:

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
  • There is some problem with the variable type, can you try search by address-id instead of registration-id. Lemme know. Commented Oct 5, 2015 at 7:30
  • Hi Borg, the value I have is the registration id, unfortunately, but is a Long, so maybe you have a point, not sure how to deal with it though. Commented Oct 6, 2015 at 5:07
  • Also please remove the jdbc dataSource url from the configuration you've shared, that should be considered sensitive information ;) Commented Oct 7, 2015 at 8:01

3 Answers 3

3

I have found what the problem was, in my entity I had another field called "student_id" that was defined in the entity as int but in the database is a varchar.

So the errors given where not related to the field I was focusing on: "registration_id" but to another one.

Thanks everyone for your answers.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe it's just a matter of improper casing :)

Named Parameters in Queries

Named parameters are case-sensitive and may be used by both dynamic and static queries.

You can easily test it out by using this API instead javax.persistence.Query#setParameter(int, java.lang.Object) like so:

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

11 Comments

I have tried this, thanks, but I get the same problem, I was reading the exception though and maybe it is related to the fact that I am using a parameter defined in my entity as Long? I will post the section of the stack trace above so you see what I mean. If I were to try a different approach than named queries, what would you recommend?
Can you add a break point on the if clause of this method org.hibernate.loader.hql.QueryLoader#checkQuery, it's org.hibernate.loader.hql.QueryLoader:500? See which one of the two conditions triggers the org.hibernate.QueryException?
Maybe you should use javax.persistence.EntityManager#createNamedQuery(java.lang.String, java.lang.Class<T>) and update your code to also instruct it about the type of the query result you're expecting like this TypedQuery<Address> query = entityManager.createNamedQuery("find address by registration_id", Address.class);. Since javax.persistence.TypedQuery interface extends the one you're already programming your query against javax.persistence.Query the rest of the code should compile...
Hi Filip, the sql is already visible at the start of the stacktrace, I have grabbed that sql statement and ran it in my sqldeveloper and it works fine, remember I have also manage to get working other db retrieval using entitymanager.find method, so the connection to the database is fine. I believe the problem is some configuration in hibernate that does not allow me to run namedqueries, maybe you can point me to a mistake in my persistence.xml?
Hi Filip, no it was that I had another field called "student_id" defined as int in my entity but in the database it was a varchar!
|
0

try naming your query like this:

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

2 Comments

I tried this, but no luck, it is a good advice though, it looks better that way, thanks.
How are you giving the registration_id, are you using long? and just for clarification, you have getters/setters in your entity class right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.