I have an identical situation and identical problem, so the answer to this question would answer my question as well. I think the question was answered, but I just want to make sure:
Quote:
alpha137:Hmm, are you sure that it would work when I would always use the same A? Even if it is transfered and marshalled through/over several proxies and stubs.
Quote:
emmanuel: If you reassociate the objet graph to a new session, yes.
The way I understand this, this means that if I use a different INSTANCE of class A to update the record corresponding to a1, the null collection of Bs will always delete all the A-to-B associations in the collection table.
By implication, this means that if I do this:
Code:
//session 1
A a1 = new A();
session1.load(a1, new Integer(<id>));
Code:
//go to client (web) tier where fields from a1 are dehydrated and cached
Code:
// session 2
A a2 = <rehydrate an A from cached fields>
session.saveOrUpdate(a2);
session.flush();
I will always do this:
Code:
update a set name=... where id=...
delete from <collection_table> where a_id=...
So the best practical solution to this problem, which will keep my B collection intact is this:
Code:
A a2 = new A();
session.load(a2, a.getId()); //load the same A again in this session
a2.setData(a); //copies all fields from a to a2
a2.update();
session.flush();
All this makes sense, but I guess what I am looking for is some kind of "please don't update this" placeholder to put in the collection of Bs. It seems like when I do a lazy, uninitialized load on the collection of Bs in a1, it must contain a Hibernate-specific implementation if an uninitialized collection that serves as this "please don't update this" placeholder.
Thus, it would be nice if we could get ahold of one of those to do something like this in session2:
Code:
// session 2
A a2 = <rehydrate an A from cached fields>
a2.setBs(<please don't update this placeholder>);//uninit'd collection?
session.saveOrUpdate(a);
session.flush();
Which would keep hibernate from deleting our B associations.
Is there anything like this possible?