Refactoring : JoinedSubclassDiscriminatorMappingImpl and EntityDiscriminatorMappingImpl

This commit is contained in:
Andrea Boriero 2019-11-08 13:55:36 +00:00
parent ebb3e36db6
commit 6b1c5c8c11
4 changed files with 159 additions and 138 deletions

View File

@ -0,0 +1,147 @@
/*
* 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.metamodel.mapping.internal;
import org.hibernate.LockMode;
import org.hibernate.engine.FetchStrategy;
import org.hibernate.engine.FetchTiming;
import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.MappingType;
import org.hibernate.metamodel.model.convert.spi.BasicValueConverter;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.query.sqm.sql.SqlAstCreationState;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.results.internal.domain.basic.BasicFetch;
import org.hibernate.sql.results.internal.domain.basic.BasicResult;
import org.hibernate.sql.results.spi.DomainResult;
import org.hibernate.sql.results.spi.DomainResultCreationState;
import org.hibernate.sql.results.spi.Fetch;
import org.hibernate.sql.results.spi.FetchParent;
import org.hibernate.type.BasicType;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
/**
* @author Andrea Boriero
*/
public abstract class AbstractEntityDiscriminatorMapping implements EntityDiscriminatorMapping {
private final EntityPersister entityDescriptor;
private final String tableExpression;
private final String mappedColumnExpression;
private final BasicType mappingType;
public AbstractEntityDiscriminatorMapping(
EntityPersister entityDescriptor,
String tableExpression,
String mappedColumnExpression,
BasicType mappingType) {
this.entityDescriptor = entityDescriptor;
this.tableExpression = tableExpression;
this.mappedColumnExpression = mappedColumnExpression;
this.mappingType = mappingType;
}
@Override
public String getContainingTableExpression() {
return tableExpression;
}
@Override
public String getMappedColumnExpression() {
return mappedColumnExpression;
}
@Override
public BasicValueConverter getConverter() {
return null;
}
@Override
public String getFetchableName() {
return ROLE_NAME;
}
@Override
public FetchStrategy getMappedFetchStrategy() {
return FetchStrategy.IMMEDIATE_JOIN;
}
@Override
public <T> DomainResult<T> createDomainResult(
NavigablePath navigablePath,
TableGroup tableGroup,
String resultVariable,
DomainResultCreationState creationState) {
final SqlSelection sqlSelection = resolveSqlSelection( tableGroup, creationState );
//noinspection unchecked
return new BasicResult(
sqlSelection.getValuesArrayPosition(),
resultVariable,
getJavaTypeDescriptor(),
navigablePath
);
}
@Override
public void applySqlSelections(
NavigablePath navigablePath,
TableGroup tableGroup,
DomainResultCreationState creationState) {
resolveSqlSelection( tableGroup, creationState );
}
@Override
public Fetch generateFetch(
FetchParent fetchParent,
NavigablePath fetchablePath,
FetchTiming fetchTiming,
boolean selected,
LockMode lockMode,
String resultVariable,
DomainResultCreationState creationState) {
final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup(
fetchParent.getNavigablePath()
);
assert tableGroup != null;
final SqlSelection sqlSelection = resolveSqlSelection( tableGroup, creationState );
return new BasicFetch(
sqlSelection.getValuesArrayPosition(),
fetchParent,
fetchablePath,
this,
false,
getConverter(),
fetchTiming,
creationState
);
}
@Override
public JavaTypeDescriptor getJavaTypeDescriptor() {
return getMappedTypeDescriptor().getMappedJavaTypeDescriptor();
}
@Override
public MappingType getMappedTypeDescriptor() {
return mappingType;
}
@Override
public JdbcMapping getJdbcMapping() {
return mappingType.getJdbcMapping();
}
protected abstract SqlSelection resolveSqlSelection(TableGroup tableGroup, DomainResultCreationState creationState);
}

View File

