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:
|