question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support cascading save in Micronaut JDBC

See original GitHub issue

Hi, 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:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
matt-snidercommented, Nov 15, 2020
1reaction
graemerochercommented, Oct 29, 2019

Basic support for cascading saves if you declare

@OneToMany(cascade = CascadeType.PERSIST)
    private List<Page> pages = new ArrayList<>();

Has been added in master. Cascading deletes have to be done manually still, that is probably a separate issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Micronaut Data's cascade persist doesn't save child objects
The cause of this issue was that IDs was defined as primitives instead of object Integer. After change to private Integer id; it...
Read more >
Micronaut Data - GitHub Pages
Micronaut Data JDBC / R2DBC supports all the features of Micronaut Data for JPA including dynamic finders, pagination, projections, Data Transfer Objects (DTO), ......
Read more >
Management & Monitoring - Micronaut Documentation
Micronaut is a modern, JVM-based, full stack Java framework designed for building modular, easily testable JVM applications with support for Java, Kotlin, and ......
Read more >
Eager Initialization of Singletons - Micronaut Documentation
Improved support for JDBC / Hibernate in Native Image ... @Transactional public Genre save(@NotBlank String name) { ... .. } } becomes:.
Read more >
Access a database with Micronaut Data JDBC
A repository interface for performing CRUD (Create, Read, Update, Delete). It provides methods such as findAll() , save(Genre) , deleteById(Long) , and findById ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found