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

DeleteAll after Spring Boot 3.4 update creates TransientObjectException #3737

Closed
rvp-c opened this issue Jan 8, 2025 · 2 comments
Closed
Labels
for: external-project For an external project and not something we can fix

Comments

@rvp-c
Copy link

rvp-c commented Jan 8, 2025

Sorry. I have generate first a stackoverflow article, but I think because it's probably a bug I will reported it here also.
https://stackoverflow.com/questions/79336531/deleteall-after-spring-boot-3-4-update-creates-transientobjectexception

We try to update our application to Spring Boot 3.4 and have with this some problems. One of it let's failed some of our integration tests. Because our application has a little complicated model I generate a small example which shows the problem.

https://github.com/rvp-c/springboot34_deletecascade

Some explaination for the model we use. We have to objects - DocumentationUnitDTO and ProcedureDTO which use a many-to-many relation. So far so easy. Problem the main object (DocumentationUnitDTO) needs an order. This we have to put into the join table (DocumentationUnitProcedureDTO).

The problems seems to be in the ProcedureDTO which have a reference to the DocumentationUnitDTO which is annotated with:

@ManyToMany(fetch = FetchType.EAGER)
  @JoinTable(
      name = "documentation_unit_procedure",
      inverseJoinColumns = @JoinColumn(name = "documentation_unit_id"),
      joinColumns = @JoinColumn(name = "procedure_id"))

The generation works fine. But the deletion seems to do a cascade from DocumentationUnitDTO -> ProcedureDTO -> DocumentationUnitDTO. (debugging of the exception)

Same code works with spring boot 3.3 without errors.

What did I try

  • If I make the property @Transient everything works fine.
  • Try the same with hibernate 6.6 and it works fine
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 8, 2025
@mp911de
Copy link
Member

mp911de commented Jan 9, 2025

I'm able to reproduce the issue by using the entity manager directly:

@AfterEach
void tearDown() {

	EntityManager entityManager = emf.createEntityManager();
	entityManager.getTransaction().begin();

	List resultList = entityManager.createQuery("FROM DocumentationUnitDTO").getResultList();

	for (Object o : resultList) {
		entityManager.remove(o);
	}
	entityManager.getTransaction().commit();
	entityManager.close();
}

@Test
void test_generateAnObjectAndDeleteItInTearDown() {
	DocumentationUnitDTO documentationUnit = new DocumentationUnitDTO();

	EntityManager entityManager = emf.createEntityManager();
	entityManager.getTransaction().begin();

	entityManager.persist(documentationUnit);
	entityManager.getTransaction().commit();

	ProcedureDTO procedure = new ProcedureDTO();

	entityManager.getTransaction().begin();

	entityManager.persist(procedure);
	entityManager.getTransaction().commit();

	DocumentationUnitProcedureDTO documentationUnitProcedure = new DocumentationUnitProcedureDTO();
	DocumentationUnitProcedureId primaryKey = new DocumentationUnitProcedureId(documentationUnit.getId(),
			procedure.getId());
	primaryKey.setRank(1);
	documentationUnitProcedure.setPrimaryKey(primaryKey);
	documentationUnitProcedure.setDocumentationUnit(documentationUnit);
	documentationUnitProcedure.setProcedure(procedure);

	documentationUnit.getProcedures().add(documentationUnitProcedure);


	entityManager.getTransaction().begin();

	entityManager.persist(documentationUnit);
	entityManager.getTransaction().commit();
	entityManager.close();
}

However, if you run your test method enclosed in a transaction (e.g. annotating the test method with @Transactional), then Hibernate is cleaning up transient instances properly. The code opens a new transaction for each repository interaction otherwise.

Downgrading to Hibernate 6.5 causes the issue to also disappear. I consider this a Hibernate bug worth reporting to the Hibernate team.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2025
@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 Jan 9, 2025
@rvp-c
Copy link
Author

rvp-c commented Jan 13, 2025

Thanks!

I look for the error in my hibernate test (I do this also and I don't get an error.)
Somehow I add in the test method in another transaction a remove of the entity and than the teardown has nothing to do.
Means the error only appears with different entity managers.

I will report it to the hibernate team.
https://hibernate.atlassian.net/browse/HHH-19037

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

3 participants