-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Session.merge uses insert instead of update
PostPosted: Fri Jul 21, 2006 7:30 am 
Newbie

Joined: Fri Jul 21, 2006 6:27 am
Posts: 10
hi all!
Problem Description:
im using hibernate together with EMF 2.2.0 and elver.
when calling Session.merge with an object already present in the dbtable(meaning: there's another object of the same type and id in the db - but with different details/values) - merge tries to do an insert instead of an update - what am i doing wrong?

TIA, chris

Hibernate version:
3.1.3

Code between sessionFactory.openSession() and session.close():
Code:
//create Datastore
HbDataStore hbds = getDataStore();
         
// persist objects
Session session = hbds.getSessionFactory().openSession();
Transaction tx = session.getTransaction();
tx.begin();
         
//store it!
for (Iterator j = resource.getContents().iterator(); j.hasNext(); ) {
   EObject eObject = (EObject)j.next();
   System.out.println("calling merge...");
   session.merge(eObject);
}
         
tx.commit();


Full stack trace of any exception that occurs:
Code:
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at jku.ce.scholion.authortool.dataaccess.DatabaseServices.persistObject(DatabaseServices.java:195)
   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:585)
   at org.apache.xmlrpc.server.PropertyHandlerMapping$1.execute(PropertyHandlerMapping.java:141)
   at org.apache.xmlrpc.server.XmlRpcServerWorker.execute(XmlRpcServerWorker.java:43)
   at org.apache.xmlrpc.server.XmlRpcServer.execute(XmlRpcServer.java:65)
   at org.apache.xmlrpc.server.XmlRpcStreamServer.execute(XmlRpcStreamServer.java:209)
   at org.apache.xmlrpc.webserver.Connection.run(Connection.java:168)
   at org.apache.xmlrpc.util.ThreadPool$MyThread.runTask(ThreadPool.java:71)
   at org.apache.xmlrpc.util.ThreadPool$MyThread.run(ThreadPool.java:87)
Caused by: java.sql.BatchUpdateException: Duplicate entry 'myidnewlalaundso' for key 1
   at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:656)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
   ... 19 more


