-->
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.  [ 1 post ] 
Author Message
 Post subject: Wie merkt sich Hibernate die Objekte in 2 Sessions?
PostPosted: Sun Jul 27, 2008 8:42 pm 
Newbie

Joined: Sat Mar 29, 2008 1:31 pm
Posts: 8
Hallo.

Ich habe ein paar Verständnisprobleme bei unten angegebenem Code.
Und zwar bekomme ich eine "org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [ValidationObject#149]",
obwohl ich ja eigentlich zwischen den beiden (völlig voneinander getrennten) Sessions hingehe und explizit die Id ändere (welche auch wirklich als identifier für Hibernate spezifiziert ist).
An Hand welcher Kriterien erkennt denn Hibernate, daß das wirklich ein (auch in einer anderen Session) schon gespeichertes Object ist?
An Hand der Id alleine ja anscheinend nicht.
Irgendwelche besonderen Optionen habe ich auch nicht angegeben (wobei ich dazu sagen muss, daß ich Eclipse Teneo fürs Mapping verwende).

Gruß,
Tanjy


Code:
ValidationProject project = ValidationProjectModelFactory.eINSTANCE.createValidationProject();
      ValidationPipeline pipe = ValidationProjectModelFactory.eINSTANCE.createValidationPipeline();
      project.setPipeline(pipe);
      ValidationObject obj = ValidationProjectModelFactory.eINSTANCE.createValidationObject();
      pipe.getObjects().add(obj);
      {
         DatabaseManager databaseManager = DatabaseManager.getInstance();
         Session session = databaseManager.openTeneoSession();
         Transaction tx = null;
         try {
            if (session == null)
               throw new HibernateException("Can't establish connection to PubCurator database.");
            
            tx = session.getTransaction();
            tx.begin();
         
            Long id = (Long) session.save(project);
            System.out.println(id);
            tx.commit();
         }
         catch(HibernateException e) {
            if (tx != null && tx.isActive())
               tx.rollback();
             throw e;
         }
         finally {
            if (session != null && session.isOpen())
               session.close();
         }
      }
      
      obj.setId(0);
      
      {
         DatabaseManager databaseManager = DatabaseManager.getInstance();
         Session session = databaseManager.openTeneoSession();
         Transaction tx = null;
         try {
            session.clear();
            session.flush();
            if (session == null)
               throw new HibernateException("Can't establish connection to PubCurator database.");
            
            tx = session.getTransaction();
            tx.begin();
         
            ValidationPipeline pipe2 = (ValidationPipeline) session.load("ValidationPipeline", pipe.getId());
            pipe2.getObjects().add(obj);
            session.flush();
            
            tx.commit();
         }
         catch(HibernateException e) {
            if (tx != null && tx.isActive())
               tx.rollback();
             throw e;
         }
         finally {
            if (session != null && session.isOpen())
               session.close();
         }
      }


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

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.