-->
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.  [ 7 posts ] 
Author Message
 Post subject: Cannot cascade insert when using saveOrUpdate()
PostPosted: Wed Oct 12, 2005 9:24 am 
Beginner
Beginner

Joined: Wed Oct 12, 2005 8:52 am
Posts: 23
Description:
The situation is that i need to retrieve an object with all dependencies from one database and upload it into another database(which is empty). This example have one group consisting of many users.
I have used two sessions which different config files to be able to connect to the databases. The second session is opend when the first one is closed.

As when retrieving the records already have their primary keys (ids), Hibernate will try to do an update in the empty database when i use insertOrUpdate().

In the group hbm file i have tried cascade="save-update" and "all" but to no success!

Why is it trying to do an update when the record does not exist in the second detabase?

Also i am having problems liking users to groups when updating, the foreign key filed is set to null.

Hibernate version: 2.1

Mapping documents:
com/warfrog/hibernatetutorial/hibernate/Group.hbm.xml
com/warfrog/hibernatetutorial/hibernate/User.hbm.xml
com/warfrog/hibernatetutorial/hibernate/Userdetail.hbm.xml
com/warfrog/hibernatetutorial/hibernate/Login.hbm.xml
com/warfrog/hibernatetutorial/hibernate/Country.hbm.xml

Code between sessionFactory.openSession() and session.close():
Session session = getHibernateSession();
Query getFrost = session.createQuery(
" select distinct g from Group g where g.groupId =65" );
List result = getFrost.list();
Group gr = new Group();

gr = (Group)result.get(i);

session.close();

session = getHibernateSession2();
System.out.println("Uploading to new DB");
Transaction tx = session.beginTransaction();

session.saveOrUpdate(gr);
tx.commit();

