-->
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.  [ 25 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Wed Jul 19, 2006 12:57 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I don't see your calls to Session.beginTransaction() and Transaction.commit(). If you don't use a transaction, I think it flushes the session everytime you do a save() or update(). If you have a for or while loop for processing the 2,000 objects, I'd recommend wrapping the loop with a single transaction. If you do that, it won't flush the session until the transaction is committed and hence it won't be as nearly as slow. i.e. it only has to scan the session cache once when the transaction is committed rather than everytime you do a save()/update().The reason it gets slower and slower is because everytime it flushes the session, it has to check all the objects in the session for changes. Note, a flush can happen at other times too, not just when you explicitly call it. You may want to use Session.setFlushMode() as well and set to to Commit also. The default is Auto and it flushes the session everytime you do a query.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 1:21 am 
Beginner
Beginner

Joined: Mon Jul 03, 2006 5:40 am
Posts: 20
Location: Russia
my cycle in enclosed in
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
...............
session.getTransaction().commit();


I'm using session.flush() to use JDBC batch mode effectively, as supposed in one of the tutorials. when ~50 objects are created in JVM, I'm using flush(), so they are written to DB. it works fine, and the problem is with session.clear() at this moment.
I have read about Session.setFlushMode(), but I don't want to change the default mode.

note: without clear() method, everything works fine for me, but slower and slower with each 100 results. and with clear() I get this Exception.
I think I need to find the root problem of that exception.


Top
 Profile  
 
 Post subject: Another instance
PostPosted: Tue Oct 03, 2006 4:02 pm 
Newbie

Joined: Tue Oct 03, 2006 3:58 pm
Posts: 1
Location: Phoenix, AZ
I too encountered this error once. I was trying to re-associate an entity with another.

Specifically, I was trying to associate a Sku with a different product. so:

oldProduct.removeSku( sku );
newProduct.addSku( sku );

didn't work because I had my cascade set to delete-orphan on the product's sku collection. By removing the sku, the a sku's collection was set for deletion. Then adding the sku to another product put the same collection in there again to be added. Hibernate got confused.

FWIW


Top
 Profile  
 
 Post subject: possible workaround
PostPosted: Fri Oct 27, 2006 3:23 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
I was having the same problem where I was hibernate.clear()-ing and then getting teh above error. I tried changing my direct save calls post-clear to saveOrUpdateCopy() but that didn't fix it.

I ended up removing all cascades from childObjects -> parents so that I wouldn't get inadvertant cascades causing this.

ie: as long as you block all saveOrUpdate calls (either through direct calls or cascades) it seems to work around this issue.

Just thought I'd add the bit about cascades biting me in the arse in case anyone else was running into it.

-mp


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 12:31 pm 
Newbie

Joined: Fri Dec 08, 2006 11:48 am
Posts: 6
From reading the bug (which is now fixed for 3.2.4), I've discovered a better workaround which works well for me:

Code:
sess.evict(objToUpdate);
sess.clear()
sess.update(objToUpdate);


This works because evict() isn't broken like clear() and actually does a good job of removing references to objToUpdate's collection.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 11:42 am 
Beginner
Beginner

Joined: Fri Apr 20, 2007 10:48 am
Posts: 49
Location: France
I've downloade the last version 3.2.5.GA.

I have a similar problem, it seems the cause is that we are not allowed to do any EM operations in a Persistence listener:
http://forum.hibernate.org/viewtopic.ph ... 67#2361167

Can someone explain why???

It still happens even after the fix bundeled with the 3.2.4
http://opensource.atlassian.com/project ... se/HHH-511

Code:
javax.persistence.RollbackException: Error while commiting the transaction
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
   at main.JPAMain.save(JPAMain.java:46)
   at main.JPAMain.main(JPAMain.java:19)
Caused by: org.hibernate.HibernateException: Found two representations of same collection: fr.into.tests.Person.archiveList
   at org.hibernate.engine.Collections.processReachableCollection(Collections.java:153)
   at org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:37)
   at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)


