Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction commit fails with TransientObjectException: persistent instance references an unsaved transient instance #3712

Closed
tcordel opened this issue Dec 12, 2024 · 5 comments
Labels
for: external-project For an external project and not something we can fix

Comments

@tcordel
Copy link

tcordel commented Dec 12, 2024

Hi,

Overview

We met an unexpected error upgrading to spring-boot 3.4.0

I'm not sure this bug deals with spring-data, but i have not been able to reproduce the default using hibernate directly ; maybe you may provide new directions of investigations...

Here is a sample project representing the default with the minimal configuration needed to reproduce it : https://github.com/tcordel/jpa-named-query/tree/main

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: persistent instance references an unsaved transient instance of 'com.example.demo.entities.UserNotifEntity' (save the transient instance before flushing)
   	at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:368)
   	at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
   	at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:566)

Details

This issue appears only if the optional fetch is done here : NotificationService

Entities

classDiagram
    class ApplicationEntity {
          
    }
    class UserEntity {

    }
    class TypeNotificationEntity{

    }
    class UserNotifEntity {
        +UserEntity User
        +TypeNotificationEntity typeNotif
    }
    class UserNotifAppliEntity {
        +UserNotifEntity UserNotif
        +ApplicationEntity application
    }
Loading

Tests

List of the tests present in DemoApplicationTests :

Thanks for your time :)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 12, 2024
@tcordel tcordel changed the title Unexpected behaviour with named queries Unexpected behaviour with named queries (spring-boot 3.4.0) Dec 12, 2024
@quaff
Copy link
Contributor

quaff commented Dec 13, 2024

I'm pretty sure it caused by bug fix hibernate/hibernate-orm@055570c, I'm not sure you are using in correct way, I didn't dig too much because the code is a bit hard to read.

@mp911de mp911de added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 13, 2024
@mp911de mp911de changed the title Unexpected behaviour with named queries (spring-boot 3.4.0) Transaction commit fails with TransientObjectException: persistent instance references an unsaved transient instance Dec 13, 2024
@mp911de
Copy link
Member

mp911de commented Dec 13, 2024

I can reproduce the issue with using the EntityManager:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<UserNotifEntity> criteriaDelete = cb.createQuery(UserNotifEntity.class);
Root<UserNotifEntity> from = criteriaDelete.from(UserNotifEntity.class);
criteriaDelete.where(cb.equal(from.get("user"), user), cb.equal(from.get("typeNotif"), typeNotif));
Query query = entityManager.createQuery(criteriaDelete);
List resultList = query.getResultList();
for (Object o : resultList) {
	entityManager.remove(o);
}

On the flip side, using a CriteriaDelete or a JPQL delete query doesn't show this behavior. That being said, I consider the issue a Hibernate problem and there is nothing we can do about.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2024
@tcordel
Copy link
Author

tcordel commented Dec 13, 2024

Nice thank you!

tcordel pushed a commit to tcordel/hibernate-orm that referenced this issue Dec 13, 2024
Commiting a transaction in which a deletion has been made on an entitu
referenced by others element in cache results in an exception.

may be related to #055570c8af7c8455d6ae318d589c47441c55850c

This issue was initialy raised in spring-data-jpa but then redirected to
hibernate (spring-projects/spring-data-jpa#3712)
tcordel pushed a commit to tcordel/hibernate-orm that referenced this issue Dec 13, 2024
Commiting a transaction in which a deletion has been made on an entitu
referenced by others element in cache results in an exception.

may be related to #055570c8af7c8455d6ae318d589c47441c55850c

This issue was initialy raised in spring-data-jpa but then redirected to
hibernate (spring-projects/spring-data-jpa#3712)
@palhoye
Copy link

palhoye commented Dec 27, 2024

This issue was introduced with Hibernate 6.6.
Hibernate 6.5.3 does not have this issue.
Short term work-around is to specify Hibernate dependency as 6.5.3.Final.

@tcordel
Copy link
Author

tcordel commented Dec 27, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

5 participants