Skip to content
Merged
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 @@ -1059,19 +1059,16 @@
NaturalIdMutability naturalIdMutability) {
if ( naturalIdMutability != NaturalIdMutability.NOT_NATURAL_ID ) {
attributeBinding.setNaturalIdentifier( true );

if ( naturalIdMutability == NaturalIdMutability.IMMUTABLE ) {
attributeBinding.setUpdatable( false );
}

final var metadataCollector = mappingDocument.getMetadataCollector();
final String entityName = entityBinding.getEntityName();
var ukBinder = metadataCollector.locateNaturalIdUniqueKeyBinder( entityName );
if ( ukBinder == null ) {
ukBinder = new NaturalIdUniqueKeyBinderImpl( mappingDocument, entityBinding );
metadataCollector.registerNaturalIdUniqueKeyBinder( entityName, ukBinder );
}

ukBinder.addAttributeBinding( attributeBinding );
}
}
Expand All @@ -1081,12 +1078,10 @@
PluralAttributeSource attributeSource,
PersistentClass entityDescriptor) {
final Collection collectionBinding;

if ( attributeSource instanceof PluralAttributeSourceListImpl pluralAttributeSourceList ) {
final var list = new org.hibernate.mapping.List(sourceDocument, entityDescriptor);
collectionBinding = list;
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );

registerSecondPass(
new PluralAttributeListSecondPass( sourceDocument, pluralAttributeSourceList, list ),
sourceDocument
Expand All @@ -1095,7 +1090,6 @@
else if ( attributeSource instanceof PluralAttributeSourceSetImpl ) {
collectionBinding = new Set( sourceDocument, entityDescriptor );
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );

registerSecondPass(
new PluralAttributeSetSecondPass( sourceDocument, attributeSource, collectionBinding ),
sourceDocument
Expand All @@ -1105,7 +1099,6 @@
final var map = new org.hibernate.mapping.Map( sourceDocument, entityDescriptor );
collectionBinding = map;
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );

registerSecondPass(
new PluralAttributeMapSecondPass( sourceDocument, pluralAttributeSourceMap, map ),
sourceDocument
Expand All @@ -1114,7 +1107,6 @@
else if ( attributeSource instanceof PluralAttributeSourceBagImpl ) {
collectionBinding = new Bag( sourceDocument, entityDescriptor );
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );

registerSecondPass(
new PluralAttributeBagSecondPass( sourceDocument, attributeSource, collectionBinding ),
sourceDocument
Expand All @@ -1123,7 +1115,6 @@
else if ( attributeSource instanceof PluralAttributeSourceIdBagImpl ) {
collectionBinding = new IdentifierBag( sourceDocument, entityDescriptor );
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );

registerSecondPass(
new PluralAttributeIdBagSecondPass( sourceDocument, attributeSource, collectionBinding ),
sourceDocument
Expand All @@ -1133,9 +1124,7 @@
final var array = new Array(sourceDocument, entityDescriptor);
collectionBinding = array;
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );

array.setElementClassName( sourceDocument.qualifyClassName( arraySource.getElementClass() ) );

registerSecondPass(
new PluralAttributeArraySecondPass( sourceDocument, arraySource, array ),
sourceDocument
Expand All @@ -1145,7 +1134,6 @@
final var primitiveArray = new PrimitiveArray( sourceDocument, entityDescriptor );
collectionBinding = primitiveArray;
bindCollectionMetadata( sourceDocument, attributeSource, collectionBinding );

registerSecondPass(
new PluralAttributePrimitiveArraySecondPass(
sourceDocument,
Expand All @@ -1161,16 +1149,17 @@
);
}

sourceDocument.getMetadataCollector().addCollectionBinding( collectionBinding );
sourceDocument.getMetadataCollector()
.addCollectionBinding( collectionBinding );

final var attribute = new Property();
attribute.setValue( collectionBinding );
bindProperty( sourceDocument, attributeSource, attribute );

return attribute;
}

private void bindCollectionMetadata(MappingDocument mappingDocument, PluralAttributeSource source, Collection binding) {
private void bindCollectionMetadata(
MappingDocument mappingDocument, PluralAttributeSource source, Collection binding) {
binding.setRole( source.getAttributeRole().getFullPath() );
binding.setInverse( source.isInverse() );
binding.setMutable( source.isMutable() );
Expand All @@ -1186,40 +1175,11 @@

applyCaching( mappingDocument, source.getCaching(), binding );

// bind the collection type info
String typeName = source.getTypeInformation().getName();
final Map<String,String> typeParameters = new HashMap<>();
if ( typeName != null ) {
// see if there is a corresponding type-def
final var typeDefinition =
mappingDocument.getMetadataCollector().getTypeDefinition( typeName );
if ( typeDefinition != null ) {
typeName = typeDefinition.getTypeImplementorClass().getName();
if ( typeDefinition.getParameters() != null ) {
typeParameters.putAll( typeDefinition.getParameters() );
}
}
else {
// it could be an unqualified class name, in which case we should qualify
// it with the implicit package name for this context, if one.
typeName = mappingDocument.qualifyClassName( typeName );
}
}
if ( source.getTypeInformation().getParameters() != null ) {
typeParameters.putAll( source.getTypeInformation().getParameters() );
}

binding.setTypeName( typeName );
binding.setTypeParameters( typeParameters );
bindCollectionType( mappingDocument, source, binding );

final var fetchCharacteristics = source.getFetchCharacteristics();
if ( fetchCharacteristics.getFetchTiming() == FetchTiming.DELAYED ) {
binding.setLazy( true );
binding.setExtraLazy( fetchCharacteristics.isExtraLazy() );
}
else {
binding.setLazy( false );
}
binding.setLazy( fetchCharacteristics.getFetchTiming() == FetchTiming.DELAYED );
binding.setExtraLazy( fetchCharacteristics.isExtraLazy() );

setupFetching( source, binding );

Expand All @@ -1230,23 +1190,16 @@
binding.setLoaderName( source.getCustomLoaderName() );
bindCustomSql( source, binding );

if ( source instanceof Sortable sortable ) {
if ( sortable.isSorted() ) {
binding.setSorted( true );
final String comparatorName = sortable.getComparatorName();
if ( !comparatorName.equals( "natural" ) ) {
binding.setComparatorClassName( comparatorName );
}
}
else {
binding.setSorted( false );
if ( source instanceof Sortable sortable && sortable.isSorted() ) {
binding.setSorted( true );
final String comparatorName = sortable.getComparatorName();
if ( !comparatorName.equals( "natural" ) ) {
binding.setComparatorClassName( comparatorName );
}
}

if ( source instanceof Orderable orderable ) {
if ( orderable.isOrdered() ) {
binding.setOrderBy( orderable.getOrder() );
}
if ( source instanceof Orderable orderable && orderable.isOrdered() ) {
binding.setOrderBy( orderable.getOrder() );
}

final String cascadeStyle = source.getCascadeStyleName();
Expand All @@ -1265,6 +1218,41 @@
}
}

private static void bindCollectionType(
MappingDocument mappingDocument, PluralAttributeSource source, Collection binding) {
// bind the collection type info
final String explicitTypeName = source.getTypeInformation().getName();
final Map<String,String> typeParameters = new HashMap<>();
final String typeName;
if ( explicitTypeName != null ) {
// see if there is a corresponding type-def
final var typeDefinition =
mappingDocument.getMetadataCollector()
.getTypeDefinition( explicitTypeName );
if ( typeDefinition != null ) {
typeName = typeDefinition.getTypeImplementorClass().getName();
final var parameters = typeDefinition.getParameters();
if ( parameters != null ) {
typeParameters.putAll( parameters );
}
}
else {
// it could be an unqualified class name, in which case qualify
// it with the implicit package name for this context, if one.
typeName = mappingDocument.qualifyClassName( explicitTypeName );
}
}
else {
typeName = null;
}
final var parameters = source.getTypeInformation().getParameters();
if ( parameters != null ) {
typeParameters.putAll( parameters );
}
binding.setTypeName( typeName );
binding.setTypeParameters( typeParameters );
}

private static void bindCustomSql(PluralAttributeSource source, Collection binding) {
if ( source.getCustomSqlInsert() != null ) {
binding.setCustomSQLInsert(
Expand Down Expand Up @@ -1298,26 +1286,19 @@

private static void setupFetching(PluralAttributeSource source, Collection binding) {
final var fetchCharacteristics = source.getFetchCharacteristics();
switch ( fetchCharacteristics.getFetchStyle() ) {
case SELECT:
binding.setFetchMode( FetchMode.SELECT );
break;
case JOIN:
binding.setFetchMode( FetchMode.JOIN );
break;
final var fetchStyle = fetchCharacteristics.getFetchStyle();
binding.setFetchMode( switch ( fetchStyle ) {
case SELECT, BATCH, SUBSELECT -> FetchMode.SELECT;
case JOIN -> FetchMode.JOIN;
} );
switch ( fetchStyle ) {

Check warning

Code scanning / CodeQL

Missing enum case in switch Warning

Switch statement does not have a case for
JOIN
or
SELECT
.
case BATCH:
binding.setFetchMode( FetchMode.SELECT );
binding.setBatchSize( fetchCharacteristics.getBatchSize() );
break;
case SUBSELECT:
binding.setFetchMode( FetchMode.SELECT );
binding.setSubselectLoadable( true );
// todo : this could totally be done using a "symbol map" approach
binding.getOwner().setSubselectLoadableCollections( true );
break;
default:
throw new AssertionFailure( "Unexpected FetchStyle : "
+ fetchCharacteristics.getFetchStyle().name() );
}
}

Expand Down Expand Up @@ -2565,17 +2546,18 @@
private static TypeResolution resolveType(
MappingDocument sourceDocument,
HibernateTypeSource typeSource) {
if ( StringHelper.isEmpty( typeSource.getName() ) ) {
final String typeSourceName = typeSource.getName();
if ( StringHelper.isEmpty( typeSourceName ) ) {
return null;
}

final var typeDefinition =
sourceDocument.getMetadataCollector()
.getTypeDefinition( typeSource.getName() );
.getTypeDefinition( typeSourceName );
final Map<String,String> typeParameters = new HashMap<>();
final String typeName;
if ( typeDefinition == null ) {
typeName = typeSource.getName();
typeName = typeSourceName;
}
else {
// the explicit name referred to a type-def
Expand All @@ -2585,13 +2567,11 @@
typeParameters.putAll( parameters );
}
}

// parameters on the property mapping should override parameters in the type-def
final var parameters = typeSource.getParameters();
if ( parameters != null ) {
typeParameters.putAll( parameters );
}

return new TypeResolution( typeName, typeParameters );
}

Expand Down