My code is under 3.2.3.GA:
a listener
Code:
   @PreUpdate
   public void updating(Archivable<T, P> source) {
      System.out.println( "Updating " + source.getEntry().getNote() +" id: "+ source.getId() );
      if( !source.getEntry().isLastOccurence() )
         throw new UnsupportedOperationException( "You can't archive archived objects (other than original inserts)" + source );
      EntityManager em = getEntityManager();
      // must clear to avoid getting the same instance as archivable (from cache)
      em.clear();
      T toArchive = (T) em.find( source.getClass(), source.getId() );
      toArchive.setId( 0 );
      ArchivePointer<T> archivePointer = source.createArchiveRecord( toArchive );
      toArchive.setArchivePointer( archivePointer );
      toArchive.getEntry().setLastOccurence( false );
      // must clear to oblige hibernate to insert a new row (elwhere even calling
      // persist attempts an update)
      em.clear();
      toArchive.removeRelationships();
      
      //FIXME remove
      toArchive.getEntry().setNote( "archive" );
      
      System.out.println( "Inserting old archive values with id number: " + toArchive.getId() );
      em.persist( toArchive );
      // no more clear needed: archivable and old are distinct instances.
   }


and the method that fires the event's code (it happens on the second save that calls em.merge().

Code:
   public static void main(String[] args) throws NamingException {
      save( false );
      save( true );
   }

   private static void save(boolean update) {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory( "IntoJPA" );
      EntityManager em = emf.createEntityManager();

      try {
         EntityTransaction transaction = em.getTransaction();
         transaction.begin();
         Person zied;
         if( update )
            zied = em.find( Person.class, 1L );
         else
            zied = new Person();

         zied.getEntry().setNote( update ? "second" : "first" );
         zied.getEntry().setStartDate( new Date() );
         JPAMain.EM.set( em );
         
         if( update ) {
            System.out.println( "Updating Person: " + zied.getEntry().getNote() );
            em.merge( zied );
         } else {
            System.out.println( "Inserting Person: " + zied.getEntry().getNote() );
            em.persist( zied );
         }
         transaction.commit();
      } catch( Exception ex ) {
         ex.printStackTrace();
      } finally {
         em.close();
         emf.close();
      }
   }

_________________
Regards,
Zied Hamdi


Top
 Profile  
 
 Post subject: Reg: Found two representations of same collection: hibernate
PostPosted: Wed Oct 10, 2007 2:43 am 
Newbie

Joined: Wed Oct 10, 2007 2:34 am
Posts: 2
Location: Chennai
Hi,

I also had faced the same problem. The problem I investigated so far shows that hibernate is not able to initialize the collection objects after session.clear().

In my case, the objects were having only static data and is not going to change often. I had made the object as static and its collections to remain in memory till the session of the user is logged out.
The object in case having collections is also loaded along with that by using "lazy="false" . This makes the object in memory.


When the hibernate is trying to load again the object, I had made a check for null and skipped the loading.

public static Object obj = null;
....
....
public static Object returnObject(){
if(obj == null){
obj = DAO.findById() ....
}
return obj;
}

in case of static data, it works fine.

_________________
Thanks
~Satish.


Top
 Profile  
 
 Post subject: Reg: Session.merge
PostPosted: Wed Oct 10, 2007 2:47 am 
Newbie

Joined: Wed Oct 10, 2007 2:34 am
Posts: 2
Location: Chennai
Zied Hamdi,

Session.merge() will return an merged object . You have to typecast the object and the save it.

_________________
Thanks
~Satish.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 20, 2008 9:00 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
In case anyone is having a similar issue, I came across a scenario where this issue crops up when the child in a @OneToMany association has a @ManyToOne mapping with a cascade value of REMOVE or ALL. Details can be found here:

http://www.damnhandy.com/2008/08/20/hib ... ion-error/

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject: Re: test case: "Found two representations of same collectio
PostPosted: Thu Dec 09, 2010 3:38 pm 
Newbie

Joined: Thu Dec 09, 2010 3:35 pm
Posts: 1
there are two session.flush();
remove the first one and then try again.
Due to the limited time, I did not read the full thread but I was getting same exception in terms of error message and it worked for me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 25 posts ]  Go to page Previous  1, 2

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.