private static List<DiscriminatorMapping> interpretDiscriminatorMappings(AnyType anyType) { final Type discriminatorType = anyType.getDiscriminatorType(); if ( ! MetaType.class.isInstance( discriminatorType ) ) { return Collections.emptyList(); } final MetaType metaType = (MetaType) discriminatorType; final List<DiscriminatorMapping> discriminatorMappings = new ArrayList<DiscriminatorMapping>(); for ( final Map.Entry<Object,String> entry : metaType.getDiscriminatorValuesToEntityNameMap().entrySet() ) { discriminatorMappings.add( new DiscriminatorMapping() { private final Object discriminatorValue = entry.getKey(); private final String entityName = entry.getValue(); @Override public Object getDiscriminatorValue() { return discriminatorValue; } @Override public String getEntityName() { return entityName; } } ); } return discriminatorMappings; }
@Override public AnyMappingDefinition toAnyDefinition() { return new StandardAnyTypeDefinition( (AnyType) getType(), getSource().getEntityMetamodel().getProperties()[ attributeNumber() ].isLazy() ); }
@Override public AnyMappingDefinition toAnyDefinition() { if ( !isAnyType() ) { throw new WalkingException( "Cannot build AnyMappingDefinition from non-any-typed attribute" ); } // todo : not sure how lazy is propogated into the component for a subattribute of type any return new StandardAnyTypeDefinition( (AnyType) getType(), false ); }
public Type getType() throws MappingException { return new AnyType( metaValues==null ? TypeFactory.heuristicType(metaTypeName) : new MetaType( metaValues, TypeFactory.heuristicType(metaTypeName) ), TypeFactory.heuristicType(identifierTypeName) ); }
@Override public AnyType getFetchedType() { return (AnyType) fetchedAttribute.getType(); }
@Override public CollectionIndexDefinition getIndexDefinition() { if ( ! hasIndex() ) { return null; } return new CollectionIndexDefinition() { @Override public CollectionDefinition getCollectionDefinition() { return AbstractCollectionPersister.this; } @Override public Type getType() { return getIndexType(); } @Override public EntityDefinition toEntityDefinition() { if ( !getType().isEntityType() ) { throw new IllegalStateException( "Cannot treat collection index type as entity" ); } return (EntityPersister) ( (AssociationType) getIndexType() ).getAssociatedJoinable( getFactory() ); } @Override public CompositionDefinition toCompositeDefinition() { if ( ! getType().isComponentType() ) { throw new IllegalStateException( "Cannot treat collection index type as composite" ); } return new CompositeCollectionElementDefinition() { @Override public String getName() { return "index"; } @Override public CompositeType getType() { return (CompositeType) getIndexType(); } @Override public boolean isNullable() { return false; } @Override public AttributeSource getSource() { // TODO: what if this is a collection w/in an encapsulated composition attribute? // should return the encapsulated composition attribute instead??? return getOwnerEntityPersister(); } @Override public Iterable<AttributeDefinition> getAttributes() { return CompositionSingularSubAttributesHelper.getCompositeCollectionIndexSubAttributes( this ); } @Override public CollectionDefinition getCollectionDefinition() { return AbstractCollectionPersister.this; } }; } @Override public AnyMappingDefinition toAnyMappingDefinition() { final Type type = getType(); if ( ! type.isAnyType() ) { throw new IllegalStateException( "Cannot treat collection index type as ManyToAny" ); } return new StandardAnyTypeDefinition( (AnyType) type, isLazy() || isExtraLazy() ); } }; }
@Override public CollectionElementDefinition getElementDefinition() { return new CollectionElementDefinition() { @Override public CollectionDefinition getCollectionDefinition() { return AbstractCollectionPersister.this; } @Override public Type getType() { return getElementType(); } @Override public AnyMappingDefinition toAnyMappingDefinition() { final Type type = getType(); if ( ! type.isAnyType() ) { throw new IllegalStateException( "Cannot treat collection element type as ManyToAny" ); } return new StandardAnyTypeDefinition( (AnyType) type, isLazy() || isExtraLazy() ); } @Override public EntityDefinition toEntityDefinition() { if ( !getType().isEntityType() ) { throw new IllegalStateException( "Cannot treat collection element type as entity" ); } return getElementPersister(); } @Override public CompositeCollectionElementDefinition toCompositeElementDefinition() { if ( ! getType().isComponentType() ) { throw new IllegalStateException( "Cannot treat entity collection element type as composite" ); } return new CompositeCollectionElementDefinition() { @Override public String getName() { return ""; } @Override public CompositeType getType() { return (CompositeType) getElementType(); } @Override public boolean isNullable() { return false; } @Override public AttributeSource getSource() { // TODO: what if this is a collection w/in an encapsulated composition attribute? // should return the encapsulated composition attribute instead??? return getOwnerEntityPersister(); } @Override public Iterable<AttributeDefinition> getAttributes() { return CompositionSingularSubAttributesHelper.getCompositeCollectionElementSubAttributes( this ); } @Override public CollectionDefinition getCollectionDefinition() { return AbstractCollectionPersister.this; } }; } }; }
public StandardAnyTypeDefinition(AnyType anyType, boolean definedAsLazy) { this.anyType = anyType; this.definedAsLazy = definedAsLazy; this.discriminatorMappings = interpretDiscriminatorMappings( anyType ); }
@Override public AnyType getType() { return anyType; }
/** * Contructor to be used by all child classes to create a new instance * and get the name right. * * @param clazz the Grails class * @param sessionFactory The Hibernate SessionFactory instance * @param metaData The ClassMetaData for this class retrieved from the SF * @param defaultConstraints The default global constraints definition */ public GrailsHibernateDomainClass(Class<?> clazz, SessionFactory sessionFactory, GrailsApplication application, ClassMetadata metaData, Map<String, Object> defaultConstraints) { super(clazz, ""); this.application = application; new StandardAnnotationMetadata(clazz); String ident = metaData.getIdentifierPropertyName(); this.defaultConstraints = defaultConstraints; if (ident != null) { Class<?> identType = getPropertyType(ident); this.identifier = new GrailsHibernateDomainClassProperty(this, ident); this.identifier.setIdentity(true); this.identifier.setType(identType); this.propertyMap.put(ident, this.identifier); } // configure the version property final int versionIndex = metaData.getVersionProperty(); String versionPropertyName = null; if (versionIndex > -1) { versionPropertyName = metaData.getPropertyNames()[versionIndex]; this.version = new GrailsHibernateDomainClassProperty(this, versionPropertyName); this.version.setType(getPropertyType(versionPropertyName)); } // configure remaining properties String[] propertyNames = metaData.getPropertyNames(); boolean[] propertyNullablility = metaData.getPropertyNullability(); for (int i = 0; i < propertyNames.length; i++) { String propertyName = propertyNames[i]; if (!propertyName.equals(ident) && !(versionPropertyName != null && propertyName.equals(versionPropertyName))) { GrailsHibernateDomainClassProperty prop = new GrailsHibernateDomainClassProperty(this, propertyName); prop.setType(getPropertyType(propertyName)); Type hibernateType = metaData.getPropertyType(propertyName); // if its an association type if (hibernateType.isAssociationType()) { prop.setAssociation(true); // get the associated type from the session factory and set it on the property AssociationType assType = (AssociationType) hibernateType; if (assType instanceof AnyType) { continue; } try { String associatedEntity = assType.getAssociatedEntityName((SessionFactoryImplementor) sessionFactory); ClassMetadata associatedMetaData = sessionFactory.getClassMetadata(associatedEntity); prop.setRelatedClassType(associatedMetaData.getMappedClass(EntityMode.POJO)); } catch (MappingException me) { // other side must be a value object if (hibernateType.isCollectionType()) { prop.setRelatedClassType(Collection.class); } } // configure type of relationship if (hibernateType.isCollectionType()) { prop.setOneToMany(true); } else if (hibernateType.isEntityType()) { prop.setManyToOne(true); // might not really be true, but for our purposes this is ok prop.setOneToOne(true); } prop.setOptional(propertyNullablility[i]); } this.propertyMap.put(propertyName, prop); } } this.properties = this.propertyMap.values().toArray(new GrailsDomainClassProperty[this.propertyMap.size()]); // process the constraints evaluateConstraints(); }
/** * Access to the mapping's AnyType * * @return The AnyType */ public AnyType getType();
/** * A Hibernate <tt>any</tt> type. * * @param metaType a type mapping <tt>java.lang.Class</tt> to a single column * @param identifierType the entity identifier type * @return the Type */ public static Type any(Type metaType, Type identifierType) { return new AnyType( metaType, identifierType ); }