Hi,
I have a strange situation, not sure if anyone has seen this before, but I have a transient object, A, and want to set the relationships on this object, so I set the ManyToOne relationship by using the set method for B, and then I ensure that B has an instance of A in its collection of A, but when I do this it creates a new instance of A, so then it tries to insert 2 instances of A into the database, which obviously throws a Unique constraint error. I put a breakpoint on the constructor of A, and it is called when I call getB().getSetOfA().add(A), Any ideas as to why it tries to create a new instance of A?
I am very puzzled by this, so is this usual? Or has someone seen anything like this before?
I can't post any code for this at the moment, as I don't have it available to me, but I can post some more details tomorrow if it helps.
Thanks in advance, Osian
p.s. Another thing I would like to know, is why, when using a manytomany relationship, do you have to use merge and not persist when you have pre-existing objects for the manytomany in the system? Is there an annotation that I can use to avoid having to use merge? as I know that the other side of the manytomany will always be pre-existing data in the database.
|