org.hibernate.type.AnyType Java Examples

The following examples show how to use org.hibernate.type.AnyType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StandardAnyTypeDefinition.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #2
Source File: EntityBasedAssociationAttribute.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public AnyMappingDefinition toAnyDefinition() {
	return new StandardAnyTypeDefinition(
			(AnyType) getType(),
			getSource().getEntityMetamodel().getProperties()[ attributeNumber() ].isLazy()
	);
}
 
Example #3
Source File: CompositeBasedAssociationAttribute.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 );
}
 
Example #4
Source File: Any.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Type getType() throws MappingException {
	return new AnyType(
		metaValues==null ?
			TypeFactory.heuristicType(metaTypeName) :
			new MetaType( metaValues, TypeFactory.heuristicType(metaTypeName) ),
			TypeFactory.heuristicType(identifierTypeName)
	);
}
 
Example #5
Source File: AnyAttributeFetchImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public AnyType getFetchedType() {
	return (AnyType) fetchedAttribute.getType();
}
 
Example #6
Source File: AbstractPropertyMapping.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected void addPropertyPath(
		String path,
		Type type,
		String[] columns,
		String[] columnReaders,
		String[] columnReaderTemplates,
		String[] formulaTemplates,
		Mapping factory) {
	Type existingType = typesByPropertyPath.get( path );
	if ( existingType != null || duplicateIncompatiblePaths.contains( path ) ) {
		// If types match or the new type is not an association type, there is nothing for us to do
		if ( type == existingType || existingType == null || !( type instanceof AssociationType ) ) {
			logDuplicateRegistration( path, existingType, type );
		}
		else if ( !( existingType instanceof AssociationType ) ) {
			// Workaround for org.hibernate.cfg.annotations.PropertyBinder.bind() adding a component for *ToOne ids
			logDuplicateRegistration( path, existingType, type );
		}
		else {
			if ( type instanceof AnyType && existingType instanceof AnyType ) {
				// TODO: not sure how to handle any types. For now we just return and let the first type dictate what type the property has...
			}
			else {
				Type commonType = null;
				MetadataImplementor metadata = (MetadataImplementor) factory;
				if ( type instanceof CollectionType && existingType instanceof CollectionType ) {
					Collection thisCollection = metadata.getCollectionBinding( ( (CollectionType) existingType ).getRole() );
					Collection otherCollection = metadata.getCollectionBinding( ( (CollectionType) type ).getRole() );

					if ( thisCollection.isSame( otherCollection ) ) {
						logDuplicateRegistration( path, existingType, type );
						return;
					}
					else {
						logIncompatibleRegistration( path, existingType, type );
					}
				}
				else if ( type instanceof EntityType && existingType instanceof EntityType ) {
					EntityType entityType1 = (EntityType) existingType;
					EntityType entityType2 = (EntityType) type;

					if ( entityType1.getAssociatedEntityName().equals( entityType2.getAssociatedEntityName() ) ) {
						logDuplicateRegistration( path, existingType, type );
						return;
					}
					else {
						commonType = getCommonType( metadata, entityType1, entityType2 );
					}
				}
				else {
					logIncompatibleRegistration( path, existingType, type );
				}
				if ( commonType == null ) {
					duplicateIncompatiblePaths.add( path );
					typesByPropertyPath.remove( path );
					// Set everything to empty to signal action has to be taken!
					// org.hibernate.hql.internal.ast.tree.DotNode.dereferenceEntityJoin() is reacting to this
					String[] empty = new String[0];
					columnsByPropertyPath.put( path, empty );
					columnReadersByPropertyPath.put( path, empty );
					columnReaderTemplatesByPropertyPath.put( path, empty );
					if ( formulaTemplates != null ) {
						formulaTemplatesByPropertyPath.put( path, empty );
					}
				}
				else {
					typesByPropertyPath.put( path, commonType );
				}
			}
		}
	}
	else {
		typesByPropertyPath.put( path, type );
		columnsByPropertyPath.put( path, columns );
		columnReadersByPropertyPath.put( path, columnReaders );
		columnReaderTemplatesByPropertyPath.put( path, columnReaderTemplates );
		if ( formulaTemplates != null ) {
			formulaTemplatesByPropertyPath.put( path, formulaTemplates );
		}
	}
}
 
Example #7
Source File: AbstractCollectionPersister.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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() );
		}
	};
}
 
Example #8
Source File: AbstractCollectionPersister.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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;
				}
			};
		}
	};
}
 
Example #9
Source File: StandardAnyTypeDefinition.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public StandardAnyTypeDefinition(AnyType anyType, boolean definedAsLazy) {
	this.anyType = anyType;
	this.definedAsLazy = definedAsLazy;
	this.discriminatorMappings = interpretDiscriminatorMappings( anyType );
}
 
Example #10
Source File: StandardAnyTypeDefinition.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public AnyType getType() {
	return anyType;
}
 
Example #11
Source File: GrailsHibernateDomainClass.java    From AlgoTrader with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example #12
Source File: AnyMappingDefinition.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Access to the mapping's AnyType
 *
 * @return The AnyType
 */
public AnyType getType();
 
Example #13
Source File: Hibernate.java    From cacheonix-core with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * 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 );
}