org.hibernate.metamodel.model.domain.internal.PluralAttributeBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core-jakarta Show documentation
Show all versions of hibernate-core-jakarta Show documentation
Hibernate O/RM implementation of the Jakarta Persistence specification
The newest version!
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* 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.metamodel.model.domain.internal;
import java.lang.reflect.Member;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.mapping.Property;
import org.hibernate.metamodel.AttributeClassification;
import org.hibernate.metamodel.CollectionClassification;
import org.hibernate.metamodel.internal.AttributeFactory;
import org.hibernate.metamodel.internal.MetadataContext;
import org.hibernate.metamodel.internal.PluralAttributeMetadata;
import org.hibernate.metamodel.model.domain.ManagedDomainType;
import org.hibernate.metamodel.model.domain.PersistentAttribute;
import org.hibernate.metamodel.model.domain.SimpleDomainType;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import static org.hibernate.metamodel.internal.AttributeFactory.determineSimpleType;
/**
* A "parameter object" for creating a plural attribute
*/
public class PluralAttributeBuilder {
private final JavaTypeDescriptor collectionJtd;
private final AttributeClassification attributeClassification;
private final CollectionClassification collectionClassification;
private final SimpleDomainType elementType;
private final SimpleDomainType listIndexOrMapKeyType;
private final ManagedDomainType declaringType;
private final Property property;
private final Member member;
public PluralAttributeBuilder(
JavaTypeDescriptor collectionJtd,
AttributeClassification attributeClassification,
CollectionClassification collectionClassification,
SimpleDomainType elementType,
SimpleDomainType listIndexOrMapKeyType,
ManagedDomainType declaringType,
Property property,
Member member) {
this.collectionJtd = collectionJtd;
this.attributeClassification = attributeClassification;
this.collectionClassification = collectionClassification;
this.elementType = elementType;
this.listIndexOrMapKeyType = listIndexOrMapKeyType;
this.declaringType = declaringType;
this.property = property;
this.member = member;
}
public static PersistentAttribute build(
PluralAttributeMetadata attributeMetadata,
MetadataContext metadataContext) {
final JavaTypeDescriptor attributeJtd = metadataContext.getTypeConfiguration()
.getJavaTypeDescriptorRegistry()
.getDescriptor( attributeMetadata.getJavaType() );
//noinspection unchecked
final PluralAttributeBuilder builder = new PluralAttributeBuilder(
attributeJtd,
attributeMetadata.getAttributeClassification(),
attributeMetadata.getCollectionClassification(),
AttributeFactory.determineSimpleType(
attributeMetadata.getElementValueContext(),
metadataContext
),
determineListIndexOrMapKeyType( attributeMetadata, metadataContext ),
attributeMetadata.getOwnerType(),
attributeMetadata.getPropertyMapping(),
attributeMetadata.getMember()
);
if ( Map.class.equals( attributeJtd.getJavaTypeClass() ) ) {
//noinspection unchecked
return new MapAttributeImpl<>( builder, metadataContext );
}
else if ( Set.class.equals( attributeJtd.getJavaTypeClass() ) ) {
//noinspection unchecked
return new SetAttributeImpl<>( builder, metadataContext );
}
else if ( List.class.equals( attributeJtd.getJavaTypeClass() ) ) {
//noinspection unchecked
return new ListAttributeImpl<>( builder, metadataContext );
}
else if ( Collection.class.equals( attributeJtd.getJavaTypeClass() ) ) {
//noinspection unchecked
return new BagAttributeImpl<>( builder, metadataContext );
}
//apply loose rules
if ( attributeJtd.getJavaTypeClass().isArray() ) {
//noinspection unchecked
return new ListAttributeImpl<>( builder, metadataContext );
}
if ( Map.class.isAssignableFrom( attributeJtd.getJavaTypeClass() ) ) {
//noinspection unchecked
return new MapAttributeImpl<>( builder, metadataContext );
}
else if ( Set.class.isAssignableFrom( attributeJtd.getJavaTypeClass() ) ) {
//noinspection unchecked
return new SetAttributeImpl<>( builder, metadataContext );
}
else if ( List.class.isAssignableFrom( attributeJtd.getJavaTypeClass() ) ) {
//noinspection unchecked
return new ListAttributeImpl<>( builder, metadataContext );
}
else if ( Collection.class.isAssignableFrom( attributeJtd.getJavaTypeClass() ) ) {
//noinspection unchecked
return new BagAttributeImpl<>( builder, metadataContext );
}
throw new UnsupportedOperationException( "Unknown collection: " + attributeJtd.getJavaType() );
}
private static SimpleDomainType determineListIndexOrMapKeyType(
PluralAttributeMetadata attributeMetadata,
MetadataContext metadataContext) {
if ( java.util.Map.class.isAssignableFrom( attributeMetadata.getJavaType() ) ) {
return determineSimpleType( attributeMetadata.getMapKeyValueContext(), metadataContext );
}
if ( java.util.List.class.isAssignableFrom( attributeMetadata.getJavaType() )
|| attributeMetadata.getJavaType().isArray() ) {
return metadataContext.getTypeConfiguration().getBasicTypeRegistry().getRegisteredType( Integer.class );
}
return null;
}
public ManagedDomainType getDeclaringType() {
return declaringType;
}
public AttributeClassification getAttributeClassification() {
return attributeClassification;
}
public CollectionClassification getCollectionClassification() {
return collectionClassification;
}
public SimpleDomainType getListIndexOrMapKeyType() {
return listIndexOrMapKeyType;
}
public JavaTypeDescriptor getCollectionJavaTypeDescriptor() {
return collectionJtd;
}
public SimpleDomainType getValueType() {
return elementType;
}
public Property getProperty() {
return property;
}
public Member getMember() {
return member;
}
}