0

The following query is throwing an exception. Loan class has a variable of type User so I'm passing the user object to the query.

String hqlQuery = "SELECT " +
                "l.outDate, l.dueDate, l.noOfRenewals, " +
                "c.authors, c.title, c.yearPublished, " +
                "f.amount " +
                "FROM Loan l " +
                "JOIN CatalogueEntry c " +
                "INNER JOIN Fine f " +
                "WHERE l.user=" + user + 
                " AND l=f.loan" +
                " AND l.catalogueEntry=c";

The exception thrown is:

org.hibernate.QueryException: unexpected char: '@' [SELECT l.outDate, l.dueDate, l.noOfRenewals, c.authors, c.title, c.yearPublished, f.amount FROM com.csu.library.mvc.dto.Loan l JOIN CatalogueEntry c INNER JOIN Fine f WHERE l.user=com.csu.library.mvc.dto.User@6f349665 AND l=f.loan AND l.catalogueEntry=c]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:223)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1735)
    at com.csu.library.mvc.dao.implementation.LoanDaoImpl.getLoansByUserForDisplay(LoanDaoImpl.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy54.getLoansByUserForDisplay(Unknown Source)
    at com.csu.library.mvc.service.impl.LoanServiceImpl.getCurrentLoansForDisplay(LoanServiceImpl.java:119)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy59.getCurrentLoansForDisplay(Unknown Source)
    at com.csu.library.mvc.controller.UserController.homepage(UserController.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:680)

Both loan and catalogueEntry are object variables and not just primitive variables.

4
  • you should write like: "WHERE l.user='" + user + "'" ,, notice in where condition Apr 26, 2013 at 4:54
  • @GrijeshChauhan After I added the changes, following error is thrown. org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [SELECT l.outDate, l.dueDate, l.noOfRenewals, c.authors, c.title, c.yearPublished, f.amount FROM com.csu.library.mvc.dto.Loan l JOIN CatalogueEntry c INNER JOIN Fine f WHERE l.user='com.csu.library.mvc.dto.User@3865db85' AND f.loan=l AND l.catalogueEntry=c]
    – h-rai
    Apr 26, 2013 at 5:01
  • sorry then No idea :( Apr 26, 2013 at 5:18
  • It is not an answer, but you better use query parameters instead of concatenation. It is more secure and you won't need to worry about whether to use quotation marks.
    – Vic
    Aug 19, 2013 at 13:56

1 Answer 1

2

You need to reframe HQL to something like this:-

"WHERE l.user.id=" + user.getId()

where id is the primary key of the User class.

The reason being giving user in the query, is appending its toString() representation in the actual query.

Update:- For the JOIN, you need to mention the ON clause too.

JOIN CatalogueEntry c
ON l.someField = c.someField

The same goes for the JOIN with Fine too. Make those changes, and see what happens!

6
  • Following error is thrown. org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [SELECT l.outDate, l.dueDate, l.noOfRenewals, c.authors, c.title, c.yearPublished, f.amount FROM com.csu.library.mvc.dto.Loan l JOIN CatalogueEntry c INNER JOIN Fine f WHERE l.user.userID=2 AND f.loan=l AND l.catalogueEntry=c]
    – h-rai
    Apr 26, 2013 at 5:10
  • okay.... but I have a list of fines in loan. so how am i supposed to map that in the query? l.fines.someField = f.someField?? I'm sure that is not right.
    – h-rai
    Apr 26, 2013 at 8:48
  • you needn't do that. It'll fetch multiple records.
    – Rahul
    Apr 26, 2013 at 9:37
  • can you suggest a particular way to match the records?
    – h-rai
    Apr 26, 2013 at 13:04
  • which record do you need to match, with what?
    – Rahul
    Apr 26, 2013 at 13:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.