-
Notifications
You must be signed in to change notification settings - Fork 100
Composite key with generated value not working properly #2303
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...n/java/org/hibernate/reactive/id/impl/ReactiveCompositeNestedGeneratedValueGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.id.impl; | ||
|
||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.generator.BeforeExecutionGenerator; | ||
import org.hibernate.generator.Generator; | ||
import org.hibernate.generator.GeneratorCreationContext; | ||
import org.hibernate.id.CompositeNestedGeneratedValueGenerator; | ||
import org.hibernate.id.IdentifierGenerationException; | ||
import org.hibernate.mapping.Component; | ||
import org.hibernate.metamodel.spi.RuntimeModelCreationContext; | ||
import org.hibernate.reactive.id.ReactiveIdentifierGenerator; | ||
import org.hibernate.reactive.session.ReactiveConnectionSupplier; | ||
import org.hibernate.reactive.tuple.entity.ReactiveEntityMetamodel; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import static org.hibernate.generator.EventType.INSERT; | ||
import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture; | ||
import static org.hibernate.reactive.util.impl.CompletionStages.loop; | ||
|
||
public class ReactiveCompositeNestedGeneratedValueGenerator extends CompositeNestedGeneratedValueGenerator implements | ||
ReactiveIdentifierGenerator<Object> { | ||
|
||
public ReactiveCompositeNestedGeneratedValueGenerator( | ||
CompositeNestedGeneratedValueGenerator generator, | ||
GeneratorCreationContext creationContext, | ||
RuntimeModelCreationContext runtimeModelCreationContext) { | ||
super( | ||
generator.getGenerationContextLocator(), | ||
generator.getCompositeType(), | ||
reactivePlans( generator, creationContext, runtimeModelCreationContext ) | ||
); | ||
} | ||
|
||
private static List<GenerationPlan> reactivePlans( | ||
CompositeNestedGeneratedValueGenerator generator, | ||
GeneratorCreationContext creationContext, | ||
RuntimeModelCreationContext runtimeModelCreationContext) { | ||
final List<GenerationPlan> plans = new ArrayList<>(); | ||
for ( GenerationPlan plan : generator.getGenerationPlans() ) { | ||
final GenerationPlan reactivePlane = new Component.ValueGenerationPlan( | ||
(BeforeExecutionGenerator) ReactiveEntityMetamodel.augmentWithReactiveGenerator( | ||
plan.getGenerator(), | ||
creationContext, | ||
runtimeModelCreationContext | ||
), | ||
plan.getInjector(), | ||
plan.getPropertyIndex() | ||
); | ||
plans.add( reactivePlane ); | ||
} | ||
return plans; | ||
} | ||
|
||
@Override | ||
public CompletionStage<Object> generate(ReactiveConnectionSupplier reactiveConnectionSupplier, Object object) { | ||
SharedSessionContractImplementor session = (SharedSessionContractImplementor) reactiveConnectionSupplier; | ||
final Object context = getGenerationContextLocator().locateGenerationContext( session, object ); | ||
|
||
final List<Object> generatedValues = getCompositeType().isMutable() | ||
? null | ||
: new ArrayList<>( getGenerationPlans().size() ); | ||
return loop( getGenerationPlans(), generationPlan -> generateIdentifier( | ||
reactiveConnectionSupplier, | ||
object, | ||
generationPlan, | ||
session, | ||
generatedValues, | ||
context | ||
) ) | ||
.thenCompose( v -> handleGeneratedValues( generatedValues, context, session ) ); | ||
} | ||
|
||
private CompletionStage<?> generateIdentifier( | ||
ReactiveConnectionSupplier reactiveConnectionSupplier, | ||
Object object, | ||
GenerationPlan generationPlan, | ||
SharedSessionContractImplementor session, | ||
List<Object> generatedValues, | ||
Object context) { | ||
final Generator generator = generationPlan.getGenerator(); | ||
if ( generator.generatedBeforeExecution( object, session ) ) { | ||
if ( generator instanceof ReactiveIdentifierGenerator<?> reactiveIdentifierGenerator ) { | ||
return reactiveIdentifierGenerator | ||
.generate( reactiveConnectionSupplier, object ) | ||
.thenAccept( generated -> { | ||
if ( generatedValues != null ) { | ||
generatedValues.add( generated ); | ||
} | ||
else { | ||
generationPlan.getInjector().set( context, generated ); | ||
} | ||
} ); | ||
} | ||
else { | ||
final Object currentValue = generator.allowAssignedIdentifiers() | ||
? getCompositeType().getPropertyValue( context, generationPlan.getPropertyIndex(), session ) | ||
: null; | ||
return completedFuture( ( (BeforeExecutionGenerator) generator ) | ||
.generate( session, object, currentValue, INSERT ) ); | ||
} | ||
} | ||
else { | ||
throw new IdentifierGenerationException( "Identity generation isn't supported for composite ids" ); | ||
} | ||
} | ||
|
||
private CompletionStage<Object> handleGeneratedValues( | ||
List<Object> generatedValues, | ||
Object context, | ||
SharedSessionContractImplementor session) { | ||
if ( generatedValues != null ) { | ||
final Object[] values = getCompositeType().getPropertyValues( context ); | ||
for ( int i = 0; i < generatedValues.size(); i++ ) { | ||
values[getGenerationPlans().get( i ).getPropertyIndex()] = generatedValues.get( i ); | ||
} | ||
return completedFuture( getCompositeType().replacePropertyValues( context, values, session ) ); | ||
} | ||
else { | ||
return completedFuture( context ); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Useless parameter Note