Working support for simple HQL DELETE -> execution!!

This commit is contained in:
Steve Ebersole 2019-11-08 16:26:10 -06:00
parent 30ad3eabe5
commit e6895c3d91
6 changed files with 72 additions and 7 deletions

View File

@ -166,6 +166,7 @@ import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.property.access.spi.Setter;
import org.hibernate.query.ComparisonOperator;
import org.hibernate.query.NavigablePath;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.Alias;
import org.hibernate.sql.Delete;
@ -237,7 +238,7 @@ public abstract class AbstractEntityPersister
private final MultiIdEntityLoader multiIdEntityLoader;
private final NaturalIdLoader naturalIdLoader;
private SqmMultiTableMutationStrategy sqmMultiTableMutationStrategy;
@ -6184,6 +6185,21 @@ public abstract class AbstractEntityPersister
else {
accessOptimizer = null;
}
final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
if ( isMultiTable() ) {
sqmMultiTableMutationStrategy = null;
//sessionFactory.getJdbcServices().getJdbcEnvironment().getDialect().getFallbackSqmMutationStrategy( this )
}
else {
sqmMultiTableMutationStrategy = null;
}
}
@Override
public SqmMultiTableMutationStrategy getSqmMultiTableMutationStrategy() {
return sqmMultiTableMutationStrategy;
}
protected int getStateArrayInitialPosition(MappingModelCreationProcess creationProcess) {

View File

@ -166,7 +166,7 @@ public interface EntityPersister extends EntityDefinition, EntityValuedModelPart
* has multiple tables. Returns {@code null} to indicate that the entity
* does not define multiple tables
*/
default SqmMultiTableMutationStrategy getSqmMultiTableMutationStrategy(){
default SqmMultiTableMutationStrategy getSqmMultiTableMutationStrategy() {
throw new NotYetImplementedFor6Exception( getClass() );
}

View File

@ -32,7 +32,9 @@ public class StandardSqlAstDeleteTranslator
appendSql( "delete from " );
appendSql( sqlAst.getTargetTable().getTableExpression() );
if ( sqlAst.getRestriction() != null ) {
sqlAst.getRestriction().accept( this );
}
return new JdbcDelete() {
@Override

View File

@ -45,6 +45,8 @@ public class StandardJdbcMutationExecutor implements JdbcMutationExecutor {
final String sql = jdbcMutation.getSql();
try {
jdbcServices.getSqlStatementLogger().logStatement( sql );
// prepare the query
final PreparedStatement preparedStatement = statementCreator.apply( sql );
@ -64,10 +66,17 @@ public class StandardJdbcMutationExecutor implements JdbcMutationExecutor {
executionContext
);
}
executionContext.getSession().getEventListenerManager().jdbcExecuteStatementStart();
try {
int rows = preparedStatement.executeUpdate();
expectationCheck.accept( rows, preparedStatement );
return rows;
}
finally {
executionContext.getSession().getEventListenerManager().jdbcExecuteStatementEnd();
}
}
finally {
logicalConnection.getResourceRegistry().release( preparedStatement );
}

View File

@ -97,7 +97,13 @@ public class DeferredResultSetAccess extends AbstractResultSetAccess {
);
}
executionContext.getSession().getEventListenerManager().jdbcExecuteStatementStart();
try {
resultSet = preparedStatement.executeQuery();
}
finally {
executionContext.getSession().getEventListenerManager().jdbcExecuteStatementEnd();
}
logicalConnection.getResourceRegistry().register( resultSet, preparedStatement );
}

View File

@ -0,0 +1,32 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.orm.test.sql.exec;
import org.hibernate.orm.test.metamodel.mapping.PluralAttributeTests;
import org.hibernate.testing.orm.domain.StandardDomainModel;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
/**
* @author Steve Ebersole
*/
@SuppressWarnings("WeakerAccess")
@DomainModel( standardModels = StandardDomainModel.GAMBIT )
@ServiceRegistry
@SessionFactory( exportSchema = true )
public class HqlDeleteExecutionTests {
@Test
public void testSimpleDelete(SessionFactoryScope scope) {
scope.inTransaction(
session -> session.createQuery( "delete BasicEntity" ).executeUpdate()
);
}
}