hi,
i'm trying to implement saving/loading to/from database in a Eclipse EMF
project using Teneo/Hibernate.
saving/loading is also working somewhat 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
exeptions that apear are:
Illegal attempt to associate a collection with two open sessions
if i load the model and save afterwards
and:
Don't change the reference to a collection with
cascade="all-delete-orphan"
if i try saving twice.
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;
}
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;
}
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;
}