ENTITY_INSTANCE_WITH_NULL_ID exception on cascading save of entity with new child
See original GitHub issueI’m using javers 1.3.17 with spring integration (spring boot + data jpa) I have a parent child relationship such as
@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Parent {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Child> children;
}
@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Child {
@Id
@GeneratedValue
private Long id;
private String name;
}
When I try to save an existing parent (with id) with a newly added child element (without id) I get the ENTITY_INSTANCE_WITH_NULL_ID. Is casading save with new child entities not supported by javers? I can only get it to work by manually persisting each new child before saving the parent (JPA can handle this just fine).
A full demo project can be found on github
Issue Analytics
- State:
- Created 8 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Spring Data JPA: Saving nested OneToMany children (with ...
I have two Model Objects: Parent and Child. The model classes are as follows: import javax.persistence.* @Entity @Table(name = "parents") ...
Read more >Deleting Objects with Hibernate - Baeldung
Quick guide to deleting an entity in Hibernate. ... By using EntityManager.remove; When a deletion is cascaded from other entity instances ...
Read more >FK null at time of saving parent - child table - CodeRanch
I have two entities: I understand the problem. The FK (name: LOGGINGEMPLOYEEMODEL_ID ) is in the tabel "loggingmodel". At the moment of new...
Read more >Tracking vs. No-Tracking Queries - EF Core | Microsoft Learn
Information on tracking and no-tracking queries in Entity Framework Core.
Read more >object is an unsaved transient instance - Appian Community
TransientObjectException : object is an unsaved transient instance - save the ... from DataStore entities" it works perfectly and delete also child data...
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

Unfortunately using
saveAndFlushdoesn’t really help as javers just doesn’t commit the change as it doesn’t fire after it (#427).I’ve dug into this and AFAICS this is because when creating a new entity the JPA repository’s
save()method will fire EntityManager’spersist()method which updates objects in-place (so both parent and nested objects get IDs populated). When updating an existing entity it firesmerge()method which doesn’t update objects in-place but returns a new managed object.Now IIUC because Javers gets triggered via an aspect, it gets reference to the original entity object which won’t be touched by
merge()so Javers sees NULL IDs. See also this SO answer.Any ideas if there’s anything that can be done to make this work?
Good news! I could solve the problem using “saveAndFlush()” instead of “save()”
saveAndFlush() belongs to org.springframework.data.jpa.repository.JpaRepository package, and my repositories extend of JpaRepository instead CrudRepository.