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.

ENTITY_INSTANCE_WITH_NULL_ID exception on cascading save of entity with new child

See original GitHub issue

I’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:closed
  • Created 8 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
gimohcommented, Oct 3, 2017

Unfortunately using saveAndFlush doesn’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’s persist() method which updates objects in-place (so both parent and nested objects get IDs populated). When updating an existing entity it fires merge() 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?

1reaction
esmelulitaonlycommented, Jun 15, 2016

Good news! I could solve the problem using “saveAndFlush()” instead of “save()”

    @Transactional
    public Cotizacion saveQuotationDraft(Cotizacion quotation) throws ServletException, IOException {

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();                       
        Usuario executive = usuarioRepository.findByUsername(auth.getName());
        quotation.setEjecutivo(executive);

        if (quotation.getCotizacionAseguradoras() != null) {
            cotizacionAseguradoraAuditory.finishEntity(quotation.getCotizacionAseguradoras(), true);
        }

        if (quotation.getRamos() != null) {
            for (CotizacionRamo branch : quotation.getRamos()) {
                cotizacionRamoAuditory.finishEntity(branch, true);

            }
        }

        cotizacionAuditory.finishEntity(quotation, true);
        quotation = cotizacionRepository.saveAndFlush(quotation);

        String numberQuotation = "COT" + String.format("%010d", Integer.parseInt(quotation.getId().toString()));
        quotation.setNumero(numberQuotation);
        quotation = cotizacionRepository.saveAndFlush(quotation);

        return quotation;
    }

saveAndFlush() belongs to org.springframework.data.jpa.repository.JpaRepository package, and my repositories extend of JpaRepository instead CrudRepository.

Read more comments on GitHub >

github_iconTop 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 >

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