Skip to content

Commit

Permalink
Introduce VirtualIdEmbeddable and IdClassEmbeddable + instantiators
Browse files Browse the repository at this point in the history
EmbeddableInitializer fully uses EmbeddableInstantiator and value injection

Still need to
  - integrate EmbeddableInstantiator work (ComponentType/ComponentTuplizer)
  - integrate embedded forms.  `VirtualIdEmbeddable` does not really need it as it can use the id-mapping itself as the embedded form.  But `IdClassEmbedded` should really be integrated
  - integrate `VirtualKeyEmbeddable` and `VirtualKeyEmbedded` for use as inverse composite fks
  - share `#finishInit` handling for `EmbeddableMappingType`, `VirtualIdEmbeddable` and `IdClassEmbeddable`
  - ability to use the containing composite owner as the parent of a composite (legacy behavior is to always use the "first" entity
  • Loading branch information
sebersole committed Dec 1, 2021
1 parent 82d884d commit a9fce4b
Showing 1 changed file with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public String toString() {
// and every row
private final Object[] resolvedValues;
private Boolean allValuesNull;
private Boolean stateInjected;
private Object compositeInstance;


Expand Down Expand Up @@ -155,6 +156,8 @@ public void initializeInstance(RowProcessingState processingState) {
navigablePath
);

stateInjected = false;

extractRowState( processingState );
prepareCompositeInstance( processingState );
handleParentInjection( processingState );
Expand All @@ -165,9 +168,10 @@ public void initializeInstance(RowProcessingState processingState) {
if ( compositeInstance instanceof HibernateProxy ) {
final Initializer parentInitializer = processingState.resolveInitializer( navigablePath.getParent() );
if ( parentInitializer != this ) {
( (FetchParentAccess) parentInitializer ).registerResolutionListener(
(entity) -> representationEmbeddable.setPropertyValues( entity, resolvedValues )
);
( (FetchParentAccess) parentInitializer ).registerResolutionListener( (entity) -> {
representationEmbeddable.setPropertyValues( entity, resolvedValues );
stateInjected = true;
} );
}
else {
// At this point, createEmptyCompositesEnabled is always true, so we generate
Expand All @@ -182,13 +186,15 @@ public void initializeInstance(RowProcessingState processingState) {
final Object target = representationStrategy
.getInstantiator()
.instantiate( valuesAccess, sessionFactory);
stateInjected = true;
( (HibernateProxy) compositeInstance ).getHibernateLazyInitializer().setImplementation( target );
}
}
else if ( allValuesNull == FALSE ) {
else if ( allValuesNull == FALSE && stateInjected != TRUE ) {
// todo (6.0) : i think this is still called for cases where
// we have already done the "ctor injection"
representationEmbeddable.setPropertyValues( compositeInstance, resolvedValues );
stateInjected = true;
}
}
}
Expand Down Expand Up @@ -290,21 +296,15 @@ private Object createCompositeInstance(
final Supplier<Object[]> valuesAccess = allValuesNull == TRUE
? null
: () -> resolvedValues;

final Object instance = representationStrategy.getInstantiator().instantiate( valuesAccess, sessionFactory );
stateInjected = true;

EmbeddableLoadingLogger.INSTANCE.debugf( "Created composite instance [%s] : %s", navigablePath, instance );

return instance;
}

private void handleParentInjection(RowProcessingState processingState) {
final PropertyAccess parentInjectionAccess = embedded.getParentInjectionAttributePropertyAccess();
if ( parentInjectionAccess == null ) {
// embeddable defined no parent injection
return;
}

// todo (6.0) : should we initialize the composite instance if we get here and it is null (not NULL_MARKER)?

// we want to avoid injection for `NULL_MARKER`
Expand All @@ -316,6 +316,12 @@ private void handleParentInjection(RowProcessingState processingState) {
return;
}

final PropertyAccess parentInjectionAccess = embedded.getParentInjectionAttributePropertyAccess();
if ( parentInjectionAccess == null ) {
// embeddable defined no parent injection
return;
}

final Object parent = determineParentInstance( processingState );
if ( parent == null ) {
EmbeddableLoadingLogger.INSTANCE.debugf(
Expand All @@ -332,11 +338,7 @@ private void handleParentInjection(RowProcessingState processingState) {
compositeInstance
);

parentInjectionAccess.getSetter().set(
compositeInstance,
parent,
sessionFactory
);
parentInjectionAccess.getSetter().set( compositeInstance, parent, sessionFactory );
}

private Object determineParentInstance(RowProcessingState processingState) {
Expand Down Expand Up @@ -387,6 +389,8 @@ private Object determineParentInstance(RowProcessingState processingState) {
public void finishUpRow(RowProcessingState rowProcessingState) {
compositeInstance = null;
allValuesNull = null;
stateInjected = null;

clearResolutionListeners();
}

Expand Down

0 comments on commit a9fce4b

Please sign in to comment.