PropertyHolder propertyHolder,
EntityBinder entityBinder,
boolean optional,
Mappings mappings) {
//All FK columns should be in the same table
Any value = new Any( mappings, columns[0].getTable() );
AnyMetaDef metaAnnDef = inferredData.getProperty().getAnnotation( AnyMetaDef.class );
if ( metaAnnDef != null ) {
//local has precedence over general and can be mapped for future reference if named
bindAnyMetaDefs( inferredData.getProperty(), mappings );
}
else {
metaAnnDef = mappings.getAnyMetaDef( anyMetaDefName );
}
if ( metaAnnDef != null ) {
value.setIdentifierType( metaAnnDef.idType() );
value.setMetaType( metaAnnDef.metaType() );
HashMap values = new HashMap();
org.hibernate.type.Type metaType = mappings.getTypeResolver().heuristicType( value.getMetaType() );
for (MetaValue metaValue : metaAnnDef.metaValues()) {
try {
Object discrim = ( (org.hibernate.type.DiscriminatorType) metaType ).stringToObject( metaValue
.value() );
String entityName = metaValue.targetEntity().getName();
values.put( discrim, entityName );
}
catch (ClassCastException cce) {
throw new MappingException( "metaType was not a DiscriminatorType: "
+ metaType.getName() );
}
catch (Exception e) {
throw new MappingException( "could not interpret metaValue", e );
}
}
if ( !values.isEmpty() ) value.setMetaValues( values );
}
else {
throw new AnnotationException( "Unable to find @AnyMetaDef for an @(ManyTo)Any mapping: "
+ StringHelper.qualify( propertyHolder.getPath(), inferredData.getPropertyName() ) );
}
value.setCascadeDeleteEnabled( cascadeOnDelete );
if ( !optional ) {
for (Ejb3JoinColumn column : columns) {
column.setNullable( false );
}
}
Ejb3Column[] metaColumns = Ejb3Column.buildColumnFromAnnotation(
new javax.persistence.Column[] { metaColumn }, null,
nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), mappings
);
//set metaColumn to the right table
for (Ejb3Column column : metaColumns) {
column.setTable( value.getTable() );
}
//meta column
for (Ejb3Column column : metaColumns) {
column.linkWithValue( value );
}