tenwit wrote:
Hibernate already does what your original question asks. You don't even need the saveOrUpdate, because you have loaded the User object and you haven't discarded the Session. You can just flush the user from the session and it will work as you want it to (asuming that you have the appropriate cascade options).
Maybe I didn't specify my question clear enough. I meant how to implement similar solution without force action to be in the same transaction.
Here is the senari, RMI send the object to the client side, and UI make some change to one of the userAddress in the set and send back the whole object. Then reattach happen in a new session. I want to only deal with user object without the side effect. In other word, update dirty object without redundant update and select. I read a little more on the extending hibernate, maybe pre-insert-and-pre-update would be good place to filter out unneccessary update. But the whole event listener thing is still under progress. I tried release candiate 2 but rollback to 3.05.
tenwit wrote:
If you particularly want a removeAddress method to leave the address accessible to getAddress() but remove it from the DB when you next flush/save, then I recommend implementing a save method for user objects, which removes marked objects from the persistent collection before calling saveOrUpdate or flush. It's not a good idea to do it this way, at least not in application code. Maybe you should implement a factory for saving this object? Or even better, use hibernate's Interceptor functionality and remove marked items in the preFlush method for the User class.
I will look into interceptor, any helpful documentation will great appricated.
[qoute="tenwit"]
I don't think I understand your unsaved-value question. Does
Code:
<id name="id" column="id" type="integer" unsaved-value="-1">
<generator .../>
</id>
not do what you want it to?[/quote]
I want to know if there is a way to specify all number less than 0 consider to be new object. If I can tailor this bahavior, then i can attach multiple new object by manually assigned unique ids (negative) to a collection without worry about equals and hashcode not working correctly.
Code:
public boolean equals(Object rhs) {
if (rhs == null)
return false;
if (! (rhs instanceof Person))
return false;
Person that = (Person) rhs;
if (this.getId() == null || that.getId() == null)
return false;
return (this.getId().equals(that.getId()));
}
public int hashCode() {
if (this.hashValue == 0) {
int result = 17;
int idValue = this.getId() == null ? 0 : this.getId().hashCode();
result = result * 37 + idValue;
this.hashValue = result;
}
return this.hashValue;
}
lastly, thank you for replay my post.