Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for Hibernate Reactive #9264

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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 @@ -111,6 +111,13 @@ public QualifiedName getPhysicalName() {
return physicalTableName;
}

/*
* Used by Hibernate Reactive
*/
public Identifier getLogicalValueColumnNameIdentifier() {
return logicalValueColumnNameIdentifier;
}

@Override
public int getInitialValue() {
return initialValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,38 @@ public abstract class AbstractEntityPersister
private List<UniqueKeyEntry> uniqueKeyEntries = null; //lazily initialized
private ConcurrentHashMap<String,SingleIdArrayLoadPlan> nonLazyPropertyLoadPlansByName;

/**
* A factory for the creation of an EntityMetamodel.
* <p>
* Hibernate Reactive uses it to pass its own entity metamodel class
* and adapt the identifier generators.
*/
public static class EntityMetamodelFactory {
public EntityMetamodel createEntityMetamodel(
PersistentClass persistentClass,
EntityPersister persister,
RuntimeModelCreationContext creationContext) {
return new EntityMetamodel( persistentClass, persister, creationContext );
}
}
Comment on lines +472 to +485
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DavideD instead of introducing this new class, could you just add a new createEntityMetamodel() operation to RuntimeModelCreationContext?

Or, if that doesn't work, could you at least make it a @FunctionInterface?


public AbstractEntityPersister(
final PersistentClass persistentClass,
final EntityDataAccess cacheAccessStrategy,
final NaturalIdDataAccess naturalIdRegionAccessStrategy,
final RuntimeModelCreationContext creationContext) throws HibernateException {
this( persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, creationContext, new EntityMetamodelFactory() );
}

/*
* Used by Hibernate Reactive
*/
public AbstractEntityPersister(
final PersistentClass persistentClass,
final EntityDataAccess cacheAccessStrategy,
final NaturalIdDataAccess naturalIdRegionAccessStrategy,
final RuntimeModelCreationContext creationContext,
final EntityMetamodelFactory entityMetamodelFactory) throws HibernateException {
this.jpaEntityName = persistentClass.getJpaEntityName();

//set it here, but don't call it, since it's still uninitialized!
Expand All @@ -500,7 +527,7 @@ public AbstractEntityPersister(
isLazyPropertiesCacheable = true;
}

entityMetamodel = new EntityMetamodel( persistentClass, this, creationContext );
entityMetamodel = entityMetamodelFactory.createEntityMetamodel( persistentClass, this, creationContext );

entityEntryFactory = entityMetamodel.isMutable()
? MutableEntityEntryFactory.INSTANCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,20 @@ public JoinedSubclassEntityPersister(
final EntityDataAccess cacheAccessStrategy,
final NaturalIdDataAccess naturalIdRegionAccessStrategy,
final RuntimeModelCreationContext creationContext) throws HibernateException {
this( persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, creationContext, new EntityMetamodelFactory());
}

/*
* Used by Hibernate Reactive
*/
public JoinedSubclassEntityPersister(
final PersistentClass persistentClass,
final EntityDataAccess cacheAccessStrategy,
final NaturalIdDataAccess naturalIdRegionAccessStrategy,
final RuntimeModelCreationContext creationContext,
final EntityMetamodelFactory entityMetamodelFactory) throws HibernateException {

super( persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, creationContext );
super( persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, creationContext, entityMetamodelFactory );

final Dialect dialect = creationContext.getDialect();
final SqmFunctionRegistry functionRegistry = creationContext.getFunctionRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,20 @@ public SingleTableEntityPersister(
final EntityDataAccess cacheAccessStrategy,
final NaturalIdDataAccess naturalIdRegionAccessStrategy,
final RuntimeModelCreationContext creationContext) throws HibernateException {
this( persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, creationContext, new EntityMetamodelFactory() );
}

/*
* Used by Hibernate Reactive
*/
public SingleTableEntityPersister(
final PersistentClass persistentClass,
final EntityDataAccess cacheAccessStrategy,
final NaturalIdDataAccess naturalIdRegionAccessStrategy,
final RuntimeModelCreationContext creationContext,
final EntityMetamodelFactory entityMetamodelFactory) throws HibernateException {

super( persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, creationContext );
super( persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, creationContext, entityMetamodelFactory );

final Dialect dialect = creationContext.getDialect();
final SqmFunctionRegistry functionRegistry = creationContext.getFunctionRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,19 @@ public UnionSubclassEntityPersister(
final EntityDataAccess cacheAccessStrategy,
final NaturalIdDataAccess naturalIdRegionAccessStrategy,
final RuntimeModelCreationContext creationContext) throws HibernateException {
super( persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, creationContext );
this( persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, creationContext, new EntityMetamodelFactory() );
}

/*
* Used by Hibernate Reactive
*/
public UnionSubclassEntityPersister(
final PersistentClass persistentClass,
final EntityDataAccess cacheAccessStrategy,
final NaturalIdDataAccess naturalIdRegionAccessStrategy,
final RuntimeModelCreationContext creationContext,
final EntityMetamodelFactory entityMetamodelFactory) throws HibernateException {
super( persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy, creationContext, entityMetamodelFactory );

validateGenerator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ protected MutationOperationGroup createOperationGroup(ValuesAnalysis valuesAnaly
case 0:
return MutationOperationGroupFactory.noOperations( mutationGroup );
case 1: {
final MutationOperation operation = mutationGroup.getSingleTableMutation()
.createMutationOperation( valuesAnalysis, factory() );
final MutationOperation operation = createOperation( valuesAnalysis, mutationGroup.getSingleTableMutation() );
return operation == null
? MutationOperationGroupFactory.noOperations( mutationGroup )
: MutationOperationGroupFactory.singleOperation( mutationGroup, operation );
Expand Down Expand Up @@ -116,6 +115,13 @@ protected MutationOperationGroup createOperationGroup(ValuesAnalysis valuesAnaly
}
}

/*
* Used by Hibernate Reactive
*/
protected MutationOperation createOperation(ValuesAnalysis valuesAnalysis, TableMutation<?> singleTableMutation) {
return singleTableMutation.createMutationOperation( valuesAnalysis, factory() );
}

protected void handleValueGeneration(
AttributeMapping attributeMapping,
MutationGroupBuilder mutationGroupBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class DeleteOrUpsertOperation implements SelfExecutingUpdateOperation {

private final OptionalTableUpdate optionalTableUpdate;


public DeleteOrUpsertOperation(
EntityMutationTarget mutationTarget,
EntityTableMapping tableMapping,
Expand All @@ -56,6 +55,16 @@ public DeleteOrUpsertOperation(
this.optionalTableUpdate = optionalTableUpdate;
}

/*
* Used by Hibernate Reactive
*/
protected DeleteOrUpsertOperation(DeleteOrUpsertOperation original) {
this.mutationTarget = original.mutationTarget;
this.tableMapping = original.tableMapping;
this.upsertOperation = original.upsertOperation;
this.optionalTableUpdate = original.optionalTableUpdate;
}

@Override
public MutationType getMutationType() {
return MutationType.UPDATE;
Expand Down Expand Up @@ -197,4 +206,18 @@ private void performUpsert(JdbcValueBindings jdbcValueBindings, SharedSessionCon

MODEL_MUTATION_LOGGER.tracef( "`%s` rows upserted into `%s`", rowCount, tableMapping.getTableName() );
}

/*
* Used by Hibernate Reactive
*/
public UpsertOperation getUpsertOperation() {
return upsertOperation;
}

/*
* Used by Hibernate Reactive
*/
public OptionalTableUpdate getOptionalTableUpdate() {
return optionalTableUpdate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ private static void bindKeyValue(
}
}

private JdbcDeleteMutation createJdbcDelete(SharedSessionContractImplementor session) {
/*
* Used by Hibernate Reactive
*/
protected JdbcDeleteMutation createJdbcDelete(SharedSessionContractImplementor session) {
final TableDelete tableDelete;
if ( tableMapping.getDeleteDetails() != null
&& tableMapping.getDeleteDetails().getCustomSql() != null ) {
Expand Down Expand Up @@ -305,39 +308,7 @@ private boolean performUpdate(
JdbcValueBindings jdbcValueBindings,
SharedSessionContractImplementor session) {
MODEL_MUTATION_LOGGER.tracef( "#performUpdate(%s)", tableMapping.getTableName() );

final TableUpdate<JdbcMutationOperation> tableUpdate;
if ( tableMapping.getUpdateDetails() != null
&& tableMapping.getUpdateDetails().getCustomSql() != null ) {
tableUpdate = new TableUpdateCustomSql(
new MutatingTableReference( tableMapping ),
mutationTarget,
"upsert update for " + mutationTarget.getRolePath(),
valueBindings,
keyBindings,
optimisticLockBindings,
parameters
);
}
else {
tableUpdate = new TableUpdateStandard(
new MutatingTableReference( tableMapping ),
mutationTarget,
"upsert update for " + mutationTarget.getRolePath(),
valueBindings,
keyBindings,
optimisticLockBindings,
parameters
);
}

final SqlAstTranslator<JdbcMutationOperation> translator = session
.getJdbcServices()
.getJdbcEnvironment()
.getSqlAstTranslatorFactory()
.buildModelMutationTranslator( tableUpdate, session.getFactory() );

final JdbcMutationOperation jdbcUpdate = translator.translate( null, MutationQueryOptions.INSTANCE );
final JdbcMutationOperation jdbcUpdate = createJdbcUpdate( session );

final PreparedStatementGroupSingleTable statementGroup = new PreparedStatementGroupSingleTable( jdbcUpdate, session );
final PreparedStatementDetails statementDetails = statementGroup.resolvePreparedStatementDetails( tableMapping.getTableName() );
Expand Down Expand Up @@ -374,6 +345,44 @@ private boolean performUpdate(
}
}

/*
* Used by Hibernate Reactive
*/
protected JdbcMutationOperation createJdbcUpdate(SharedSessionContractImplementor session) {
final TableUpdate<JdbcMutationOperation> tableUpdate;
if ( tableMapping.getUpdateDetails() != null
&& tableMapping.getUpdateDetails().getCustomSql() != null ) {
tableUpdate = new TableUpdateCustomSql(
new MutatingTableReference( tableMapping ),
mutationTarget,
"upsert update for " + mutationTarget.getRolePath(),
valueBindings,
keyBindings,
optimisticLockBindings,
parameters
);
}
else {
tableUpdate = new TableUpdateStandard(
new MutatingTableReference( tableMapping ),
mutationTarget,
"upsert update for " + mutationTarget.getRolePath(),
valueBindings,
keyBindings,
optimisticLockBindings,
parameters
);
}

final SqlAstTranslator<JdbcMutationOperation> translator = session
.getJdbcServices()
.getJdbcEnvironment()
.getSqlAstTranslatorFactory()
.buildModelMutationTranslator( tableUpdate, session.getFactory() );

return translator.translate( null, MutationQueryOptions.INSTANCE );
}

private void performInsert(JdbcValueBindings jdbcValueBindings, SharedSessionContractImplementor session) {
final JdbcInsertMutation jdbcInsert = createJdbcInsert( session );

Expand Down Expand Up @@ -414,7 +423,10 @@ private void performInsert(JdbcValueBindings jdbcValueBindings, SharedSessionCon
}
}

private JdbcInsertMutation createJdbcInsert(SharedSessionContractImplementor session) {
/*
* Used by Hibernate Reactive
*/
protected JdbcInsertMutation createJdbcInsert(SharedSessionContractImplementor session) {
final TableInsert tableInsert;
if ( tableMapping.getInsertDetails() != null
&& tableMapping.getInsertDetails().getCustomSql() != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,10 @@ private void populateTablesWithColumns(
}
}

private ColumnInformationImpl columnInformation(TableInformation tableInformation, ResultSet resultSet)
/*
* Hibernate Reactive overrides this
*/
protected ColumnInformationImpl columnInformation(TableInformation tableInformation, ResultSet resultSet)
throws SQLException {
return new ColumnInformationImpl(
tableInformation,
Expand Down Expand Up @@ -864,7 +867,10 @@ protected void addColumns(TableInformation tableInformation) {
}
}

private Boolean interpretTruthValue(String nullable) {
/*
* Used by Hibernate Reactive
*/
protected Boolean interpretTruthValue(String nullable) {
if ( "yes".equalsIgnoreCase( nullable ) ) {
return Boolean.TRUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
Expand Down Expand Up @@ -149,6 +150,18 @@ public EntityMetamodel(
PersistentClass persistentClass,
EntityPersister persister,
RuntimeModelCreationContext creationContext) {
this( persistentClass, persister, creationContext,
rootName -> buildIdGenerator( rootName, persistentClass, creationContext ) );
}

/*
* Used by Hibernate Reactive to adapt the id generators
*/
public EntityMetamodel(
PersistentClass persistentClass,
EntityPersister persister,
RuntimeModelCreationContext creationContext,
Function<String, Generator> generatorSupplier) {
this.sessionFactory = creationContext.getSessionFactory();

// Improves performance of EntityKey#equals by avoiding content check in String#equals
Expand All @@ -160,7 +173,7 @@ public EntityMetamodel(

subclassId = persistentClass.getSubclassId();

final Generator idgenerator = buildIdGenerator( persistentClass, creationContext );
final Generator idgenerator = generatorSupplier.apply( rootName );
identifierAttribute = PropertyFactory.buildIdentifierAttribute( persistentClass, idgenerator );

versioned = persistentClass.isVersioned();
Expand Down Expand Up @@ -483,7 +496,7 @@ private static boolean writePropertyValue(OnExecutionGenerator generator) {
return writePropertyValue;
}

private Generator buildIdGenerator(PersistentClass persistentClass, RuntimeModelCreationContext creationContext) {
private static Generator buildIdGenerator(String rootName, PersistentClass persistentClass, RuntimeModelCreationContext creationContext) {
final Generator existing = creationContext.getGenerators().get( rootName );
if ( existing != null ) {
return existing;
Expand Down
Loading