Example usage for org.hibernate.type MapType getElementType

List of usage examples for org.hibernate.type MapType getElementType

Introduction

In this page you can find the example usage for org.hibernate.type MapType getElementType.

Prototype

public final Type getElementType(SessionFactoryImplementor factory) throws MappingException 

Source Link

Document

Get the Hibernate type of the collection elements

Usage

From source file:org.atemsource.atem.impl.hibernate.attributetype.MapAssociationAttributeFactory.java

License:Apache License

@Override
public Attribute create(ValidationMetaData validationMetaData, EntityType entityType,
        HibernateEntityTypeCreationContext ctx, PropertyDescriptor propertyDescriptor, Type attributeType,
        SessionFactoryImplementor sessionFactoryImplementor) {
    MapType mapType = (MapType) attributeType;
    Association association = propertyDescriptor.getAnnotation(Association.class);
    final MapAttributeImpl mapAssociationAttributeImpl = beanCreator.create(MapAttributeImpl.class);
    mapAssociationAttributeImpl.setAccessor(new HibernateAccessor(propertyDescriptor.getField(),
            propertyDescriptor.getReadMethod(), propertyDescriptor.getWriteMethod()));
    mapAssociationAttributeImpl.setCode(propertyDescriptor.getName());
    mapAssociationAttributeImpl.setEntityType(entityType);

    final String targeTypeName = mapType.getElementType(sessionFactoryImplementor).getName();
    final EntityType targetType = ctx.getEntityTypeReference(targeTypeName);
    mapAssociationAttributeImpl.setTargetType(targetType);
    MapKey jpaMapKey = propertyDescriptor.getAnnotation(MapKey.class);
    final String mapKeyPropertyName = jpaMapKey.name();

    ctx.addListener(new EntityTypeRepositoryListener() {
        @Override/*  ww  w  .j  a v a  2  s . c  o m*/
        public void onEvent(PhaseEvent event) {
            if (event.getPhase() == Phase.ENTITY_TYPES_INITIALIZED) {
                final Attribute keyAttribute = targetType.getAttribute(mapKeyPropertyName);
                mapAssociationAttributeImpl.setKeyType(keyAttribute.getTargetType());

            }
        }
    });
    // TODO add subtypes here
    mapAssociationAttributeImpl.setWriteable(true);
    return mapAssociationAttributeImpl;
}