use of org.hibernate.metamodel.AttributeClassification in project hibernate-orm by hibernate.
the class AttributeFactory method determineAttributeMetadata.
private static <X, Y> AttributeMetadata<X, Y> determineAttributeMetadata(AttributeContext<X> attributeContext, MemberResolver memberResolver, MetadataContext context) {
final Property propertyMapping = attributeContext.getPropertyMapping();
final String propertyName = propertyMapping.getName();
LOG.tracef("Starting attribute metadata determination [%s]", propertyName);
final Member member = memberResolver.resolveMember(attributeContext, context);
LOG.tracef(" Determined member [%s]", member);
final Value value = propertyMapping.getValue();
final org.hibernate.type.Type type = value.getType();
LOG.tracef(" Determined type [name=%s, class=%s]", type.getName(), type.getClass().getName());
if (type.isAnyType()) {
return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.ANY, context);
} else if (type.isAssociationType()) {
// collection or entity
if (type.isEntityType()) {
// entity
return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, determineSingularAssociationClassification(member), context);
}
// collection
if (value instanceof Collection) {
final Collection collValue = (Collection) value;
final Value elementValue = collValue.getElement();
final org.hibernate.type.Type elementType = elementValue.getType();
final boolean isManyToMany = isManyToMany(member);
// First, determine the type of the elements and use that to help determine the
// collection type
final AttributeClassification elementClassification;
final AttributeClassification attributeClassification;
if (elementType.isAnyType()) {
attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
elementClassification = AttributeClassification.ANY;
} else if (elementValue instanceof Component) {
elementClassification = AttributeClassification.EMBEDDED;
attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
} else if (elementType.isAssociationType()) {
elementClassification = isManyToMany ? AttributeClassification.MANY_TO_MANY : AttributeClassification.ONE_TO_MANY;
attributeClassification = elementClassification;
} else {
elementClassification = AttributeClassification.BASIC;
attributeClassification = AttributeClassification.ELEMENT_COLLECTION;
}
final AttributeClassification indexClassification;
// Finally, we determine the type of the map key (if needed)
if (value instanceof Map) {
final Value keyValue = ((Map) value).getIndex();
final org.hibernate.type.Type keyType = keyValue.getType();
if (keyType.isAnyType()) {
indexClassification = AttributeClassification.ANY;
} else if (keyValue instanceof Component) {
indexClassification = AttributeClassification.EMBEDDED;
} else if (keyType.isAssociationType()) {
indexClassification = AttributeClassification.MANY_TO_ONE;
} else {
indexClassification = AttributeClassification.BASIC;
}
} else if (value instanceof List) {
indexClassification = AttributeClassification.BASIC;
} else {
indexClassification = null;
}
return new PluralAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, attributeClassification, elementClassification, indexClassification, context);
} else if (value instanceof OneToMany) {
// element value within a o.h.mapping.Collection (see logic branch above)
throw new IllegalArgumentException("HUH???");
// final boolean isManyToMany = isManyToMany( member );
// //one to many with FK => entity
// return new PluralAttributeMetadataImpl(
// attributeContext.getPropertyMapping(),
// attributeContext.getOwnerType(),
// member,
// isManyToMany
// ? Attribute.PersistentAttributeType.MANY_TO_MANY
// : Attribute.PersistentAttributeType.ONE_TO_MANY
// value,
// AttributeContext.TypeStatus.ENTITY,
// Attribute.PersistentAttributeType.ONE_TO_MANY,
// null, null, null
// );
}
} else if (propertyMapping.isComposite()) {
// component
return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.EMBEDDED, context);
} else {
// basic type
return new SingularAttributeMetadataImpl<>(propertyMapping, attributeContext.getOwnerType(), member, AttributeClassification.BASIC, context);
}
throw new UnsupportedMappingException("oops, we are missing something: " + propertyMapping);
}
Aggregations