package org.hibernate.cfg.annotations;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.ConstraintMode;
import javax.persistence.InheritanceType;
import javax.persistence.MapKeyClass;
import javax.persistence.MapKeyColumn;
import javax.persistence.MapKeyJoinColumn;
import javax.persistence.MapKeyJoinColumns;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.FetchMode;
import org.hibernate.MappingException;
import org.hibernate.annotations.common.reflection.ClassLoadingException;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.cfg.AccessType;
import org.hibernate.cfg.AnnotatedClassType;
import org.hibernate.cfg.AnnotationBinder;
import org.hibernate.cfg.BinderHelper;
import org.hibernate.cfg.CollectionPropertyHolder;
import org.hibernate.cfg.CollectionSecondPass;
import org.hibernate.cfg.Ejb3Column;
import org.hibernate.cfg.Ejb3JoinColumn;
import org.hibernate.cfg.InheritanceState;
import org.hibernate.cfg.PropertyData;
import org.hibernate.cfg.PropertyHolderBuilder;
import org.hibernate.cfg.PropertyPreloadedData;
import org.hibernate.cfg.SecondPass;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.DependantValue;
import org.hibernate.mapping.Formula;
import org.hibernate.mapping.Join;
import org.hibernate.mapping.ManyToOne;
import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Selectable;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.ToOne;
import org.hibernate.mapping.Value;
import org.hibernate.sql.Template;
public class MapBinder extends CollectionBinder {
public MapBinder(boolean sorted) {
super( sorted );
}
public boolean isMap() {
return true;
}
protected Collection createCollection(PersistentClass persistentClass) {
return new org.hibernate.mapping.Map( getBuildingContext(), persistentClass );
}
@Override
public SecondPass getSecondPass(
final Ejb3JoinColumn[] fkJoinColumns,
final Ejb3JoinColumn[] keyColumns,
final Ejb3JoinColumn[] inverseColumns,
final Ejb3Column[] elementColumns,
final Ejb3Column[] mapKeyColumns,
final Ejb3JoinColumn[] mapKeyManyToManyColumns,
final boolean isEmbedded,
final XProperty property,
final XClass collType,
final boolean ignoreNotFound,
final boolean unique,
final TableBinder assocTableBinder,
final MetadataBuildingContext buildingContext) {
return new CollectionSecondPass( buildingContext, MapBinder.this.collection ) {
public void secondPass(Map persistentClasses, Map inheritedMetas)
throws MappingException {
bindStarToManySecondPass(
persistentClasses, collType, fkJoinColumns, keyColumns, inverseColumns, elementColumns,
isEmbedded, property, unique, assocTableBinder, ignoreNotFound, buildingContext
);
bindKeyFromAssociationTable(
collType, persistentClasses, mapKeyPropertyName, property, isEmbedded, buildingContext,
mapKeyColumns, mapKeyManyToManyColumns,
inverseColumns != null ? inverseColumns[0].getPropertyName() : null
);
makeOneToManyMapKeyColumnNullableIfNotInProperty( property );
}
};
}
private void makeOneToManyMapKeyColumnNullableIfNotInProperty(
final XProperty property) {
final org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection;
if ( map.isOneToMany() &&
property.isAnnotationPresent( MapKeyColumn.class ) ) {
final Value indexValue = map.getIndex();
if ( indexValue.getColumnSpan() != 1 ) {
throw new AssertionFailure( "Map key mapped by @MapKeyColumn does not have 1 column" );
}
final Selectable selectable = indexValue.getColumnIterator().next();
if ( selectable.isFormula() ) {
throw new AssertionFailure( "Map key mapped by @MapKeyColumn is a Formula" );
}
Column column = (Column) map.getIndex().getColumnIterator().next();
if ( !column.isNullable() ) {
final PersistentClass persistentClass = ( ( OneToMany ) map.getElement() ).getAssociatedClass();
if ( !propertyIteratorContainsColumn( persistentClass.getUnjoinedPropertyIterator(), column ) ) {
column.setNullable( true );
}
}
}
}
private boolean propertyIteratorContainsColumn(Iterator propertyIterator, Column column) {
for ( Iterator it = propertyIterator; it.hasNext(); ) {
final Property property = (Property) it.next();
for ( Iterator<Selectable> selectableIterator = property.getColumnIterator(); selectableIterator.hasNext(); ) {
final Selectable selectable = selectableIterator.next();
if ( column.equals( selectable ) ) {
final Column iteratedColumn = (Column) selectable;
if ( column.getValue().getTable().equals( iteratedColumn.getValue().getTable() ) ) {
return true;
}
}
}
}
return false;
}
private void bindKeyFromAssociationTable(
XClass collType,
Map persistentClasses,
String mapKeyPropertyName,
XProperty property,
boolean isEmbedded,
MetadataBuildingContext buildingContext,
Ejb3Column[] mapKeyColumns,
Ejb3JoinColumn[] mapKeyManyToManyColumns,
String targetPropertyName) {
if ( mapKeyPropertyName != null ) {
PersistentClass associatedClass = (PersistentClass) persistentClasses.get( collType.getName() );
if ( associatedClass == null ) throw new AnnotationException( "Associated class not found: " + collType );
Property mapProperty = BinderHelper.findPropertyByName( associatedClass, mapKeyPropertyName );
if ( mapProperty == null ) {
throw new AnnotationException(
"Map key property not found: " + collType + "." + mapKeyPropertyName
);
}
org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection;
InheritanceState inheritanceState = inheritanceStatePerClass.get( collType );
PersistentClass targetPropertyPersistentClass = InheritanceType.JOINED.equals( inheritanceState.getType() ) ?
mapProperty.getPersistentClass() :
associatedClass;
Value indexValue = createFormulatedValue(
mapProperty.getValue(), map, targetPropertyName, associatedClass, targetPropertyPersistentClass, buildingContext
);
map.setIndex( indexValue );
}
else {
String mapKeyType;
Class target = void.class;
if ( property.isAnnotationPresent( MapKeyClass.class ) ) {
target = property.getAnnotation( MapKeyClass.class ).value();
}
if ( !void.class.equals( target ) ) {
mapKeyType = target.getName();
}
else {
mapKeyType = property.getMapKey().getName();
}
PersistentClass collectionEntity = (PersistentClass) persistentClasses.get( mapKeyType );
boolean isIndexOfEntities = collectionEntity != null;
ManyToOne element = null;
org.hibernate.mapping.Map mapValue = (org.hibernate.mapping.Map) this.collection;
if ( isIndexOfEntities ) {
element = new ManyToOne( buildingContext, mapValue.getCollectionTable() );
mapValue.setIndex( element );
element.setReferencedEntityName( mapKeyType );
element.setFetchMode( FetchMode.JOIN );
element.setLazy( false );
}
else {
XClass keyXClass;
AnnotatedClassType classType;
if ( BinderHelper.PRIMITIVE_NAMES.contains( mapKeyType ) ) {
classType = AnnotatedClassType.NONE;
keyXClass = null;
}
else {
try {
keyXClass = buildingContext.getBootstrapContext().getReflectionManager().classForName( mapKeyType );
}
catch (ClassLoadingException e) {
throw new AnnotationException( "Unable to find class: " + mapKeyType, e );
}
classType = buildingContext.getMetadataCollector().getClassType( keyXClass );
if ( isEmbedded || mappingDefinedAttributeOverrideOnMapKey( property ) ) {
classType = AnnotatedClassType.EMBEDDABLE;
}
}
CollectionPropertyHolder holder = PropertyHolderBuilder.buildPropertyHolder(
mapValue,
StringHelper.qualify( mapValue.getRole(), "mapkey" ),
keyXClass,
property,
propertyHolder,
buildingContext
);
propertyHolder.startingProperty( property );
holder.prepare( property );
PersistentClass owner = mapValue.getOwner();
AccessType accessType;
if ( owner.getIdentifierProperty() != null ) {
accessType = owner.getIdentifierProperty().getPropertyAccessorName().equals( "property" )
? AccessType.PROPERTY
: AccessType.FIELD;
}
else if ( owner.getIdentifierMapper() != null && owner.getIdentifierMapper().getPropertySpan() > 0 ) {
Property prop = (Property) owner.getIdentifierMapper().getPropertyIterator().next();
accessType = prop.getPropertyAccessorName().equals( "property" ) ? AccessType.PROPERTY
: AccessType.FIELD;
}
else {
throw new AssertionFailure( "Unable to guess collection property accessor name" );
}
if ( AnnotatedClassType.EMBEDDABLE.equals( classType ) ) {
EntityBinder entityBinder = new EntityBinder();
PropertyData inferredData;
if ( isHibernateExtensionMapping() ) {
inferredData = new PropertyPreloadedData( AccessType.PROPERTY, "index", keyXClass );
}
else {
inferredData = new PropertyPreloadedData( AccessType.PROPERTY, "key", keyXClass );
}
Component component = AnnotationBinder.fillComponent(
holder,
inferredData,
accessType,
true,
entityBinder,
false,
false,
true,
buildingContext,
inheritanceStatePerClass
);
mapValue.setIndex( component );
}
else {
SimpleValueBinder elementBinder = new SimpleValueBinder();
elementBinder.setBuildingContext( buildingContext );
elementBinder.setReturnedClassName( mapKeyType );
Ejb3Column[] elementColumns = mapKeyColumns;
if ( elementColumns == null || elementColumns.length == 0 ) {
elementColumns = new Ejb3Column[1];
Ejb3Column column = new Ejb3Column();
column.setImplicit( false );
column.setNullable( true );
column.setLength( Ejb3Column.DEFAULT_COLUMN_LENGTH );
column.setLogicalColumnName( Collection.DEFAULT_KEY_COLUMN_NAME );
column.setJoins( new HashMap<String, Join>() );
column.setBuildingContext( buildingContext );
column.bind();
elementColumns[0] = column;
}
for (Ejb3Column column : elementColumns) {
column.setTable( mapValue.getCollectionTable() );
}
elementBinder.setColumns( elementColumns );
elementBinder.setKey(true);
elementBinder.setType(
property,
keyXClass,
this.collection.getOwnerEntityName(),
holder.mapKeyAttributeConverterDescriptor( property, keyXClass )
);
elementBinder.setPersistentClassName( propertyHolder.getEntityName() );
elementBinder.setAccessType( accessType );
mapValue.setIndex( elementBinder.make() );
}
}
if ( !collection.isOneToMany() ) {
for (Ejb3JoinColumn col : mapKeyManyToManyColumns) {
col.forceNotNull();
}
}
if ( element != null ) {
final javax.persistence.ForeignKey foreignKey = getMapKeyForeignKey( property );
if ( foreignKey != null ) {
if ( foreignKey.value() == ConstraintMode.NO_CONSTRAINT ) {
element.setForeignKeyName( "none" );
}
else {
element.setForeignKeyName( StringHelper.nullIfEmpty( foreignKey.name() ) );
element.setForeignKeyDefinition( StringHelper.nullIfEmpty( foreignKey.foreignKeyDefinition() ) );
}
}
}
if ( isIndexOfEntities ) {
bindManytoManyInverseFk(
collectionEntity,
mapKeyManyToManyColumns,
element,
false,
buildingContext
);
}
}
}
private javax.persistence.ForeignKey getMapKeyForeignKey(XProperty property) {
final MapKeyJoinColumns mapKeyJoinColumns = property.getAnnotation( MapKeyJoinColumns.class );
if ( mapKeyJoinColumns != null ) {
return mapKeyJoinColumns.foreignKey();
}
else {
final MapKeyJoinColumn mapKeyJoinColumn = property.getAnnotation( MapKeyJoinColumn.class );
if ( mapKeyJoinColumn != null ) {
return mapKeyJoinColumn.foreignKey();
}
}
return null;
}
private boolean mappingDefinedAttributeOverrideOnMapKey(XProperty property) {
if ( property.isAnnotationPresent( AttributeOverride.class ) ) {
return namedMapKey( property.getAnnotation( AttributeOverride.class ) );
}
if ( property.isAnnotationPresent( AttributeOverrides.class ) ) {
final AttributeOverrides annotations = property.getAnnotation( AttributeOverrides.class );
for ( AttributeOverride attributeOverride : annotations.value() ) {
if ( namedMapKey( attributeOverride ) ) {
return true;
}
}
}
return false;
}
private boolean namedMapKey(AttributeOverride annotation) {
return annotation.name().startsWith( "key." );
}
protected Value createFormulatedValue(
Value value,
Collection collection,
String targetPropertyName,
PersistentClass associatedClass,
PersistentClass targetPropertyPersistentClass,
MetadataBuildingContext buildingContext) {
Value element = collection.getElement();
String fromAndWhere = null;
if ( !( element instanceof OneToMany ) ) {
String referencedPropertyName = null;
if ( element instanceof ToOne ) {
referencedPropertyName = ( (ToOne) element ).getReferencedPropertyName();
}
else if ( element instanceof DependantValue ) {
if ( propertyName != null ) {
referencedPropertyName = collection.getReferencedPropertyName();
}
else {
throw new AnnotationException( "SecondaryTable JoinColumn cannot reference a non primary key" );
}
}
Iterator<Selectable> referencedEntityColumns;
if ( referencedPropertyName == null ) {
referencedEntityColumns = associatedClass.getIdentifier().getColumnIterator();
}
else {
Property referencedProperty = associatedClass.getRecursiveProperty( referencedPropertyName );
referencedEntityColumns = referencedProperty.getColumnIterator();
}
fromAndWhere = getFromAndWhereFormula(
associatedClass.getTable().getName(),
element.getColumnIterator(),
referencedEntityColumns
);
}
else {
if ( !associatedClass.equals( targetPropertyPersistentClass ) ) {
fromAndWhere = getFromAndWhereFormula(
targetPropertyPersistentClass.getTable()
.getQualifiedTableName()
.toString(),
element.getColumnIterator(),
associatedClass.getIdentifier().getColumnIterator()
);
}
}
if ( value instanceof Component ) {
Component component = (Component) value;
Iterator properties = component.getPropertyIterator();
Component indexComponent = new Component( getBuildingContext(), collection );
indexComponent.setComponentClassName( component.getComponentClassName() );
while ( properties.hasNext() ) {
Property current = (Property) properties.next();
Property newProperty = new Property();
newProperty.setCascade( current.getCascade() );
newProperty.setValueGenerationStrategy( current.getValueGenerationStrategy() );
newProperty.setInsertable( false );
newProperty.setUpdateable( false );
newProperty.setMetaAttributes( current.getMetaAttributes() );
newProperty.setName( current.getName() );
newProperty.setNaturalIdentifier( false );
newProperty.setOptional( false );
newProperty.setPersistentClass( current.getPersistentClass() );
newProperty.setPropertyAccessorName( current.getPropertyAccessorName() );
newProperty.setSelectable( current.isSelectable() );
newProperty.setValue(
createFormulatedValue(
current.getValue(), collection, targetPropertyName, associatedClass, associatedClass, buildingContext
)
);
indexComponent.addProperty( newProperty );
}
return indexComponent;
}
else if ( value instanceof SimpleValue ) {
SimpleValue sourceValue = (SimpleValue) value;
SimpleValue targetValue;
if ( value instanceof ManyToOne ) {
ManyToOne sourceManyToOne = (ManyToOne) sourceValue;
ManyToOne targetManyToOne = new ManyToOne( getBuildingContext(), collection.getCollectionTable() );
targetManyToOne.setFetchMode( FetchMode.DEFAULT );
targetManyToOne.setLazy( true );
targetManyToOne.setReferencedEntityName( sourceManyToOne.getReferencedEntityName() );
targetValue = targetManyToOne;
}
else {
targetValue = new SimpleValue( getBuildingContext(), collection.getCollectionTable() );
targetValue.copyTypeFrom( sourceValue );
}
Iterator columns = sourceValue.getColumnIterator();
Random random = new Random();
while ( columns.hasNext() ) {
Object current = columns.next();
Formula formula = new Formula();
String formulaString;
if ( current instanceof Column ) {
formulaString = ( (Column) current ).getQuotedName();
}
else if ( current instanceof Formula ) {
formulaString = ( (Formula) current ).getFormula();
}
else {
throw new AssertionFailure( "Unknown element in column iterator: " + current.getClass() );
}
if ( fromAndWhere != null ) {
formulaString = Template.renderWhereStringTemplate( formulaString, "$alias$", new HSQLDialect() );
formulaString = "(select " + formulaString + fromAndWhere + ")";
formulaString = StringHelper.replace(
formulaString,
"$alias$",
"a" + random.nextInt( 16 )
);
}
formula.setFormula( formulaString );
targetValue.addFormula( formula );
}
return targetValue;
}
else {
throw new AssertionFailure( "Unknown type encounters for map key: " + value.getClass() );
}
}
private String getFromAndWhereFormula(
String tableName,
Iterator<Selectable> collectionTableColumns,
Iterator<Selectable> referencedEntityColumns) {
String alias = "$alias$";
StringBuilder fromAndWhereSb = new StringBuilder( " from " )
.append( tableName )
.append( " " )
.append( alias ).append( " where " );
while ( collectionTableColumns.hasNext() ) {
Column colColumn = (Column) collectionTableColumns.next();
Column refColumn = (Column) referencedEntityColumns.next();
fromAndWhereSb.append( alias )
.append( '.' )
.append( refColumn.getQuotedName() )
.append( '=' )
.append( colColumn.getQuotedName() )
.append( " and " );
}
return fromAndWhereSb.substring( 0, fromAndWhereSb.length() - 5 );
}
}