-->
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.  [ 3 posts ] 
Author Message
 Post subject: Modell aktualisierung in Datenbank schlägt fehl [Teneo/Hib.
PostPosted: Wed Apr 16, 2008 4:42 am 
Newbie

Joined: Tue Apr 15, 2008 5:28 am
Posts: 6
hallo,
ich versuche derzeit eine speichern/laden funktion für ein Eclipse EMF projekt mit hilfe von Teneo/Hibernate zu implementieren, hab aber das eine oder andere problem damit.
an sich funktioniert speichern sowie laden, solange man nicht versucht das selbe modell zwei mal zu speichern bzw es zu aktualisieren.
sprich,
modell erzeugen, speichern, modell erzeugen, speichern, ... -> lade klappt
modell erzeugen, speichern, speichern -> klappt nicht
laden -> speichern -> klappt nicht

ich hoff das ist ein ganz einfacher fehler und ich bin nur zu blöd ihn zu sehen ;)
danke schön für die geduld :)

Hibernate version: 3.2.6

Name and version of the database: HSQLDB 1.8.0.9

Code:
die 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;
}



die 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;
}


und die initialisirungsmethode für den 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;
}



die kompletten Stacktraces der auftretenden Exceptions:
der stacktrace der auftritt wenn man läd und anschließend speichert:
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)



der stacktrace der auftritt, wenn man zwei mal hintereinander speichert:
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)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 17, 2008 11:51 am 
Newbie

Joined: Tue Apr 15, 2008 5:28 am
Posts: 6
ok, ich hab das problem gelöst.

die hibernate ressource besitzt ihre eigene session, deshalb musste ich einfach die manuell erstellte session löschen, den code ein bischen anpassen und gut :)
(laden speichern über die ressource nicht über die session)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 4:38 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Ein persistentes Object (an eine Session gebunden) braucht kein saveorupdate.
Daher der Fehler.

Die Session ist nicht thread safe ! Du wirst bei konkurrierendem Zugriff Probleme bekommen.

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.