emmanuel wrote:
I'm not sure what you call a stateful model. If you mean keep a object version between different sessions, you can use the second-level cache.
Perhaps statefull model is a bad term. I simply meant that if (objectA.id == objectB.id) then (objectA == objectB) must also be true. So within the current JVM exists only one instance of each distinct domain object.
From what I read in the documentation the second-level cache could do this, but I can't find whether it could actually enforce it. Are the objects used directly or are they cloned and merged at each session?
The problem I'm encountering has to do with observers. Below is a fragment of the test code I'm using.
Code:
public class Egg {
// the set of observers, not persistent.
private Set observers;
// Hibernate id field
private Long id;
// persistent fields
private Date hatchtime;
private int colour;
private double size;
public void addObserver(EggObserver observer){
this.observers.add(observer);
}
protected void notifyHatching(){
Iterator iter = this.observers.iterator();
while (iter.hasNext()) {
EggObserver observer = (EggObserver) iter.next();
observer.hatched(this);
}
}
public void Heartbeat(){
//...
}
// ect...
}
When the egg hatches all observers are notified about the event. The observers are not persistent classes and are therefore not persistent. The event must fire once and only once and all subscribers must be notified once.
Would second-level cache help with this?