Debug level Hibernate log excerpt:
Code:
13:14:04,857  WARN HbHelper:144 - EMF Data Store already registered under name: MySF, returning it
13:14:04,858 DEBUG SessionImpl:219 - opened session at timestamp: 4724655902138368
13:14:04,859 DEBUG JDBCTransaction:54 - begin
13:14:04,860 DEBUG ConnectionManager:415 - opening JDBC connection
13:14:04,860 DEBUG DriverManagerConnectionProvider:93 - total checked-out connections: 1
13:14:04,861 DEBUG DriverManagerConnectionProvider:109 - opening new JDBC connection
13:14:09,500 DEBUG DriverManagerConnectionProvider:115 - created connection to: jdbc:mysql://86.59.4.74:3306/test_cd2, Isolation Level: 4
13:14:09,527 DEBUG JDBCTransaction:59 - current autocommit status: false
13:14:09,527 DEBUG JDBCContext:194 - after transaction begin
13:14:09,528 DEBUG IdentifierCacheHandler:66 - ID for object jku.ce.scholion.authortool.learningobjects.impl.ResrcFileImpl not found in id cache
13:14:09,528 DEBUG VersionValue:44 - version unsaved-value strategy UNDEFINED
13:14:09,529 DEBUG AbstractSaveEventListener:462 - transient instance of: jku.ce.scholion.authortool.learningobjects.impl.ResrcFileImpl
13:14:09,529 DEBUG DefaultMergeEventListener:140 - merging transient instance
13:14:09,530 DEBUG IdentifierCacheHandler:66 - ID for object jku.ce.scholion.authortool.learningobjects.impl.ResrcFileImpl not found in id cache
13:14:09,530 DEBUG IdentifierCacheHandler:74 - Setting id: myidnewlalaundso for object jku.ce.scholion.authortool.learningobjects.impl.ResrcFileImpl in idcache
13:14:09,531 DEBUG IdentifierCacheHandler:106 - Setting version: null for object jku.ce.scholion.authortool.learningobjects.impl.ResrcFileImpl in idcache
13:14:09,531 DEBUG AbstractSaveEventListener:106 - generated identifier: myidnewlalaundso, using strategy: org.hibernate.id.Assigned
13:14:09,532 DEBUG AbstractSaveEventListener:139 - saving [ResrcFile#myidnewlalaundso]
13:14:09,556 DEBUG IdentifierCacheHandler:74 - Setting id: myidnewlalaundso for object jku.ce.scholion.authortool.learningobjects.impl.ResrcFileImpl in idcache
13:14:09,557 DEBUG Versioning:42 - Seeding: 0
13:14:09,558 DEBUG IdentifierCacheHandler:106 - Setting version: 0 for object jku.ce.scholion.authortool.learningobjects.impl.ResrcFileImpl in idcache
13:14:09,558 DEBUG IdentifierCacheHandler:106 - Setting version: 0 for object jku.ce.scholion.authortool.learningobjects.impl.ResrcFileImpl in idcache
13:14:09,559 DEBUG JDBCTransaction:103 - commit
13:14:09,559 DEBUG SessionImpl:332 - automatically flushing session
13:14:09,560 DEBUG AbstractFlushingEventListener:58 - flushing session
13:14:09,560 DEBUG AbstractFlushingEventListener:111 - processing flush-time cascades
13:14:09,561 DEBUG AbstractFlushingEventListener:153 - dirty checking collections
13:14:09,561 DEBUG AbstractFlushingEventListener:170 - Flushing entities and processing referenced collections
13:14:09,562 DEBUG AbstractFlushingEventListener:209 - Processing unreferenced collections
13:14:09,628 DEBUG AbstractFlushingEventListener:223 - Scheduling collection removes/(re)creates/updates
13:14:09,629 DEBUG AbstractFlushingEventListener:85 - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
13:14:09,647 DEBUG AbstractFlushingEventListener:91 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
13:14:09,648 DEBUG Printer:83 - listing entities:
13:14:09,649 DEBUG Printer:90 - jku.ce.scholion.authortool.learningobjects.impl.ResrcFileImpl
13:14:09,649 DEBUG AbstractFlushingEventListener:289 - executing flush
13:14:09,650 DEBUG ConnectionManager:463 - registering flush begin
13:14:09,651 DEBUG AbstractEntityPersister:2034 - Inserting entity: [ResrcFile#myidnewlalaundso]
13:14:09,651 DEBUG AbstractEntityPersister:2035 - Version: 0
13:14:09,653 DEBUG AbstractBatcher:311 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
13:14:09,654 DEBUG SQL:346 - insert into `RESRCFILE` (vn, `HREF`, `TIMESTAMP`, `FILE`, econtainer_class, e_container, e_container_featureid, `DTYPE`, `ID`) values (?, ?, ?, ?, ?, ?, ?, 'ResrcFile', ?)
13:14:09,659 DEBUG AbstractBatcher:424 - preparing statement
13:14:09,673 DEBUG AbstractEntityPersister:1821 - Dehydrating entity: [ResrcFile#myidnewlalaundso]
13:14:09,674 DEBUG IntegerType:80 - binding '0' to parameter: 1
13:14:09,674 DEBUG StringType:80 - binding 'http://insel4.com' to parameter: 2
13:14:09,677 DEBUG TimestampType:80 - binding '2006-07-22 14:47:33' to parameter: 3
13:14:09,678 DEBUG BinaryType:80 - binding 'f3e3e8e1ece1ece1ece1ece1' to parameter: 4
13:14:09,679 DEBUG IntegerType:80 - binding '0' to parameter: 7
13:14:09,679 DEBUG StringType:80 - binding 'myidnewlalaundso' to parameter: 8
13:14:09,680 DEBUG AbstractBatcher:28 - Adding to batch
13:14:09,680 DEBUG AbstractBatcher:55 - Executing batch size: 1
13:14:09,684 DEBUG AbstractBatcher:319 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
13:14:09,688 DEBUG AbstractBatcher:470 - closing statement
13:14:09,689 DEBUG JDBCExceptionReporter:63 - Could not execute JDBC batch update [insert into `RESRCFILE` (vn, `HREF`, `TIMESTAMP`, `FILE`, econtainer_class, e_container, e_container_featureid, `DTYPE`, `ID`) values (?, ?, ?, ?, ?, ?, ?, 'ResrcFile', ?)]
java.sql.BatchUpdateException: Duplicate entry 'myidnewlalaundso' for key 1
   at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:656)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at jku.ce.scholion.authortool.dataaccess.DatabaseServices.persistObject(DatabaseServices.java:195)
   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:585)
   at org.apache.xmlrpc.server.PropertyHandlerMapping$1.execute(PropertyHandlerMapping.java:141)
   at org.apache.xmlrpc.server.XmlRpcServerWorker.execute(XmlRpcServerWorker.java:43)
   at org.apache.xmlrpc.server.XmlRpcServer.execute(XmlRpcServer.java:65)
   at org.apache.xmlrpc.server.XmlRpcStreamServer.execute(XmlRpcStreamServer.java:209)
   at org.apache.xmlrpc.webserver.Connection.run(Connection.java:168)
   at org.apache.xmlrpc.util.ThreadPool$MyThread.runTask(ThreadPool.java:71)
   at org.apache.xmlrpc.util.ThreadPool$MyThread.run(ThreadPool.java:87)
13:14:09,690  WARN JDBCExceptionReporter:71 - SQL Error: 1062, SQLState: 23000
13:14:09,690 ERROR JDBCExceptionReporter:72 - Duplicate entry 'myidnewlalaundso' for key 1
13:14:09,691 ERROR AbstractFlushingEventListener:300 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at jku.ce.scholion.authortool.dataaccess.DatabaseServices.persistObject(DatabaseServices.java:195)
   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:585)
   at org.apache.xmlrpc.server.PropertyHandlerMapping$1.execute(PropertyHandlerMapping.java:141)
   at org.apache.xmlrpc.server.XmlRpcServerWorker.execute(XmlRpcServerWorker.java:43)
   at org.apache.xmlrpc.server.XmlRpcServer.execute(XmlRpcServer.java:65)
   at org.apache.xmlrpc.server.XmlRpcStreamServer.execute(XmlRpcStreamServer.java:209)
   at org.apache.xmlrpc.webserver.Connection.run(Connection.java:168)
   at org.apache.xmlrpc.util.ThreadPool$MyThread.runTask(ThreadPool.java:71)
   at org.apache.xmlrpc.util.ThreadPool$MyThread.run(ThreadPool.java:87)
Caused by: java.sql.BatchUpdateException: Duplicate entry 'myidnewlalaundso' for key 1
   at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:656)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
   ... 19 more
13:14:09,692 DEBUG ConnectionManager:472 - registering flush end
13:14:09,742 DEBUG XmlRpcStreamServer:213 - execute: Request performed successfully
13:14:09,743 DEBUG XmlRpcStreamServer:258 - execute: <-


Name and version of the database you are using:
MySQL v5.0.18-NT

The generated SQL:
Code:
insert into `RESRCFILE` (vn, `HREF`, `TIMESTAMP`, `FILE`, econtainer_class, e_container, e_container_featureid, `DTYPE`, `ID`) values (?, ?, ?, ?, ?, ?, ?, 'ResrcFile', ?)


Mapping documents:
seems to be too long, see:
http://217.172.48.251/mirror/hibernate/ ... te.hbm.xml


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.