Hi,
I have two objects managed by hibernate with annotations
Code:
@Entity
@Table(name = "person")
public class Person {
..........................
@OneToMany(fetch = FetchType.EAGER, targetEntity = Address.class, cascade = CascadeType.ALL)
@JoinColumn(name = "idforeign_add")
public Set<Address> getAddresses() {
return addresses;
}
.........................
}
@Entity
@Table(name = "address")
public class Address {
.................................
private int idaddress; //referenced address table primary key
private int idforeign; //foreign key
.................................
}
I have no many-to-one in address (for person) because many table are linked with address
so I don't want to put a foreign key in address for each one.
If I load person instances, it's ok I have person instances with addresses filled owned to @OneToMany(fetch = FetchType.EAGER.
When I try to update person I have a problem,
It updates person,
It updates Address,
then it tries a second update on Address :
update address set idforeign = null where idforeign = ?
It updated address just before why do Hibernate tries this second update on address ?
I note this problem happens only if I put an HashSet for my Address collection in Person
If I load Person (with its address), addresses will be loaded in a PersistentSet
I modify an address, I update Person , it will be ok, it updates person then updates address
but if put a new address collection (an hashset) in person (with same addresses) without any modification
if I try to update person, it will try to set null on idForeign.
I think hibernate considers I delete my address it want to break the relation in setting a null on the address foreign key
Eventually, with a "normal" relation so with a one-to-many on a side and many-to-one on other side
there is no problem even with a new hashset
Could you explain me this problem ?
Thanks