3

Using Spring Data JPA 1.11.6.RELEASE, I have the following trouble;

With a simple 1-to-n relationship between them, I have TableA, and TableB, where A contains multiple B. And I have RepoA, that modifies not only TableA, but also TableB as a dependent child.

In this sense, when I have the following in DB (writing entities in JSON);

{
    "uuid": "f10cdd75-ffbe-49e6-b7a5-ad6f8e15b2b5",
    "name": "title",
    "listOfB": [{
        "pk": 1
    }, {
        "pk": 2
    }]
}

and I'd like to update TableA, and consequentially TableB's with the following through RepoA;

{
    "uuid": "f10cdd75-ffbe-49e6-b7a5-ad6f8e15b2b5",
    "name": "title",
    "listOfB": [{
        "pk": 2
    }, {
        "pk": 3
    }]
}

But I am getting the constraint violation due to Hibernate following its famous order of operations, it tries to insert all dependent TableB values, without removing the original ones.

Is there no way to overwrite the TableB entities in any way? I was able to find this solution;

select `TableA` with `TableB` values
clear `"listOfB"`
save & flush `TableA` // deletes all current `TableB`
add new `B`'s to `"listOfB"`
save again

But it is very laborious, and ugly, plus the more such tables I have, the more such code I have to write. Can't I have some definition in JPA to allow this behaviour automatically? Do not treat this table as a proper table, but only as a basic resource, that should be overwritten in all update requests?

buræquete
  • 14,226
  • 4
  • 44
  • 89
  • Post those entities. – Chacko Sep 30 '17 at 06:48
  • @ChackoMathew as I said in my question, I've already posted them in JSON, please read before commenting... – buræquete Sep 30 '17 at 06:49
  • Sorry, I was not clear. I was asking for the java entity classes for the tables. – Chacko Sep 30 '17 at 06:52
  • This https://stackoverflow.com/questions/25125210/hibernate-persistentset-remove-operation-not-working or this https://stackoverflow.com/questions/46363275/onetomany-delete-children should help – Chacko Sep 30 '17 at 08:33

0 Answers0