Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,8 @@ private static void visitCollectionTableDeletes(
final SessionFactoryImplementor sessionFactory = attributeMapping.getCollectionDescriptor().getFactory();
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();

if ( separateCollectionTable == null ) {
// one-to-many - update the matching rows in the associated table setting the fk column(s) to null
// not yet implemented - do nothing
}
else {
// Skip deleting rows in collection tables if cascade delete is enabled
if ( separateCollectionTable != null && !attributeMapping.getCollectionDescriptor().isCascadeDeleteEnabled() ) {
// element-collection or many-to-many - delete the collection-table row

final NamedTableReference tableReference = new NamedTableReference(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ protected void addDmlCtes(
SqmMutationStrategyHelper.visitCollectionTables(
(EntityMappingType) mutatingTableGroup.getModelPart(),
pluralAttribute -> {
if ( pluralAttribute.getSeparateCollectionTable() != null ) {
// Skip deleting rows in collection tables if cascade delete is enabled
if ( pluralAttribute.getSeparateCollectionTable() != null
&& !pluralAttribute.getCollectionDescriptor().isCascadeDeleteEnabled() ) {
// Ensure that the FK target columns are available
final boolean useFkTarget = !pluralAttribute.getKeyDescriptor()
.getTargetPart().isEntityIdentifierMapping();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ protected InlineDeleteHandler(
SqmMutationStrategyHelper.visitCollectionTables(
getEntityDescriptor(),
pluralAttribute -> {
if ( pluralAttribute.getSeparateCollectionTable() != null ) {
// Skip deleting rows in collection tables if cascade delete is enabled
if ( pluralAttribute.getSeparateCollectionTable() != null
&& !pluralAttribute.getCollectionDescriptor().isCascadeDeleteEnabled() ) {
// this collection has a separate collection table, meaning it is one of:
// 1) element-collection
// 2) many-to-many
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import org.hibernate.Hibernate;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;

Expand All @@ -26,10 +25,12 @@
@Jpa(annotatedClasses =
{OnDeleteCollectionTest.A.class},
useCollectingStatementInspector = true)
//@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsCascadeDeleteCheck.class)
class OnDeleteCollectionTest {
@Test void test(EntityManagerFactoryScope scope) {
@Test
void test(EntityManagerFactoryScope scope) {
var inspector = scope.getCollectingStatementInspector();
inspector.clear();

scope.inTransaction( em -> {
A a = new A();
a.id = 2;
Expand Down Expand Up @@ -66,8 +67,38 @@ class OnDeleteCollectionTest {
});
}

@Test
@Jira("https://hibernate.atlassian.net/browse/HHH-19730")
void testBulk(EntityManagerFactoryScope scope) {
var inspector = scope.getCollectingStatementInspector();
inspector.clear();

scope.inTransaction( em -> {
A a = new A();
a.id = 2;
a.bs.add( "b" );
em.persist( a );
} );
inspector.assertExecutedCount( 2 );
inspector.clear();

scope.inTransaction( em -> {
em.createQuery( "delete from A" ).executeUpdate();
inspector.assertExecutedCount( scope.getDialect().supportsCascadeDelete() ? 1 : 2 );
} );
inspector.clear();

scope.inTransaction( em -> {
assertEquals( 0,
em.createNativeQuery( "select count(*) from A_bs", Integer.class )
.getSingleResultOrNull() );
assertEquals( 0,
em.createNativeQuery( "select count(*) from A", Integer.class )
.getSingleResultOrNull() );
});
}

@Entity(name = "A")
@Inheritance(strategy = InheritanceType.JOINED)
static class A {
@Id
long id;
Expand Down
Loading