@ -6,141 +6,32 @@
*/
package org.hibernate.metamodel.mapping.internal;
import org.hibernate.LockMode;
import org.hibernate.engine.FetchStrategy;
import org.hibernate.engine.FetchTiming;
import org.hibernate.metamodel.mapping.EntityDiscriminatorMapping;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.MappingType;
import org.hibernate.metamodel.model.convert.spi.BasicValueConverter;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.query.sqm.sql.SqlExpressionResolver;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.expression.ColumnReference;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.results.internal.domain.basic.BasicFetch;
import org.hibernate.sql.results.internal.domain.basic.BasicResult;
import org.hibernate.sql.results.spi.DomainResult;
import org.hibernate.sql.results.spi.DomainResultCreationState;
import org.hibernate.sql.results.spi.Fetch;
import org.hibernate.sql.results.spi.FetchParent;
import org.hibernate.type.BasicType;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
/**
* @author Steve Ebersole
*/
public class EntityDiscriminatorMappingImpl implements EntityDiscriminatorMapping {
private final EntityPersister entityPersister;
private final String tableExpression;
private String mappedColumnExpression;
private final BasicType mappingType;
public class EntityDiscriminatorMappingImpl extends AbstractEntityDiscriminatorMapping {
public EntityDiscriminatorMappingImpl(
EntityPersister entityPersister,
EntityPersister entityDescriptor,
String tableExpression,
String mappedColumnExpression,
BasicType mappingType) {
this( entityPersister, tableExpression, mappingType );
this.mappedColumnExpression = mappedColumnExpression;
}
public EntityDiscriminatorMappingImpl(
EntityPersister entityPersister,
String tableExpression,
BasicType mappingType) {
this.entityPersister = entityPersister;
this.tableExpression = tableExpression;
this.mappingType = mappingType;
super( entityDescriptor, tableExpression, mappedColumnExpression, mappingType );
}
@Override
public String getContainingTableExpression() {
return tableExpression;
}
@Override
public String getMappedColumnExpression() {
return mappedColumnExpression;
}
@Override
public BasicValueConverter getConverter() {
return null;
}
@Override
public String getFetchableName() {
return ROLE_NAME;
}
@Override
public FetchStrategy getMappedFetchStrategy() {
return FetchStrategy.IMMEDIATE_JOIN;
}
@Override
public <T> DomainResult<T> createDomainResult(
NavigablePath navigablePath,
TableGroup tableGroup,
String resultVariable,
DomainResultCreationState creationState) {
final SqlSelection sqlSelection = resolveSqlSelection( tableGroup, creationState );
//noinspection unchecked
return new BasicResult(
sqlSelection.getValuesArrayPosition(),
resultVariable,
getJavaTypeDescriptor(),
navigablePath
);
}
@Override
public void applySqlSelections(
NavigablePath navigablePath,
TableGroup tableGroup,
DomainResultCreationState creationState) {
resolveSqlSelection( tableGroup, creationState );
}
@Override
public Fetch generateFetch(
FetchParent fetchParent,
NavigablePath fetchablePath,
FetchTiming fetchTiming,
boolean selected,
LockMode lockMode,
String resultVariable,
DomainResultCreationState creationState) {
final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup(
fetchParent.getNavigablePath()
);
assert tableGroup != null;
final SqlSelection sqlSelection = resolveSqlSelection( tableGroup, creationState );
return new BasicFetch(
sqlSelection.getValuesArrayPosition(),
fetchParent,
fetchablePath,
this,
false,
getConverter(),
fetchTiming,
creationState
);
}
protected SqlSelection resolveSqlSelection(TableGroup tableGroup, DomainResultCreationState creationState) {
final SqlExpressionResolver expressionResolver = creationState.getSqlAstCreationState().getSqlExpressionResolver();
final SqlExpressionResolver expressionResolver = creationState.getSqlAstCreationState()
.getSqlExpressionResolver();
final TableReference tableReference = tableGroup.resolveTableReference( getContainingTableExpression() );
@ -153,7 +44,7 @@ public class EntityDiscriminatorMappingImpl implements EntityDiscriminatorMappin
sqlAstProcessingState -> new ColumnReference(
tableReference.getIdentificationVariable(),
getMappedColumnExpression(),
mappingType.getJdbcMapping(),
getJdbcMapping(),
creationState.getSqlAstCreationState().getCreationContext().getSessionFactory()
)
),
@ -161,23 +52,4 @@ public class EntityDiscriminatorMappingImpl implements EntityDiscriminatorMappin
creationState.getSqlAstCreationState().getCreationContext().getDomainModel().getTypeConfiguration()
);
}
@Override
public JavaTypeDescriptor getJavaTypeDescriptor() {
return getMappedTypeDescriptor().getMappedJavaTypeDescriptor();
}
@Override
public MappingType getMappedTypeDescriptor() {
return mappingType;
}
@Override
public JdbcMapping getJdbcMapping() {
return mappingType.getJdbcMapping();
}
protected EntityPersister getEntityPersister(){
return entityPersister;
}
}

View File

@ -20,7 +20,7 @@ import org.hibernate.type.BasicType;
/**
* @author Andrea Boriero
*/
public class JoinedSubclassDiscriminatorMappingImpl extends EntityDiscriminatorMappingImpl {
public class JoinedSubclassDiscriminatorMappingImpl extends AbstractEntityDiscriminatorMapping {
private final CaseSearchedExpression caseSearchedExpression;
private final List<ColumnReference> columnReferences;
@ -28,12 +28,13 @@ public class JoinedSubclassDiscriminatorMappingImpl extends EntityDiscriminatorM
public JoinedSubclassDiscriminatorMappingImpl(
EntityPersister entityDescriptor,
String tableExpression,
String mappedColumExpression,
CaseSearchedExpression caseSearchedExpression,
List<ColumnReference> columnReferences,
BasicType mappingType) {
super( entityDescriptor, tableExpression, mappingType );
this.caseSearchedExpression = caseSearchedExpression;
super( entityDescriptor, tableExpression, mappedColumExpression, mappingType );
this.caseSearchedExpression = caseSearchedExpression;
this.columnReferences = columnReferences;
}

View File

@ -1240,6 +1240,7 @@ public class JoinedSubclassEntityPersister extends AbstractEntityPersister {
return new JoinedSubclassDiscriminatorMappingImpl(
this,
getRootTableName(),
getDiscriminatorColumnName(),
info.caseSearchedExpression,
info.columnReferences,
(BasicType) getDiscriminatorType()