I've got two tables like so:
Code:
visitors
VisitorID
addresses
addressID
VisitorID
(members is a one to many relationship to visitors)
Snippets from my class/mapping (xdoclet) code looks like so:
Code:
Visitor
/**
* @hibernate.set lazy="true" inverse="true"
* @hibernate.collection-key column="VisitorID"
* @hibernate.collection-one-to-many class="Address"
*/
public Set getAddresses() { return addresses; }
public void setAddresses(Set addresses) { this.addresses = addresses; }
private Set addresses;
Code:
Address
/**
* @hibernate.many-to-one
* @hibernate.column name="VisitorID"
*/
public Visitor getVisitor() { return visitor; }
public void setVisitor(Visitor visitor) { this.visitor = visitor; }
protected Visitor visitor;
This is all working splendidly, nothings broken. Here's my question: What' happens when I do this?:
Code:
Visitor oldVisitor = dao.getVisitor(123);
Visitor newVisitor = dao.getvisitor(234);
newAddresses = newVisitor.getAddresses(); // <---
oldVisitor.setAddresses(newAddresses) // <---
dao.save(oldVisitor)
In other words, what happens if I take an addresses set from my new visitor object (whose addresses have IDs pointing to that *new* visitor record) and simply copy them into my old visitor record and then save.
Is Hibernate smart enough to see that this set need to be "reassociated" with a new object? If yes, does it take the original addresses and rewrite their IDs with an UPDATE statement? Or maybe it just inserts new address records that are duplicates of the original but with appropriate IDs?
Many thanks!
- Gary
Hibernate version: 2.1
Code between sessionFactory.openSession() and session.close():Using Spring to manage the session
Full stack trace of any exception that occurs:not applicable
Name and version of the database you are using:MySQL 4.1.8
The generated SQL (show_sql=true):not applicable
Debug level Hibernate log excerpt:not applicable[/code]