Skip to content

Commit

Permalink
Implement support for @embeddable with ToMany and ToOne
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Dec 10, 2019
1 parent 3be2369 commit 1363844
Show file tree
Hide file tree
Showing 28 changed files with 273 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@
import java.util.function.Function;

import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.ToOne;
import org.hibernate.metamodel.mapping.internal.MappingModelCreationHelper;
import org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess;
import org.hibernate.metamodel.mapping.internal.SingularAssociationAttributeMapping;
import org.hibernate.metamodel.spi.EmbeddableRepresentationStrategy;
import org.hibernate.metamodel.spi.RuntimeModelCreationContext;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.tree.from.TableGroup;
Expand All @@ -34,7 +38,9 @@
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetchable;
import org.hibernate.type.BasicType;
import org.hibernate.type.CollectionType;
import org.hibernate.type.CompositeType;
import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.spi.TypeConfiguration;
Expand Down Expand Up @@ -114,15 +120,16 @@ private boolean finishInitialization(
while ( propertyIterator.hasNext() ) {
final Property bootPropertyDescriptor = propertyIterator.next();

if ( subtypes[ attributeIndex ] instanceof BasicType ) {
final Type subtype = subtypes[attributeIndex];
if ( subtype instanceof BasicType ) {
attributeMappings.put(
bootPropertyDescriptor.getName(),
MappingModelCreationHelper.buildBasicAttributeMapping(
bootPropertyDescriptor.getName(),
attributeIndex,
bootPropertyDescriptor,
this,
(BasicType) subtypes[ attributeIndex ],
(BasicType) subtype,
containingTableExpression,
mappedColumnExpressions.get( columnPosition++ ),
representationStrategy.resolvePropertyAccess( bootPropertyDescriptor ),
Expand All @@ -131,8 +138,8 @@ private boolean finishInitialization(
)
);
}
else if ( subtypes[ attributeIndex ] instanceof CompositeType ) {
final CompositeType subCompositeType = (CompositeType) subtypes[ attributeIndex ];
else if ( subtype instanceof CompositeType ) {
final CompositeType subCompositeType = (CompositeType) subtype;
final int columnSpan = subCompositeType.getColumnSpan( creationProcess.getCreationContext().getSessionFactory() );

attributeMappings.put(
Expand All @@ -153,6 +160,53 @@ else if ( subtypes[ attributeIndex ] instanceof CompositeType ) {

columnPosition += columnSpan;
}
else {
final EntityPersister entityPersister = creationProcess
.getEntityPersister( bootDescriptor.getOwner().getEntityName() );
if ( subtype instanceof CollectionType ) {
attributeMappings.put(
bootPropertyDescriptor.getName(),
MappingModelCreationHelper.buildPluralAttributeMapping(
bootPropertyDescriptor.getName(),
attributeIndex,
bootPropertyDescriptor,
entityPersister,
representationStrategy.resolvePropertyAccess( bootPropertyDescriptor ),
compositeType.getCascadeStyle( attributeIndex),
compositeType.getFetchMode( attributeIndex ),
creationProcess
)
);
}
else if ( subtype instanceof EntityType ) {
final Dialect dialect = creationProcess.getCreationContext()
.getSessionFactory()
.getJdbcServices()
.getDialect();

final SingularAssociationAttributeMapping singularAssociationAttributeMapping = MappingModelCreationHelper.buildSingularAssociationAttributeMapping(
bootPropertyDescriptor.getName(),
attributeIndex,
bootPropertyDescriptor,
entityPersister,
(EntityType) subtype,
getRepresentationStrategy().resolvePropertyAccess( bootPropertyDescriptor ),
compositeType.getCascadeStyle( attributeIndex ),
creationProcess
);
MappingModelCreationHelper.interpretKeyDescriptor(
singularAssociationAttributeMapping,
bootPropertyDescriptor,
(ToOne) bootPropertyDescriptor.getValue(),
entityPersister,
dialect,
creationProcess
);
attributeMappings.put( bootPropertyDescriptor.getName(), singularAssociationAttributeMapping );
// todo (6.0) : not sure it is always correct
columnPosition++;
}
}

attributeIndex++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.sql.ast.tree.from.TableGroupJoinProducer;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
Expand Down Expand Up @@ -246,13 +247,14 @@ public TableGroupJoin createTableGroupJoin(
lhs
);

lhs.addTableGroupJoin( new TableGroupJoin( navigablePath, SqlAstJoinType.INNER, compositeTableGroup, null ) );

return new TableGroupJoin(
TableGroupJoin tableGroupJoin = new TableGroupJoin(
navigablePath,
sqlAstJoinType,
compositeTableGroup
);
lhs.addTableGroupJoin( tableGroupJoin );

return tableGroupJoin;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.SortedSet;
import java.util.function.Consumer;

import org.hibernate.FetchMode;
import org.hibernate.LockMode;
import org.hibernate.MappingException;
import org.hibernate.NotYetImplementedFor6Exception;
Expand Down Expand Up @@ -679,6 +680,7 @@ public static PluralAttributeMapping buildPluralAttributeMapping(
ManagedMappingType declaringType,
PropertyAccess propertyAccess,
CascadeStyle cascadeStyle,
FetchMode fetchMode,
MappingModelCreationProcess creationProcess) {

final Collection bootValueMapping = (Collection) bootProperty.getValue();
Expand Down Expand Up @@ -873,7 +875,7 @@ public boolean isIncludedInOptimisticLocking() {
};

final FetchStyle style = FetchStrategyHelper.determineFetchStyleByMetadata(
( (OuterJoinLoadable) declaringType ).getFetchMode( stateArrayPosition ),
fetchMode,
collectionDescriptor.getCollectionType(),
sessionFactory
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ public EntityFetch generateFetch(
creationState.getSqlAstCreationState().getCreationContext()
);

lhsTableGroup.addTableGroupJoin( tableGroupJoin );

sqlAstCreationState.getFromClauseAccess().registerTableGroup(
fetchablePath,
tableGroupJoin.getJoinedGroup()
Expand Down Expand Up @@ -327,18 +325,22 @@ public boolean isCircular(FetchParent fetchParent, SqlAstProcessingState creatio
.getFromClauseAccess()
.findTableGroup( parentParentNavigablePath )
.getModelPart();
// final SingularAssociationAttributeMapping part = (SingularAssociationAttributeMapping) modelPart
// .findSubPart( panentNaviblePath.getLocalName(), null );
final EntityAssociationMapping part = (EntityAssociationMapping) modelPart.findSubPart( panentNaviblePath.getLocalName(), null );

if ( panentNaviblePath.getLocalName().equals( referencedPropertyName )
&& part.getFetchableName().equals( referencedPropertyName ) ) {
return true;
final ModelPart subPart = modelPart.findSubPart( panentNaviblePath.getLocalName(), null );
if ( subPart instanceof EntityAssociationMapping ) {
final EntityAssociationMapping part = (EntityAssociationMapping) subPart;

if ( panentNaviblePath.getLocalName().equals( referencedPropertyName )
&& part.getFetchableName().equals( referencedPropertyName ) ) {
return true;
}
else if ( part.getKeyTargetMatchPart() != null
&& part.getKeyTargetMatchPart().getPartName().equals( getAttributeName() ) ) {
return true;
}
}
else if ( part.getKeyTargetMatchPart() != null
// && part.getKeyTargetMatchPart().equals( this ) ) {
&& part.getKeyTargetMatchPart().getPartName().equals( getAttributeName() ) ) {
return true;
else {
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5824,7 +5824,6 @@ public void finishMappingModelInitialization(MappingModelCreationProcess creatio
final Dialect dialect = creationProcess.getCreationContext()
.getSessionFactory()
.getJdbcServices()
.getJdbcEnvironment()
.getDialect();

MappingModelCreationHelper.interpretKeyDescriptor(
Expand All @@ -5836,6 +5835,8 @@ public void finishMappingModelInitialization(MappingModelCreationProcess creatio
creationProcess
);
} );

singularAssociationsToFinilize.clear();
}

protected static SqmMultiTableMutationStrategy interpretSqmMultiTableStrategy(
Expand Down Expand Up @@ -6063,6 +6064,7 @@ else if ( attrType instanceof CollectionType ) {
this,
propertyAccess,
tupleAttrDefinition.getCascadeStyle(),
getFetchMode( stateArrayPosition ),
creationProcess
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,8 @@ protected void processTableGroupJoins(TableGroup source) {
protected void processTableGroupJoin(TableGroupJoin tableGroupJoin) {
final TableGroup joinedGroup = tableGroupJoin.getJoinedGroup();

//noinspection StatementWithEmptyBody
if ( joinedGroup instanceof VirtualTableGroup ) {
// nothing to do
// todo (6.0) : join predicates?
processTableGroupJoins( tableGroupJoin.getJoinedGroup());
}
else {
appendSql( EMPTY_STRING );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import java.util.function.Consumer;

import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
import org.hibernate.metamodel.mapping.SingularAttributeMapping;
import org.hibernate.metamodel.mapping.StateArrayContributorMapping;
Expand Down Expand Up @@ -48,11 +49,12 @@ public AbstractEmbeddableInitializer(
this.embeddedModelPartDescriptor = resultDescriptor.getReferencedMappingContainer();
this.fetchParentAccess = fetchParentAccess;

final int numOfAttrs = embeddedModelPartDescriptor.getEmbeddableTypeDescriptor().getNumberOfAttributeMappings();
final EmbeddableMappingType embeddableTypeDescriptor = embeddedModelPartDescriptor.getEmbeddableTypeDescriptor();
final int numOfAttrs = embeddableTypeDescriptor.getNumberOfAttributeMappings();
this.resolvedValues = new Object[ numOfAttrs ];
this.assemblerMap = new IdentityHashMap<>( numOfAttrs );

this.embeddedModelPartDescriptor.getEmbeddableTypeDescriptor().visitStateArrayContributors(
embeddableTypeDescriptor.visitStateArrayContributors(
stateArrayContributor -> {
final Fetch fetch = resultDescriptor.findFetch( stateArrayContributor.getFetchableName() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* 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.test.annotations.override;
package org.hibernate.orm.test.annotations.override;

import javax.persistence.Entity;
import javax.persistence.Id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* 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.test.annotations.override;
package org.hibernate.orm.test.annotations.override;

import javax.persistence.Embeddable;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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.test.annotations.override;
package org.hibernate.orm.test.annotations.override;

import org.hibernate.dialect.H2Dialect;
import org.hibernate.mapping.Table;
Expand Down
Loading

0 comments on commit 1363844

Please sign in to comment.