I made a simple example to explain my problem.
Code:
Public class Person {
@Entity
private Address address;
@OneToOne
@Cascade(CascadeType.ALL)
@JoinColumn(name="ADDRESS_ID")
public Address getAddress() {...}
public void setAddress(Address address);
}
I made a unidirectional OneToOne mapping from Person to Address, and my cascading works when I delete Person, Address is also deleted.
But now when I execute the following code :
Code:
person.setAddress(new Address);
personDao.update(person);
when i look in the database the address_id of the person is correctly changed and references to the new address.
The problem is that the old address is still in the database, no longer in use by anything.
What is the best way to solve this, I don't want any unreferenced address records in my database.
Shoud I create a collection of addresses in my person Entity, and only put one address in it ?
Thanks in advance for any help provided