use of org.hibernate.mapping.Join in project hibernate-orm by hibernate.
the class CollectionBinder method bindOneToManySecondPass.
protected void bindOneToManySecondPass(Collection collection, Map persistentClasses, Ejb3JoinColumn[] fkJoinColumns, XClass collectionType, boolean cascadeDeleteEnabled, boolean ignoreNotFound, MetadataBuildingContext buildingContext, Map<XClass, InheritanceState> inheritanceStatePerClass) {
final boolean debugEnabled = LOG.isDebugEnabled();
if (debugEnabled) {
LOG.debugf("Binding a OneToMany: %s.%s through a foreign key", propertyHolder.getEntityName(), propertyName);
}
if (buildingContext == null) {
throw new AssertionFailure("CollectionSecondPass for oneToMany should not be called with null mappings");
}
org.hibernate.mapping.OneToMany oneToMany = new org.hibernate.mapping.OneToMany(buildingContext.getMetadataCollector(), collection.getOwner());
collection.setElement(oneToMany);
oneToMany.setReferencedEntityName(collectionType.getName());
oneToMany.setIgnoreNotFound(ignoreNotFound);
String assocClass = oneToMany.getReferencedEntityName();
PersistentClass associatedClass = (PersistentClass) persistentClasses.get(assocClass);
if (jpaOrderBy != null) {
final String orderByFragment = buildOrderByClauseFromHql(jpaOrderBy.value(), associatedClass, collection.getRole());
if (StringHelper.isNotEmpty(orderByFragment)) {
collection.setOrderBy(orderByFragment);
}
}
Map<String, Join> joins = buildingContext.getMetadataCollector().getJoins(assocClass);
if (associatedClass == null) {
throw new MappingException(String.format("Association [%s] for entity [%s] references unmapped class [%s]", propertyName, propertyHolder.getClassName(), assocClass));
}
oneToMany.setAssociatedClass(associatedClass);
for (Ejb3JoinColumn column : fkJoinColumns) {
column.setPersistentClass(associatedClass, joins, inheritanceStatePerClass);
column.setJoins(joins);
collection.setCollectionTable(column.getTable());
}
if (debugEnabled) {
LOG.debugf("Mapping collection: %s -> %s", collection.getRole(), collection.getCollectionTable().getName());
}
bindFilters(false);
bindCollectionSecondPass(collection, null, fkJoinColumns, cascadeDeleteEnabled, property, propertyHolder, buildingContext);
if (!collection.isInverse() && !collection.getKey().isNullable()) {
// for non-inverse one-to-many, with a not-null fk, add a backref!
String entityName = oneToMany.getReferencedEntityName();
PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding(entityName);
Backref prop = new Backref();
prop.setName('_' + fkJoinColumns[0].getPropertyName() + '_' + fkJoinColumns[0].getLogicalColumnName() + "Backref");
prop.setUpdateable(false);
prop.setSelectable(false);
prop.setCollectionRole(collection.getRole());
prop.setEntityName(collection.getOwner().getEntityName());
prop.setValue(collection.getKey());
referenced.addProperty(prop);
}
}
use of org.hibernate.mapping.Join in project hibernate-orm by hibernate.
the class CollectionBinder method bindManytoManyInverseFk.
/**
* bind the inverse FK of a ManyToMany
* If we are in a mappedBy case, read the columns from the associated
* collection element
* Otherwise delegates to the usual algorithm
*/
public static void bindManytoManyInverseFk(PersistentClass referencedEntity, Ejb3JoinColumn[] columns, SimpleValue value, boolean unique, MetadataBuildingContext buildingContext) {
final String mappedBy = columns[0].getMappedBy();
if (StringHelper.isNotEmpty(mappedBy)) {
final Property property = referencedEntity.getRecursiveProperty(mappedBy);
Iterator mappedByColumns;
if (property.getValue() instanceof Collection) {
mappedByColumns = ((Collection) property.getValue()).getKey().getColumnIterator();
} else {
//find the appropriate reference key, can be in a join
Iterator joinsIt = referencedEntity.getJoinIterator();
KeyValue key = null;
while (joinsIt.hasNext()) {
Join join = (Join) joinsIt.next();
if (join.containsProperty(property)) {
key = join.getKey();
break;
}
}
if (key == null)
key = property.getPersistentClass().getIdentifier();
mappedByColumns = key.getColumnIterator();
}
while (mappedByColumns.hasNext()) {
Column column = (Column) mappedByColumns.next();
columns[0].linkValueUsingAColumnCopy(column, value);
}
String referencedPropertyName = buildingContext.getMetadataCollector().getPropertyReferencedAssociation("inverse__" + referencedEntity.getEntityName(), mappedBy);
if (referencedPropertyName != null) {
//TODO always a many to one?
((ManyToOne) value).setReferencedPropertyName(referencedPropertyName);
buildingContext.getMetadataCollector().addUniquePropertyReference(referencedEntity.getEntityName(), referencedPropertyName);
}
((ManyToOne) value).setReferenceToPrimaryKey(referencedPropertyName == null);
value.createForeignKey();
} else {
BinderHelper.createSyntheticPropertyReference(columns, referencedEntity, null, value, true, buildingContext);
TableBinder.bindFk(referencedEntity, null, columns, value, unique, buildingContext);
}
}
use of org.hibernate.mapping.Join in project hibernate-orm by hibernate.
the class MapBinder method bindKeyFromAssociationTable.
private void bindKeyFromAssociationTable(XClass collType, Map persistentClasses, String mapKeyPropertyName, XProperty property, boolean isEmbedded, MetadataBuildingContext buildingContext, Ejb3Column[] mapKeyColumns, Ejb3JoinColumn[] mapKeyManyToManyColumns, String targetPropertyName) {
if (mapKeyPropertyName != null) {
//this is an EJB3 @MapKey
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;
// HHH-11005 - if InheritanceType.JOINED then need to find class defining the column
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 {
//this is a true Map mapping
//TODO ugly copy/pastle from CollectionBinder.bindManyToManySecondPass
String mapKeyType;
Class target = void.class;
/*
* target has priority over reflection for the map key type
* JPA 2 has priority
*/
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.getMetadataCollector(), mapValue.getCollectionTable());
mapValue.setIndex(element);
element.setReferencedEntityName(mapKeyType);
//element.setFetchMode( fetchMode );
//element.setLazy( fetchMode != FetchMode.JOIN );
//make the second join non lazy
element.setFetchMode(FetchMode.JOIN);
element.setLazy(false);
//does not make sense for a map key element.setIgnoreNotFound( ignoreNotFound );
} else {
XClass keyXClass;
AnnotatedClassType classType;
if (BinderHelper.PRIMITIVE_NAMES.contains(mapKeyType)) {
classType = AnnotatedClassType.NONE;
keyXClass = null;
} else {
try {
keyXClass = buildingContext.getBuildingOptions().getReflectionManager().classForName(mapKeyType);
} catch (ClassLoadingException e) {
throw new AnnotationException("Unable to find class: " + mapKeyType, e);
}
classType = buildingContext.getMetadataCollector().getClassType(keyXClass);
// force in case of attribute override naming the key
if (isEmbedded || mappingDefinedAttributeOverrideOnMapKey(property)) {
classType = AnnotatedClassType.EMBEDDABLE;
}
}
CollectionPropertyHolder holder = PropertyHolderBuilder.buildPropertyHolder(mapValue, StringHelper.qualify(mapValue.getRole(), "mapkey"), keyXClass, property, propertyHolder, buildingContext);
// 'propertyHolder' is the PropertyHolder for the owner of the collection
// 'holder' is the CollectionPropertyHolder.
// 'property' is the collection XProperty
propertyHolder.startingProperty(property);
holder.prepare(property);
PersistentClass owner = mapValue.getOwner();
AccessType accessType;
// String accessType = access != null ? access.value() : null;
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 {
//"key" is the JPA 2 prefix for map keys
inferredData = new PropertyPreloadedData(AccessType.PROPERTY, "key", keyXClass);
}
//TODO be smart with isNullable
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);
//TODO create an EMPTY_JOINS collection
column.setJoins(new HashMap<String, Join>());
column.setBuildingContext(buildingContext);
column.bind();
elementColumns[0] = column;
}
//override the table
for (Ejb3Column column : elementColumns) {
column.setTable(mapValue.getCollectionTable());
}
elementBinder.setColumns(elementColumns);
//do not call setType as it extract the type from @Type
//the algorithm generally does not apply for map key anyway
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());
}
}
//FIXME pass the Index Entity JoinColumns
if (!collection.isOneToMany()) {
//index column shoud not be null
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, //a map key column has no unique constraint
false, buildingContext);
}
}
}
use of org.hibernate.mapping.Join in project hibernate-orm by hibernate.
the class ClassPropertyHolder method addProperty.
public void addProperty(Property prop, XClass declaringClass) {
if (prop.getValue() instanceof Component) {
//TODO handle quote and non quote table comparison
String tableName = prop.getValue().getTable().getName();
if (getJoinsPerRealTableName().containsKey(tableName)) {
final Join join = getJoinsPerRealTableName().get(tableName);
addPropertyToJoin(prop, declaringClass, join);
} else {
addPropertyToPersistentClass(prop, declaringClass);
}
} else {
addPropertyToPersistentClass(prop, declaringClass);
}
}
use of org.hibernate.mapping.Join in project hibernate-orm by hibernate.
the class ClassPropertyHolder method addJoin.
public Join addJoin(JoinTable joinTableAnn, boolean noDelayInPkColumnCreation) {
Join join = entityBinder.addJoin(joinTableAnn, this, noDelayInPkColumnCreation);
this.joins = entityBinder.getSecondaryTables();
return join;
}
Aggregations