micronaut-projects / micronaut-data

Ahead of Time Data Repositories
Apache License 2.0
459 stars 195 forks source link

Relation cascade insert not working #1381

Open StefanSrf opened 2 years ago

StefanSrf commented 2 years ago

Expected Behavior

@Relation with Relation.Cascade.ALL should persist child-relations on save

Actual Behaviour

Only the parent is persisted. The debug-logs indicate that the child-relation is also inserted, but they are not saved on the database.

Steps To Reproduce

  1. Have two @MappedEntity Parent and Child, where the Parent relates to the Child with @field:Relation(value = Relation.Kind.ONE_TO_MANY, mappedBy = "parentId", cascade = [Relation.Cascade.ALL]) val children: List
  2. Insert a Parent using Micronaut-data-jdbc with a Child-relation
  3. Child-relation is not persisted on the database

Environment Information

Example Application

https://github.com/StefanSrf/micronaut-data-cascade-bug

Version

3.3.4

dstepanov commented 2 years ago

It's happening because you already have the id assigned. In this case, we assume that the entity is already persisted. Can you try with the latest RC? I think there was some improvement in the case.

StefanSrf commented 2 years ago

Using the latest RC unfortunately does not help. Generating the childs-id on the database works, but that would be a workaround. In my understanding an existing Id should only be used as an indicator for an already persisted entity when a @GeneratedValue annotation is set on that Id. Do you agree?

dstepanov commented 2 years ago

You can assign the id manually and persist the entity which I think you should do in this case.

StefanSrf commented 2 years ago

Assigning the id manually and persisting works for the parent entity, but the child entity does not get persisted, i.e. the @Relation-Relation.Cascade.ALL is ignored/not functional. I can of course manually persist the child entity, but what's the point of having a cascade value then?

dstepanov commented 2 years ago

There are some limitations because we don't have any kind of session compared to Hibernate. The most common use-case is to have IDs assigned or uniquely generated, the cascade works in those cases.

StefanSrf commented 2 years ago

Yes, i know. I think it should also work for the less common use-case of already existing ids. I understand that this is probably not trivial to implement. Currently it's unfortunately not obvious that this scenario is not supported.