Cascading save/persist not working with @OneToOne
See original GitHub issuePersisting an entity that contains a @OneToOne
relationship to a child entity should generate insert statements for both the parent and child entities.
Task List
- Steps to reproduce provided
- Stacktrace (if present) provided
- Example that reproduces the problem uploaded to Github
- Full description of the issue provided (see below)
Steps to Reproduce
- Define the entities and repositories
@Entity
public class Face {
@GeneratedValue
@Id
private Long id;
@OneToOne(cascade = CascadeType.PERSIST)
private Nose nose;
// Getters, setters
}
@Entity
public class Nose {
@GeneratedValue
@Id
private Long id;
// Getters, setters
}
public interface FaceRepository extends CrudRepository<Face, Long> { }
public interface NoseRepository extends CrudRepository<Face, Long> { }
- Create and save entities
Face face = new Face("Bob");
Nose nose = new Nose();
face.setNose(nose);
Face savedFace = faceRepository.save(face);
assertNotNull(savedFace.nose);
Expected Behaviour
The child entity should be saved.
Actual Behaviour
The child entity is not saved.
Environment Information
- Operating System: NixOS 20.03
- Micronaut Version: 2.1.3
- JDK Version: 14
Example Application
I added a test case displaying this issue:
https://github.com/matt-snider/micronaut-data/commit/b4dc0a9027d55f55a890dadcf696af718e9a9ae2
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Cascade is not working in Hibernate One-to-One mapping
My requirement is to setup One-to-One mapping between Person and Passport. If person data gets deleted then it's associated passport should be ...
Read more >Micronaut Data - GitHub Pages
Since Micronaut Data is a build time tool, it will not work correctly unless your build is configured correctly. There are two important...
Read more >Hibernate @OneToOne Annotation - Coders Campus
We use the @OneToOne annotation with a cascade on the getter method for the employee object (not the employeeId ); Since the employeeId...
Read more >Hibernate JPA Cascade Type (ALL & PERSIST) Using Spring ...
... Cascade Type ALL & PERSIST using a Spring Boot ApplicationGit Repo - https://github.com/satishkumar11/Hibernate-JPA- Cascade -All-Persist.
Read more >Hibernate: save, persist, update, merge, saveOrUpdate
The notion of “persistence context” is Hibernate's solution to this problem. We can think of persistence context as a container or ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I had this but stepping through the micronaut data code, I hit this condition https://github.com/micronaut-projects/micronaut-data/blob/2.1.x/data-jdbc/src/main/java/io/micronaut/data/jdbc/operations/DefaultJdbcRepositoryOperations.java#L735
where it uses the
id
to check if it should insert it or not (which makes sense since no lazy loading/proxy objs), so cascading inserts only works in a world where ids are generated in some way, and not setup by the DAO/some other code itself it seems?@matt-snider I am unable to reproduce the issue as you described it.
However, from the test case you produced, the issue is not in the save method which looks to work correctly. The issue is that findById does not do a Join automatically to retrieve the entity. I think that this is by design as data-jdbc is not an ORM.
You can instruct micronaut data to do a join by decorating the findById method with @Join(“nose”). This should work