Support cascading save in Micronaut JDBC
See original GitHub issueHi, I am not able to save 2 related tables with Micronaut but maybe it’s just something I am doing wrong. Could you please advise?
I have a parent entity
@Entity
public class ManagedComponentInstance {
@Id
@AutoPopulated
private UUID id;
@OneToMany(mappedBy = "managedComponentInstance",
cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<ManagedComponentInstanceLabel> labels;
...
}
and a child entity
@Entity
public class ManagedComponentInstanceLabel {
@Id
@AutoPopulated
private UUID id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "managed_component_instance_id")
private ManagedComponentInstance managedComponentInstance;
...
}
One Instance should have multiple Labels, so it’s one-to-many relation.
I was expecting that after adding Labels to Instance (via setLabels(listWithLabels)
) and calling InstanceRepository.save(), that Micronaut Data would also save labels. This is not the case, however. I have tried saving labels prior to saving instance but they are saved with managed_component_instance_id == null even though I do set parent on the entity before saving.
I’ve also tried using Micronaut annotations but the behaviour was the same.
@MappedEntity
public class ManagedComponentInstance {
@Id
@AutoPopulated
private UUID id;
// @Relation(value = Relation.Kind.ONE_TO_MANY, mappedBy = "managedComponentInstance")
@Relation(value = Relation.Kind.ONE_TO_MANY)
private List<ManagedComponentInstanceLabel> labels;
...
}
@MappedEntity
public class ManagedComponentInstanceLabel {
@Id
@AutoPopulated
private UUID id;
@MappedProperty(value = "managed_component_instance_id")
@Relation(Relation.Kind.MANY_TO_ONE)
private ManagedComponentInstance managedComponentInstance;
...
}
I am using Micronaut Data JDBC.
Is cascading saves like this unsupported? If it should work, is there a problem with my code above? There are some examples in documentation but a complete working example would be nice.
Thanks.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:9 (4 by maintainers)
@graemerocher I added #815
Basic support for cascading saves if you declare
Has been added in master. Cascading deletes have to be done manually still, that is probably a separate issue.