|
43 | 43 | import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping; |
44 | 44 | import org.hibernate.persister.entity.EntityPersister; |
45 | 45 | import org.hibernate.query.spi.QueryOptions; |
| 46 | +import org.hibernate.sql.ast.tree.AbstractUpdateOrDeleteStatement; |
46 | 47 | import org.hibernate.sql.ast.tree.delete.DeleteStatement; |
47 | 48 | import org.hibernate.sql.ast.tree.from.NamedTableReference; |
48 | 49 | import org.hibernate.sql.ast.tree.from.TableReference; |
49 | 50 | import org.hibernate.sql.ast.tree.predicate.Predicate; |
| 51 | +import org.hibernate.sql.ast.tree.update.Assignment; |
| 52 | +import org.hibernate.sql.ast.tree.update.UpdateStatement; |
50 | 53 | import org.hibernate.sql.exec.spi.JdbcOperationQueryMutation; |
51 | 54 | import org.hibernate.sql.exec.spi.JdbcParameterBindings; |
52 | 55 |
|
| 56 | +import static java.util.Collections.singletonList; |
| 57 | + |
53 | 58 | /** |
54 | 59 | * @author Steve Ebersole |
55 | 60 | */ |
@@ -171,31 +176,42 @@ private static void visitCollectionTableDeletes( |
171 | 176 | QueryOptions queryOptions, |
172 | 177 | Consumer<JdbcOperationQueryMutation> jdbcOperationConsumer) { |
173 | 178 | final String separateCollectionTable = attributeMapping.getSeparateCollectionTable(); |
| 179 | + // Skip deleting rows in collection tables if cascade delete is enabled |
| 180 | + if ( separateCollectionTable == null || attributeMapping.getCollectionDescriptor().isCascadeDeleteEnabled() ) { |
| 181 | + return; |
| 182 | + } |
174 | 183 | final SessionFactoryImplementor sessionFactory = attributeMapping.getCollectionDescriptor().getFactory(); |
175 | 184 | final JdbcServices jdbcServices = sessionFactory.getJdbcServices(); |
| 185 | + // element-collection or many-to-many - delete the collection-table row |
| 186 | + final NamedTableReference tableReference = new NamedTableReference( |
| 187 | + separateCollectionTable, |
| 188 | + DeleteStatement.DEFAULT_ALIAS, |
| 189 | + true |
| 190 | + ); |
176 | 191 |
|
177 | | - // Skip deleting rows in collection tables if cascade delete is enabled |
178 | | - if ( separateCollectionTable != null && !attributeMapping.getCollectionDescriptor().isCascadeDeleteEnabled() ) { |
179 | | - // element-collection or many-to-many - delete the collection-table row |
180 | | - |
181 | | - final NamedTableReference tableReference = new NamedTableReference( |
182 | | - separateCollectionTable, |
183 | | - DeleteStatement.DEFAULT_ALIAS, |
184 | | - true |
185 | | - ); |
186 | | - |
187 | | - final DeleteStatement sqlAstDelete = new DeleteStatement( |
188 | | - tableReference, |
189 | | - restrictionProducer.apply( tableReference, attributeMapping ) |
| 192 | + final AbstractUpdateOrDeleteStatement sqlAst; |
| 193 | + if ( attributeMapping.getSoftDeleteMapping() != null ) { |
| 194 | + final Assignment softDeleteAssignment = attributeMapping |
| 195 | + .getSoftDeleteMapping() |
| 196 | + .createSoftDeleteAssignment( tableReference ); |
| 197 | + sqlAst = new UpdateStatement( |
| 198 | + tableReference, |
| 199 | + singletonList( softDeleteAssignment ), |
| 200 | + restrictionProducer.apply( tableReference, attributeMapping ) |
190 | 201 | ); |
191 | | - |
192 | | - jdbcOperationConsumer.accept( |
193 | | - jdbcServices.getJdbcEnvironment() |
194 | | - .getSqlAstTranslatorFactory() |
195 | | - .buildMutationTranslator( sessionFactory, sqlAstDelete ) |
196 | | - .translate( jdbcParameterBindings, queryOptions ) |
| 202 | + } |
| 203 | + else { |
| 204 | + sqlAst = new DeleteStatement( |
| 205 | + tableReference, |
| 206 | + restrictionProducer.apply( tableReference, attributeMapping ) |
197 | 207 | ); |
198 | 208 | } |
| 209 | + jdbcOperationConsumer.accept( |
| 210 | + jdbcServices.getJdbcEnvironment() |
| 211 | + .getSqlAstTranslatorFactory() |
| 212 | + .buildMutationTranslator( sessionFactory, sqlAst ) |
| 213 | + .translate( jdbcParameterBindings, queryOptions ) |
| 214 | + ); |
199 | 215 | } |
200 | 216 |
|
201 | 217 | public static boolean isId(JdbcMappingContainer type) { |
|
0 commit comments