Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How do proxied collections really work?
PostPosted: Fri Feb 10, 2012 12:16 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
I think I understand the concept of proxied collections but, there is something else I don't get.
How can the last insertion work in he next code?

Code:
Set<String> namesSet = new HashSet<String>();
names.add("NAME 1");

Person person = (Person) session.get(Person.class, id);
person.setNames(namesSet);
// person.names is a HashSet
session.flush();
// Hibernate inserts "NAME 1"
// now person.names is a PersistentSet having namesSet as its internal set

person.names.add("NAME 2");
session.flush();
// Hibernate inserts "NAME 2"
// I can understand this because person.names is a PersistentSet

names.add("NAME 3");
// Here comes the magic!
session.flush();
// Hibernate inserts "NAME 3" but
// how does Hibernate know it has to do that ????


Top
 Profile  
 
 Post subject: Re: How do proxied collections really work?
PostPosted: Mon Feb 13, 2012 7:31 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 988
org.hibernate.collection.PersistentSet (org.hibernate.collection.internal.PersistentSet in Hiberante4)
keeps a reference to the original set passed to the constructor and also it does
internally keep snapshots of the last set state.
When flushing hibernate checks if the snapshot size is equal to the size of the original set,
if not, the hibernate knows that the collection has become dirty (forceDirty = true) and it iterates again over the original set ...

Code:
PersistentSet.equalsSnapshot(CollectionPersister) line: 113   
CollectionEntry.dirty(PersistentCollection) line: 176   
CollectionEntry.preFlush(PersistentCollection) line: 196   
EJB3FlushEventListener(AbstractFlushingEventListener).prepareCollectionFlushes(PersistenceContext) line: 187   
EJB3FlushEventListener(AbstractFlushingEventListener).flushEverythingToExecutions(FlushEvent) line: 92   
EJB3FlushEventListener(DefaultFlushEventListener).onFlush(FlushEvent) line: 51   
SessionImpl.flush() line: 1127   


Top
 Profile  
 
 Post subject: Re: How do proxied collections really work?
PostPosted: Mon Feb 13, 2012 9:00 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Thanks a lot for the hint!!!

Hib checks the snapshot size:
- if it's different then the collection is dirty
- else checks the snapshot elements one by one.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.