Full stack trace of any exception that occurs:
(cfg.Environment 462 ) Hibernate 2.1.3
(cfg.Environment 496 ) loaded properties from resource hibernate.properties: {hibernate.connection.username=sa, hibernate.connection.password=, hibernate.cglib.use_reflection_optimizer=true, hibernate.connection.pool_size=3, hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect, hibernate.show_sql=true, hibernate.connection.url=jdbc:hsqldb:hsql://localhost, hibernate.connection.driver_class=org.hsqldb.jdbcDriver}
(cfg.Environment 519 ) using CGLIB reflection optimizer
(cfg.Configuration 347 ) Mapping resource: com/warfrog/hibernatetutorial/hibernate/Group.hbm.xml
(cfg.Binder 229 ) Mapping class: com.warfrog.hibernatetutorial.hibernate.Group -> GROUPS
(cfg.Configuration 347 ) Mapping resource: com/warfrog/hibernatetutorial/hibernate/User.hbm.xml
(cfg.Binder 229 ) Mapping class: com.warfrog.hibernatetutorial.hibernate.User -> USERS
(cfg.Configuration 347 ) Mapping resource: com/warfrog/hibernatetutorial/hibernate/Userdetail.hbm.xml
(cfg.Binder 229 ) Mapping class: com.warfrog.hibernatetutorial.hibernate.Userdetail -> USERDETAILS
(cfg.Configuration 347 ) Mapping resource: com/warfrog/hibernatetutorial/hibernate/Login.hbm.xml
(cfg.Binder 229 ) Mapping class: com.warfrog.hibernatetutorial.hibernate.Login -> LOGIN
(cfg.Configuration 347 ) Mapping resource: com/warfrog/hibernatetutorial/hibernate/Country.hbm.xml
(cfg.Binder 229 ) Mapping class: com.warfrog.hibernatetutorial.hibernate.Country -> COUNTRY
(cfg.Configuration 613 ) processing one-to-many association mappings
(cfg.Binder 1168) Mapping collection: com.warfrog.hibernatetutorial.hibernate.Group.users -> USERS
(cfg.Binder 1168) Mapping collection: com.warfrog.hibernatetutorial.hibernate.User.userdetails -> USERDETAILS
(cfg.Binder 1168) Mapping collection: com.warfrog.hibernatetutorial.hibernate.User.logins -> LOGIN
(cfg.Binder 1168) Mapping collection: com.warfrog.hibernatetutorial.hibernate.Userdetail.countries -> COUNTRY
(cfg.Configuration 622 ) processing one-to-one association property references
(cfg.Configuration 647 ) processing foreign key constraints
(dialect.Dialect 82 ) Using dialect: net.sf.hibernate.dialect.HSQLDialect
(cfg.SettingsFactory 62 ) Use outer join fetching: false
(connection.DriverManagerConnectionProvider 42 ) Using Hibernate built-in connection pool (not for production use!)
(connection.DriverManagerConnectionProvider 43 ) Hibernate connection pool size: 3
(connection.DriverManagerConnectionProvider 77 ) using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost
(connection.DriverManagerConnectionProvider 78 ) connection properties: {user=sa, password=}
(transaction.TransactionManagerLookupFactory 33 ) No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
(cfg.SettingsFactory 102 ) Use scrollable result sets: true
(cfg.SettingsFactory 105 ) Use JDBC3 getGeneratedKeys(): false
(cfg.SettingsFactory 108 ) Optimize cache for minimal puts: false
(cfg.SettingsFactory 114 ) echoing all SQL to stdout
(cfg.SettingsFactory 117 ) Query language substitutions: {}
(cfg.SettingsFactory 128 ) cache provider: net.sf.ehcache.hibernate.Provider
(cfg.Configuration 1093) instantiating and configuring caches
(impl.SessionFactoryImpl 119 ) building session factory
(impl.SessionFactoryObjectFactory 82 ) no JNDI name configured
Hibernate: select distinct group0_.GROUP_ID as GROUP_ID, group0_.NAME as NAME, group0_.REGISTRATION_DATE as REGISTRA3_, group0_.ANNUAL_BUDGET as ANNUAL_B4_ from GROUPS group0_ where (group0_.GROUP_ID=65 )
Number of Objects: 1
com.warfrog.hibernatetutorial.hibernate.Group@fefe3f[groupId=65]
Group: HIBERNATE_TEST
Hibernate: select users0_.USER_ID as USER_ID__, users0_.GROUP_ID as GROUP_ID__, users0_.USER_ID as USER_ID0_, users0_.FIRST_NAME as FIRST_NAME0_, users0_.LAST_NAME as LAST_NAME0_, users0_.DOB as DOB0_, users0_.SSN as SSN0_, users0_.PHONE as PHONE0_, users0_.SALARY as SALARY0_, users0_.ACTIVE as ACTIVE0_, users0_.GROUP_ID as GROUP_ID0_ from USERS users0_ where users0_.GROUP_ID=?
Hibernate: select logins0_.LOGIN_ID as LOGIN_ID__, logins0_.USER_ID as USER_ID__, logins0_.LOGIN_ID as LOGIN_ID0_, logins0_.USER_NAME as USER_NAME0_, logins0_.PASSWORD as PASSWORD0_, logins0_.LEVEL2 as LEVEL20_, logins0_.USER_ID as USER_ID0_ from LOGIN logins0_ where logins0_.USER_ID=?
Hibernate: select userdetail0_.DETAILS_ID as DETAILS_ID__, userdetail0_.USER_ID as USER_ID__, userdetail0_.DETAILS_ID as DETAILS_ID0_, userdetail0_.ADDRESS1 as ADDRESS10_, userdetail0_.ADDRESS2 as ADDRESS20_, userdetail0_.USER_ID as USER_ID0_ from USERDETAILS userdetail0_ where userdetail0_.USER_ID=?
Hibernate: select logins0_.LOGIN_ID as LOGIN_ID__, logins0_.USER_ID as USER_ID__, logins0_.LOGIN_ID as LOGIN_ID0_, logins0_.USER_NAME as USER_NAME0_, logins0_.PASSWORD as PASSWORD0_, logins0_.LEVEL2 as LEVEL20_, logins0_.USER_ID as USER_ID0_ from LOGIN logins0_ where logins0_.USER_ID=?
Hibernate: select userdetail0_.DETAILS_ID as DETAILS_ID__, userdetail0_.USER_ID as USER_ID__, userdetail0_.DETAILS_ID as DETAILS_ID0_, userdetail0_.ADDRESS1 as ADDRESS10_, userdetail0_.ADDRESS2 as ADDRESS20_, userdetail0_.USER_ID as USER_ID0_ from USERDETAILS userdetail0_ where userdetail0_.USER_ID=?
Hibernate: select logins0_.LOGIN_ID as LOGIN_ID__, logins0_.USER_ID as USER_ID__, logins0_.LOGIN_ID as LOGIN_ID0_, logins0_.USER_NAME as USER_NAME0_, logins0_.PASSWORD as PASSWORD0_, logins0_.LEVEL2 as LEVEL20_, logins0_.USER_ID as USER_ID0_ from LOGIN logins0_ where logins0_.USER_ID=?
Hibernate: select userdetail0_.DETAILS_ID as DETAILS_ID__, userdetail0_.USER_ID as USER_ID__, userdetail0_.DETAILS_ID as DETAILS_ID0_, userdetail0_.ADDRESS1 as ADDRESS10_, userdetail0_.ADDRESS2 as ADDRESS20_, userdetail0_.USER_ID as USER_ID0_ from USERDETAILS userdetail0_ where userdetail0_.USER_ID=?
Hibernate: select logins0_.LOGIN_ID as LOGIN_ID__, logins0_.USER_ID as USER_ID__, logins0_.LOGIN_ID as LOGIN_ID0_, logins0_.USER_NAME as USER_NAME0_, logins0_.PASSWORD as PASSWORD0_, logins0_.LEVEL2 as LEVEL20_, logins0_.USER_ID as USER_ID0_ from LOGIN logins0_ where logins0_.USER_ID=?
Hibernate: select userdetail0_.DETAILS_ID as DETAILS_ID__, userdetail0_.USER_ID as USER_ID__, userdetail0_.DETAILS_ID as DETAILS_ID0_, userdetail0_.ADDRESS1 as ADDRESS10_, userdetail0_.ADDRESS2 as ADDRESS20_, userdetail0_.USER_ID as USER_ID0_ from USERDETAILS userdetail0_ where userdetail0_.USER_ID=?
Hibernate: select logins0_.LOGIN_ID as LOGIN_ID__, logins0_.USER_ID as USER_ID__, logins0_.LOGIN_ID as LOGIN_ID0_, logins0_.USER_NAME as USER_NAME0_, logins0_.PASSWORD as PASSWORD0_, logins0_.LEVEL2 as LEVEL20_, logins0_.USER_ID as USER_ID0_ from LOGIN logins0_ where logins0_.USER_ID=?
Hibernate: select userdetail0_.DETAILS_ID as DETAILS_ID__, userdetail0_.USER_ID as USER_ID__, userdetail0_.DETAILS_ID as DETAILS_ID0_, userdetail0_.ADDRESS1 as ADDRESS10_, userdetail0_.ADDRESS2 as ADDRESS20_, userdetail0_.USER_ID as USER_ID0_ from USERDETAILS userdetail0_ where userdetail0_.USER_ID=?
(cfg.Configuration 872 ) configuring from resource: /hibernateprop/hibernate.cfg.xml
(cfg.Configuration 844 ) Configuration resource: /hibernateprop/hibernate.cfg.xml
(cfg.Configuration 328 ) Mapping resource: com/warfrog/hibernatetutorial/hibernate/Group.hbm.xml
(cfg.Binder 229 ) Mapping class: com.warfrog.hibernatetutorial.hibernate.Group -> GROUPS
(cfg.Configuration 328 ) Mapping resource: com/warfrog/hibernatetutorial/hibernate/User.hbm.xml
(cfg.Binder 229 ) Mapping class: com.warfrog.hibernatetutorial.hibernate.User -> USERS
(cfg.Configuration 328 ) Mapping resource: com/warfrog/hibernatetutorial/hibernate/Userdetail.hbm.xml
(cfg.Binder 229 ) Mapping class: com.warfrog.hibernatetutorial.hibernate.Userdetail -> USERDETAILS
(cfg.Configuration 328 ) Mapping resource: com/warfrog/hibernatetutorial/hibernate/Login.hbm.xml
(cfg.Binder 229 ) Mapping class: com.warfrog.hibernatetutorial.hibernate.Login -> LOGIN
(cfg.Configuration 328 ) Mapping resource: com/warfrog/hibernatetutorial/hibernate/Country.hbm.xml
(cfg.Binder 229 ) Mapping class: com.warfrog.hibernatetutorial.hibernate.Country -> COUNTRY
(cfg.Configuration 1030) Configured SessionFactory: null
(cfg.Configuration 613 ) processing one-to-many association mappings
(cfg.Binder 1168) Mapping collection: com.warfrog.hibernatetutorial.hibernate.Group.users -> USERS
(cfg.Binder 1168) Mapping collection: com.warfrog.hibernatetutorial.hibernate.User.userdetails -> USERDETAILS
(cfg.Binder 1168) Mapping collection: com.warfrog.hibernatetutorial.hibernate.User.logins -> LOGIN
(cfg.Binder 1168) Mapping collection: com.warfrog.hibernatetutorial.hibernate.Userdetail.countries -> COUNTRY
(cfg.Configuration 622 ) processing one-to-one association property references
(cfg.Configuration 647 ) processing foreign key constraints
(dialect.Dialect 82 ) Using dialect: net.sf.hibernate.dialect.HSQLDialect
(cfg.SettingsFactory 62 ) Use outer join fetching: false
(connection.DriverManagerConnectionProvider 42 ) Using Hibernate built-in connection pool (not for production use!)
(connection.DriverManagerConnectionProvider 43 ) Hibernate connection pool size: 3
(connection.DriverManagerConnectionProvider 77 ) using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://172.16.200.91
(connection.DriverManagerConnectionProvider 78 ) connection properties: {user=sa, password=}
(transaction.TransactionFactoryFactory 31 ) Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
(transaction.TransactionManagerLookupFactory 33 ) No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
(cfg.SettingsFactory 102 ) Use scrollable result sets: true
(cfg.SettingsFactory 105 ) Use JDBC3 getGeneratedKeys(): false
(cfg.SettingsFactory 108 ) Optimize cache for minimal puts: false
(cfg.SettingsFactory 114 ) echoing all SQL to stdout
(cfg.SettingsFactory 117 ) Query language substitutions: {}
(cfg.SettingsFactory 128 ) cache provider: net.sf.ehcache.hibernate.Provider
(cfg.Configuration 1093) instantiating and configuring caches
(impl.SessionFactoryImpl 119 ) building session factory
(impl.SessionFactoryObjectFactory 82 ) no JNDI name configured
Uploading to new DB
Hibernate: update GROUPS set NAME=?, REGISTRATION_DATE=?, ANNUAL_BUDGET=? where GROUP_ID=?
(impl.SessionImpl 2368) Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:687)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:640)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2407)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2361)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2229)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at HibernateTutorial.getDBItem(HibernateTutorial.java:266)
at HibernateTutorial.main(HibernateTutorial.java:470)
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:687)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:640)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2407)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2361)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2229)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at HibernateTutorial.getDBItem(HibernateTutorial.java:266)
at HibernateTutorial.main(HibernateTutorial.java:470)

