I have 2 entities plug and socket which have one to one mapping.
Now, plug has a foreign key relationship to the socket it is plugged in to.
Hibernate generates unique constraint on the foreign key so as to ensure a one-to-one particpation.
I have say 10 plugs which are plugged into 10 sockets.
After some time, an update comes which informs the system about the change in arrangement.
The scenario is plugA which was plugged in to socketA is now plugged into SocketB and plugB which was plugged into SocketB is now plugged into socketA.
When doing the update, hibernate first tries to update plugA's foreign key column to SocketB which results in the violation of unique constraint. This is because the plugB to socketB relationship has not yet ben altered.
What is the most correct way to model the relationship?
Note that entire update needs to occur as a single transaction.
This is the code
Inside socket
@OneToOne(mappedBy="socket")
public Plug getPlug()
{
return plug;
}
Inside Plug @OneToOne(optional=false) public Socket getSocket() { return socket; }