micronaut-data
micronaut-data copied to clipboard
Relation cascade insert not working
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
- 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<Child>
- Insert a Parent using Micronaut-data-jdbc with a Child-relation
- Child-relation is not persisted on the database
Environment Information
- Probably anywhere, verified on Linux/Mac
Example Application
https://github.com/StefanSrf/micronaut-data-cascade-bug
Version
3.3.4
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.
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?
You can assign the id manually and persist the entity which I think you should do in this case.
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?
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.
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.