1

I have an action that should select a row from a table using the id:

@RequestMapping("/test/{userId}")
@ResponseBody
public void testUserSettings(@DestinationVariable Integer userId){
    System.out.println("test");
    Session session = sessionFactory.openSession();
    Query query = session.createQuery("select us.settings from UserSettings us"
            + " where us.userId = :userId");
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        query.setParameter("userId", userId);
        query.executeUpdate();
        transaction.commit();
    } catch (HibernateException e) {
        if (transaction != null)
            transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
}

It invokes the method because "test" is printed. The problem is in the statement. For some reason I get the following output in the console:

org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for select queries [select us.settings from com.musala.ving.entities.UserSettings us where us.userId = :userId]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.errorIfSelect(QueryTranslatorImpl.java:324)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:444)
at org.hibernate.engine.query.spi.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:379)
at org.hibernate.internal.SessionImpl.executeUpdate(SessionImpl.java:1286)
at org.hibernate.internal.QueryImpl.executeUpdate(QueryImpl.java:118)
at com.musala.ving.controllers.UserSettingsController.testUserSettings(UserSettingsController.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
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:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
1
  • Show us your UserSettings entity. Commented Feb 27, 2015 at 11:47

4 Answers 4

10

You are calling query.executeUpdate(); for a select query. Try using query.list(), or query.uniqueResult() instead.

query.uniqueResult() should be used if you expect a single result.

Javadoc excerpt for Query#list():

Return the query results as a List. If the query contains multiple results pre row, the results are returned in an instance of Object[].

Javadoc excerpt for Query#uniqueResult():

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Also, transaction.commit(); should not be necessary since this query doesn't update the database.

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

Comments

0
@RequestMapping("/test/{userId}")
@ResponseBody
public void testUserSettings(@DestinationVariable Integer userId){
    System.out.println("test");
    Session session = sessionFactory.openSession();
    Query query = session.createQuery("select us.settings from UserSettings us"
            + " where us.userId = :userId");
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        query.setParameter("userId", userId);
        query.list(); 
        transaction.commit();
    } catch (HibernateException e) {
        if (transaction != null)
            transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
}

Comments

0

I am using hibernate with hql (in conjunction with spring mvc, if that matters) and fixed the issue by doing the following:

String hql = "select clientId from Cart where cartId = :val" ;
        Query query = getSession().createQuery(hql);
        query.setParameter("val", cartId);
        //int result = query.executeUpdate(); OLD VERSION. REPLACED WITH THE LINE BELOW:
        int result = query.getFirstResult();
        return result;

So for me getFirstResult() solved the problem.

Comments

-2

query.executeUpdate();

you do a update of an select query !? How the error message says... "Not supported for select queries" use query.execute(); or what the correct method is...

Comments

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.