Hello,
To keep my description short, I will use the classical Parent-Child example to explain my problem.
The two persistent classes, Parent and Child are linked with a bidirectionnal many-to-many relationship.
My use case is to try to replicate parents and children from one session (database A) to another (database B).
Here is a sample of the data I have in database A :
Code:
Table Parent
-------------
id name
1 John
2 Susan
Table ParentChild
------------------
parent_id child_id
1 1
1 2
2 1
2 2
Table Child
------------
id name
1 Oliver
2 Paul
And for the mapping, I have specified a cascade="replicate, evict" for the Parent-Child side of the relationship.
So, first I load the Parents and evict them from session A (which also evicts their children).
Then I simply try to replicate each parent and its children by
Code:
sessionB.replicate(parent1, ReplicationMode.LATEST_VERSION);
sessionB.replicate(parent2, ReplicationMode.LATEST_VERSION);
I assumed that, given the specified cascade attribute on the Parent-Child relationship, this would be sufficient to replicate parents along with their children.
Unfortunately, this does not work because the replication mode seems not to be cascaded, as you can see in the following session flush result :
Code:
INSERT INTO PARENT (1, 'John')
INSERT INTO CHILD (1, 'Oliver')
INSERT INTO CHILD (2, 'Paul')
INSERT INTO PARENT_CHILD (1, 1)
INSERT INTO PARENT_CHILD (1, 2)
INSERT INTO PARENT (2, 'Susan')
INSERT INTO CHILD (1, 'Oliver') //Ooops ! duplicate insert
...
The last line violates the unique PK constraint in my db schema.
It seems that Hibernate does not keep track of what was already replicated in the same transaction.
Is this the expected behavior of cascading replicate ?
Is there another workaround that the one consisting in avoid using cascading replicate and replicate distinctly parents and then children ? (this would be a pain in our 'real-world' complex data model)
I'd love to hear about a nice'n'easy fix/workaround from the experts...
Thanks,
Gregory
PS :
Hibernate version: 3.0.5