Skip to content

Commit

Permalink
tests: Transaction commit fails
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Thibault Cordel committed Dec 13, 2024
1 parent 611fb77 commit 5c81a82
Showing 1 changed file with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package org.hibernate.orm.test.jpa.cascade;

import jakarta.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.query.criteria.JpaCriteriaQuery;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import java.util.List;

@DomainModel(
annotatedClasses = {
DeleteCascadeTest.A.class,
DeleteCascadeTest.B.class,
DeleteCascadeTest.C.class,
}
)
@SessionFactory
class DeleteCascadeTest {

@Entity
public static class A {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}
}

@Entity
public static class B {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
@JoinColumn(name = "a_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private A a;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public A getA() {
return a;
}

public void setA(A a) {
this.a = a;
}

}

@Entity
public static class C {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
@JoinColumn(name = "b_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private B b;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public B getB() {
return b;
}

public void setB(B b) {
this.b = b;
}

}


@Test
void testRemove(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
A a = new A();
session.persist(a);

B bb = new B();
bb.setA(a);
session.persist(bb);

C c = new C();
c.setB(bb);
session.persist(c);
});
scope.inTransaction(
session -> {

HibernateCriteriaBuilder cb = session.getCriteriaBuilder();
JpaCriteriaQuery<C> cq = cb.createQuery(C.class);
cq.from(C.class);
JpaCriteriaQuery<B> bq = cb.createQuery(B.class);
bq.from(B.class);

// This is how spring-data-jpa manage delete* Named queries
session.createQuery(cq).getResultList();
List<B> bs = session.createQuery(bq).getResultList();
for (B b1 : bs) {
session.remove(b1);
}
});
}
}

0 comments on commit 5c81a82

Please sign in to comment.