0


my Application is gwt and hibernate below is my code

 public boolean changeUserStatus(long id, String status){
    try {
        Session session = HibernateFactory.openSession();
        Transaction transaction = session.beginTransaction();
        User user = (User ) session.get(User.class, id);
        user.setStatus(status);
        session.saveOrUpdate(user);
        transaction.commit(); 
        HibernateFactory.close(session);
    } catch (Exception e) {
        throw e;
    }
 }

staktrace

class org.hibernate.TransactionException
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)
sun.reflect.GeneratedMethodAccessor1829.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263)
com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178)
com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62)
com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118)
com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:489)
org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
java.lang.Thread.run(Thread.java:744)


see full stacktrace When i try to commit code it will give transaction Exception.
can anybody tell me what is wrong in my code?

Sejal Rudani
  • 372
  • 4
  • 19

1 Answers1

0

try session.merge();

saveOrUpdate() does the following:

if the object is already persistent in this session, do nothing if another object associated with the session has the same identifier, throw an exception if the object has no identifier property, save() it if the object's identifier has the value assigned to a newly instantiated object, save() it if the object is versioned (by a or ), and the version property value is the same value assigned to a newly instantiated object, save() it otherwise update() the object

and merge() is very different:

if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance the persistent instance is returned the given instance does not become associated with the session, it remains detached

Here is the original answer :- https://stackoverflow.com/a/171017/3175948

Community
  • 1
  • 1
LahiruBandara
  • 931
  • 1
  • 9
  • 14