jamesfredley commented on code in PR #16757: URL: https://github.com/apache/grails-core/pull/16757#discussion_r4369042244
########## grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/persister/JoinTableCollectionPersister.java: ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.persister; + +import org.hibernate.MappingException; +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.access.CollectionDataAccess; +import org.hibernate.engine.jdbc.mutation.JdbcValueBindings; +import org.hibernate.engine.jdbc.mutation.ParameterUsage; +import org.hibernate.engine.jdbc.mutation.internal.MutationQueryOptions; +import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.mapping.Collection; +import org.hibernate.metamodel.mapping.PluralAttributeMapping; +import org.hibernate.metamodel.spi.RuntimeModelCreationContext; +import org.hibernate.persister.collection.BasicCollectionPersister; +import org.hibernate.persister.collection.mutation.RowMutationOperations; +import org.hibernate.sql.model.ast.MutatingTableReference; +import org.hibernate.sql.model.ast.builder.CollectionRowDeleteBuilder; +import org.hibernate.sql.model.jdbc.JdbcMutationOperation; + +import static org.hibernate.persister.collection.mutation.RowMutationOperations.DEFAULT_RESTRICTOR; + +/** + * The persister of a collection of entities in a join table that deletes the row of one removed element by the + * owner's key and the element's foreign key. + * + * <p>Hibernate deletes a row of a set or a bag by the key and only the element columns that are not nullable. GORM + * leaves the element column of a join table nullable, and it has to where several collections share the table with a + * column each, so removing one element would delete every row of the owner's collection. A row of a collection of + * entities always has its element, so this persister restricts the delete by the element columns, nullable or not. + * {@link GormPersisterClassResolver} gives it to sets and bags of entities; lists and maps delete a row by their + * index.</p> + * + * @since 8.0.1 + */ +public class JoinTableCollectionPersister extends BasicCollectionPersister { + + /** Creates the persister, with the signature Hibernate calls a collection persister class with. */ + public JoinTableCollectionPersister( + Collection collectionBinding, + CollectionDataAccess cacheAccessStrategy, + RuntimeModelCreationContext creationContext) throws MappingException, CacheException { + super(collectionBinding, cacheAccessStrategy, creationContext); + } + + @Override + protected RowMutationOperations buildRowMutationOperations() { + RowMutationOperations hibernate = super.buildRowMutationOperations(); + if (!hibernate.hasDeleteRow()) { + return hibernate; + } + return new RowMutationOperations( + this, + hibernate.hasInsertRow() ? reference -> hibernate.getInsertRowOperation(reference.getTableMapping()) : null, + hibernate.getInsertRowValues(), + hibernate.hasUpdateRow() ? reference -> hibernate.getUpdateRowOperation() : null, + hibernate.getUpdateRowValues(), + hibernate.getUpdateRowRestrictions(), + this::deleteRowOperation, + (collection, key, element, rowPosition, session, bindings) -> + bindDeleteRowRestrictions(key, element, session, bindings), + hibernate.getDeleteAllRowsOperationProducer()); + } + + private JdbcMutationOperation deleteRowOperation(MutatingTableReference reference) { Review Comment: A JPA `@ManyToMany` with `@SoftDelete`, registered through `hibernate.annotatedClasses`, also gets this persister. The row delete is a physical `DELETE`. Hibernate would have updated the deletion marker. The retained row is gone. Keep Hibernate's soft-delete operation, or leave those mappings on Hibernate's persister. Please remove one element of a soft-deleted collection and assert the row is still there with the marker set. ########## grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/persister/GormPersisterClassResolver.java: ########## @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.orm.hibernate.persister; + +import org.hibernate.mapping.Collection; +import org.hibernate.mapping.IndexedCollection; +import org.hibernate.mapping.ManyToOne; +import org.hibernate.persister.collection.CollectionPersister; +import org.hibernate.persister.internal.StandardPersisterClassResolver; + +/** + * Resolves the persister of a set or a bag of entities in a join table to a {@link JoinTableCollectionPersister}, and + * every other persister the way Hibernate does. + * + * @since 8.0.1 + */ +public class GormPersisterClassResolver extends StandardPersisterClassResolver { + + @Override + public Class<? extends CollectionPersister> getCollectionPersisterClass(Collection metadata) { + if (!metadata.isOneToMany() && Review Comment: An `@CollectionId` bag is an `IdentifierBag`, not an `IndexedCollection`, so it takes this persister. Flush then tries to read a numeric row id as an entity and throws. Leave identifier bags on Hibernate's persister. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
