Noor
Noor

Reputation: 20178

HSQ Hibernate Update Problem

I am trying to update an object but hibernate is trying to insert the object newly in the database, as a result i am getting a violation error since the object already exist in database.

for e.g.

    Category X= getCategory();//returns me a category which i then update  
    Transaction tx = null;
    try
    {
        tx=session.beginTransaction();
        tx.begin();
        session.Update(X);
        tx.commit();
        return true;
    }
    catch (Exception e)
    {
        tx.rollback();
        e.printStackTrace();
        return false;
    }

I am getting this error because the category already exist in the database

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

I through by using session.update, hibernate automatically update the object. Does anyone know how to update without writing sql??

Full error:

Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into CATEGORY (CATEGORYNAME, PARENT_CATEGORY_ID, CATEGORYID) values (?, ?, ?)
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
    at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2395)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2858)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:179)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    at com.BiddingSystem.server.ServiceImpl.TestSaveCategory(ServiceImpl.java:280)
    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 net.sf.gilead.gwt.PersistentRemoteService.processCall(PersistentRemoteService.java:174)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:243)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into CATEGORY (CATEGORYNAME, PARENT_CATEGORY_ID, CATEGORYID) values ('Laptop', '0', '397') was aborted.  Call getNextException to see the cause.
    at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2598)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2737)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 41 more

Upvotes: 1

Views: 2336

Answers (1)

killdash9
killdash9

Reputation: 2462

Without seeing what getCategory() does it's hard to answer your question. According to the hibernate docs, Session.update expects to be passed a detached entity, which is an entity that was returned by Session.load() or Session.get() from a previous session that has been closed.

It turns out that in normal hibernate usage, update is rarely needed. This is because hibernate can detect changes to objects automatically. Here is a more common pattern:


    Transaction tx = null;
    try
    {
        tx=session.beginTransaction();
        tx.begin();
        Category X = session.load(Category.class,[categoryId])
        X.setXXX(...) // update the category
        tx.commit();
        return true;
    }
    catch (Exception e)
    {
        tx.rollback();
        e.printStackTrace();
        return false;
    }

Upon commit, hibernate will know that the category returned from Load was modified and will automatically issue an update to the database.

Stated more formally, hibernate will automatically detect changes made to a persistent entities, and automatically issue an UPDATE when the transaction is committed. A persistent entity is any entity returned from Session.get, Session.load, or any of the session query functions.

More information on persistent, detached and transient objects can be found here

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/objectstate.html#objectstate-overview

Upvotes: 1

Related Questions