hi,
i'm trying to implement saving/loading to/from database in a Eclipse EMF project using Teneo/Hibernate.
saving/loading is also working somehow fine (creates mapping, tables and stuff quite fine), but once i've loaded something from the DB i can not
save it anymore.
creating new models and saving them works fine, also loading them from the DB again is doing well. but i neither can save the same model twice, nor can i load and then save the model again.
i'm quite new to this kind of stuff, so maybe it is (hopefully) just because i'm plain stupid and don't see an obvious error.
I hope you can pin down my errors or maybe tell me where to look more closely. thx for your patience :)
Hibernate version: 3.2.6
Name and version of the database: HSQLDB 1.8.0.9
Code:
the save method:
Code:
public static boolean doSaveToDB(String fileName, HbDataStore hbds)
{
if (!session.isOpen())
{
session = sessionFactory.openSession();
session.setFlushMode(FlushMode.MANUAL);
tx = session.getTransaction();
}
tx.begin();
try
{
session.saveOrUpdate(editingDomain.getResourceSet().getResources().get(0).getContents().get(0));
} catch (HibernateException e)
{
e.printStackTrace();
}
session.flush();
tx.commit();
if (session.isOpen())
{
session.clear();
session.close();
}
return true;
}
the load method:
Code:
public static Modell loadExistingModellFromDB(String fileName, HbDataStore hbds)
{
if (!session.isOpen())
{
session = sessionFactory.openSession();
session.setFlushMode(FlushMode.MANUAL);
tx = session.getTransaction();
}
URI resourceURI = URI.createURI("hbxml://?dsname=" + fileName);
Modell model = null;
tx.begin();
try
{
try
{
editingDomain.getResourceSet().getResources().get(0).load(Collections.EMPTY_MAP);
} catch (IOException e)
{
e.printStackTrace();
}
BigInteger fileID = new BigInteger(fileName);
BigInteger ID = null;
try
{
for (int i = 0; !fileID.equals(ID) || (fileID.compareTo(ID) < 0) ; i++)
{
editingDomain.getResourceSet().getResources().clear();
editingDomain.getResourceSet().createResource(resourceURI);
editingDomain.getResourceSet().getResource(resourceURI, true);
model = (Modell) editingDomain.getResourceSet().getResources().get(0).getContents().get(i);
List models = session.createSQLQuery("SELECT E_ID FROM \"modell\"").list();
ID = (BigInteger) models.get(i);
}
} catch (RuntimeException e)
{
System.err.println("No entry found!");
}
} catch (HibernateException e)
{
e.printStackTrace();
} catch (RuntimeException re)
{
re.printStackTrace();
}
session.flush();
tx.commit();
if (session.isOpen())
{
try
{
session.clear();
session.close();
} catch (HibernateException e)
{
System.err.println("Session closing failed");
e.printStackTrace();
}
}
return model;
}
and the initializing method for the HbDataStore
Code:
public static HbDataStore initializeDataStore(String fileName)
{
final HbDataStore hbds = (HbDataStore) HbHelper.INSTANCE.createRegisterDataStore(fileName);
final EPackage[] ePackages = new EPackage[] { EcupackPackage.eINSTANCE };
hbds.setEPackages(ePackages);
final Properties props = new Properties(); // the database settings
try
{
props.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver");
props.setProperty(Environment.URL, "jdbc:hsqldb:hsql://127.0.0.1/" + "ECUall");
props.setProperty(Environment.USER, "sa");
props.setProperty(Environment.PASS, "");
props.setProperty(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
props.setProperty(Environment.HBM2DDL_AUTO, "update");
props.setProperty(Environment.CACHE_PROVIDER, "org.hibernate.cache.HashtableCacheProvider");
props.setProperty(Environment.AUTOCOMMIT, "false");
hbds.setProperties(props);
} catch (RuntimeException e1)
{
e1.printStackTrace();
}
try
{
hbds.initialize();
} catch (RuntimeException e)
{
e.printStackTrace();
}
return hbds;
}
Full stack trace of any exception that occurs:the stacktrace if i try to load and then save a model:
Code:
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:43)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:293)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.engine.Cascade.cascade(Cascade.java:97)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:357)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:329)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
at teneoRuntime.TeneoRuntimeLayer.doSaveToDB(TeneoRuntimeLayer.java:398)
at teneoRuntime.TeneoRuntimeLayer.main(TeneoRuntimeLayer.java:227)
Exception in thread "main" org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:43)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:293)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at teneoRuntime.TeneoRuntimeLayer.doSaveToDB(TeneoRuntimeLayer.java:421)
at teneoRuntime.TeneoRuntimeLayer.main(TeneoRuntimeLayer.java:227)
the stacktrace if i try to save the model twice:
Code:
org.hibernate.HibernateException: Don't change the reference to a collection with cascade="all-delete-orphan": PortTypeContainer.contents
at org.hibernate.engine.Collections.prepareCollectionForUpdate(Collections.java:226)
at org.hibernate.engine.Collections.processReachableCollection(Collections.java:185)
at org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:37)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:138)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at teneoRuntime.TeneoRuntimeLayer.doSaveToDB(TeneoRuntimeLayer.java:436)
at teneoRuntime.TeneoRuntimeLayer.main(TeneoRuntimeLayer.java:235)
Debug level Hibernate log excerpt:the debug level log for the load -> save operation
Code:
18:13:55,321 DEBUG JDBCTransaction:54 - begin
18:13:55,321 DEBUG ConnectionManager:421 - opening JDBC connection
18:13:55,321 DEBUG DriverManagerConnectionProvider:93 - total checked-out connections: 0
18:13:55,321 DEBUG DriverManagerConnectionProvider:99 - using pooled JDBC connection, pool size: 0
18:13:55,321 DEBUG JDBCTransaction:59 - current autocommit status: false
18:13:55,321 DEBUG JDBCContext:214 - after transaction begin
18:13:55,321 DEBUG VersionValue:44 - version unsaved-value strategy UNDEFINED
18:13:55,321 DEBUG IdentifierValue:104 - id unsaved-value: null
18:13:55,321 DEBUG AbstractSaveEventListener:523 - detached instance of: ecupack.impl.ModellImpl
18:13:55,321 DEBUG DefaultSaveOrUpdateEventListener:205 - updating detached instance
18:13:55,321 DEBUG DefaultSaveOrUpdateEventListener:272 - updating [Modell#1]
18:13:55,321 DEBUG DefaultSaveOrUpdateEventListener:323 - updating [Modell#1]
18:13:55,321 DEBUG Cascade:115 - processing cascade ACTION_SAVE_UPDATE for: Modell
18:13:55,321 DEBUG CascadingAction:216 - cascading to saveOrUpdate: PortTypeContainer
18:13:55,321 DEBUG VersionValue:44 - version unsaved-value strategy UNDEFINED
18:13:55,321 DEBUG IdentifierValue:104 - id unsaved-value: null
18:13:55,321 DEBUG AbstractSaveEventListener:523 - detached instance of: PortTypeContainer
18:13:55,321 DEBUG DefaultSaveOrUpdateEventListener:205 - updating detached instance
18:13:55,321 DEBUG DefaultSaveOrUpdateEventListener:272 - updating [PortTypeContainer#1]
18:13:55,336 DEBUG AbstractFlushingEventListener:58 - flushing session
18:13:55,336 DEBUG AbstractFlushingEventListener:111 - processing flush-time cascades
18:13:55,336 DEBUG Cascade:115 - processing cascade ACTION_SAVE_UPDATE for: Modell
18:13:55,336 DEBUG CascadingAction:216 - cascading to saveOrUpdate: PortTypeContainer
18:13:55,336 DEBUG VersionValue:44 - version unsaved-value strategy UNDEFINED
18:13:55,336 DEBUG IdentifierValue:104 - id unsaved-value: null
18:13:55,336 DEBUG AbstractSaveEventListener:523 - detached instance of: PortTypeContainer
18:13:55,336 DEBUG DefaultSaveOrUpdateEventListener:205 - updating detached instance
18:13:55,336 DEBUG DefaultSaveOrUpdateEventListener:272 - updating [PortTypeContainer#1]