private EntityMetamodel getDeclarerEntityMetamodel(IdentifiableType<?> ownerType) { final Type.PersistenceType persistenceType = ownerType.getPersistenceType(); if (persistenceType == Type.PersistenceType.ENTITY) { return context .getSessionFactory() .getEntityPersister(ownerType.getJavaType().getName()) .getEntityMetamodel(); } else if (persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS) { PersistentClass persistentClass = context.getPersistentClassHostingProperties((MappedSuperclassTypeImpl<?>) ownerType); return context .getSessionFactory() .getEntityPersister(persistentClass.getClassName()) .getEntityMetamodel(); } else { throw new AssertionFailure( "Cannot get the metamodel for PersistenceType: " + persistenceType); } }
@SuppressWarnings("unchecked") private <Y> Type<Y> getMetaModelType(ValueContext typeContext) { switch (typeContext.getValueClassification()) { case BASIC: { return new BasicTypeImpl<Y>(typeContext.getBindableType(), Type.PersistenceType.BASIC); } case ENTITY: { final org.hibernate.type.EntityType type = (EntityType) typeContext.getValue().getType(); return (Type<Y>) context.locateEntityType(type.getAssociatedEntityName()); } case EMBEDDABLE: { final Component component = (Component) typeContext.getValue(); final EmbeddableTypeImpl<Y> embeddableType = new EmbeddableTypeImpl<Y>( typeContext.getBindableType(), typeContext.getAttributeMetadata().getOwnerType(), (ComponentType) typeContext.getValue().getType()); context.registerEmbeddedableType(embeddableType); final Iterator<Property> subProperties = component.getPropertyIterator(); while (subProperties.hasNext()) { final Property property = subProperties.next(); final AttributeImplementor<Y, Object> attribute = buildAttribute(embeddableType, property); if (attribute != null) { embeddableType.getBuilder().addAttribute(attribute); } } embeddableType.lock(); return embeddableType; } default: { throw new AssertionFailure("Unknown type : " + typeContext.getValueClassification()); } } }
/** * Here is most of the nuts and bolts of this factory, where we interpret the known JPA metadata * against the known Hibernate metadata and build a descriptor for the attribute. * * @param attributeContext The attribute to be described * @param memberResolver Strategy for how to resolve the member defining the attribute. * @param <X> The owner type * @param <Y> The attribute type * @return The attribute description */ @SuppressWarnings({"unchecked"}) private <X, Y> AttributeMetadata<X, Y> determineAttributeMetadata( AttributeContext<X> attributeContext, MemberResolver memberResolver) { LOG.trace( "Starting attribute metadata determination [" + attributeContext.getPropertyMapping().getName() + "]"); final Member member = memberResolver.resolveMember(attributeContext); LOG.trace(" Determined member [" + member + "]"); final Value value = attributeContext.getPropertyMapping().getValue(); final org.hibernate.type.Type type = value.getType(); LOG.trace( " Determined type [name=" + type.getName() + ", class=" + type.getClass().getName() + "]"); if (type.isAnyType()) { // ANY mappings are currently not supported in the JPA metamodel; see HHH-6589 if (context.isIgnoreUnsupported()) { return null; } else { throw new UnsupportedOperationException("ANY not supported"); } } else if (type.isAssociationType()) { // collection or entity if (type.isEntityType()) { // entity return new SingularAttributeMetadataImpl<X, Y>( attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, determineSingularAssociationAttributeType(member)); } // collection if (value instanceof Collection) { final Collection collValue = (Collection) value; final Value elementValue = collValue.getElement(); final org.hibernate.type.Type elementType = elementValue.getType(); // First, determine the type of the elements and use that to help determine the // collection type) final Attribute.PersistentAttributeType elementPersistentAttributeType; final Attribute.PersistentAttributeType persistentAttributeType; if (elementType.isAnyType()) { throw new UnsupportedOperationException("collection of any not supported yet"); } final boolean isManyToMany = isManyToMany(member); if (elementValue instanceof Component) { elementPersistentAttributeType = Attribute.PersistentAttributeType.EMBEDDED; persistentAttributeType = Attribute.PersistentAttributeType.ELEMENT_COLLECTION; } else if (elementType.isAssociationType()) { elementPersistentAttributeType = isManyToMany ? Attribute.PersistentAttributeType.MANY_TO_MANY : Attribute.PersistentAttributeType.ONE_TO_MANY; persistentAttributeType = elementPersistentAttributeType; } else { elementPersistentAttributeType = Attribute.PersistentAttributeType.BASIC; persistentAttributeType = Attribute.PersistentAttributeType.ELEMENT_COLLECTION; } final Attribute.PersistentAttributeType keyPersistentAttributeType; // 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()) throw new UnsupportedOperationException("collection of any not supported yet"); if (keyValue instanceof Component) keyPersistentAttributeType = Attribute.PersistentAttributeType.EMBEDDED; else if (keyType.isAssociationType()) keyPersistentAttributeType = Attribute.PersistentAttributeType.MANY_TO_ONE; else keyPersistentAttributeType = Attribute.PersistentAttributeType.BASIC; } else keyPersistentAttributeType = null; return new PluralAttributeMetadataImpl( attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, persistentAttributeType, elementPersistentAttributeType, keyPersistentAttributeType); } else if (value instanceof OneToMany) { // TODO : is this even possible??? Really OneToMany should be describing the // 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 (attributeContext.getPropertyMapping().isComposite()) { // component return new SingularAttributeMetadataImpl<X, Y>( attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, Attribute.PersistentAttributeType.EMBEDDED); } else { // basic type return new SingularAttributeMetadataImpl<X, Y>( attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, Attribute.PersistentAttributeType.BASIC); } throw new UnsupportedOperationException( "oops, we are missing something: " + attributeContext.getPropertyMapping()); }