下面列出了怎么用org.hibernate.type.AnyType的API类实例代码及写法,或者点击链接到github查看源代码。
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();
}
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 );
}
}
}
@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 );
}