Name and version of the database you are using:
HSQLDB 1.7.1.b

The generated SQL (show_sql=true):
Hibernate: update GROUPS set NAME=?, REGISTRATION_DATE=?, ANNUAL_BUDGET=? where GROUP_ID=?

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 9:59 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
why don't you juse use session.save?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 10:06 am 
Beginner
Beginner

Joined: Wed Oct 12, 2005 8:52 am
Posts: 23
session.save will save the groups but will try to update users, thus resulting in a exception like "Could not synchronize database state with session net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)"

thanks for the reply :-)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 10:10 am 
Beginner
Beginner

Joined: Wed Oct 12, 2005 8:52 am
Posts: 23
the reason why it is trying to update users when doing session.save() instead of inserting, is because the objects contains primary keys (id fileds are not null in the object) of users!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 10:31 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
use the unsaved-value tag for the users id to tell hibernate when an object needs to be saved or updated

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 12, 2005 10:47 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
kochcp wrote:
use the unsaved-value tag for the users id to tell hibernate when an object needs to be saved or updated


If you upgrade to H3, there's a new Session.replicate() method that will copy everything, including the ID's, and ignore the <generator> tags.

Otherwise, the only way around this is to set all the ID's to NULL.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 9:18 am 
Beginner
Beginner

Joined: Wed Oct 12, 2005 8:52 am
Posts: 23
Thank a lot, I tried both ways: using unsaved tag and switched to Hibernate 3 using session.replicate(). It now works fine.

Thank Guys!!


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

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.