use of org.hibernate.mapping.JoinedSubclass in project hibernate-orm by hibernate.
the class TableBinder method bindFk.
public static void bindFk(PersistentClass referencedEntity, PersistentClass destinationEntity, Ejb3JoinColumn[] columns, SimpleValue value, boolean unique, MetadataBuildingContext buildingContext) {
PersistentClass associatedClass;
if (destinationEntity != null) {
// overridden destination
associatedClass = destinationEntity;
} else {
associatedClass = columns[0].getPropertyHolder() == null ? null : columns[0].getPropertyHolder().getPersistentClass();
}
final String mappedByProperty = columns[0].getMappedBy();
if (StringHelper.isNotEmpty(mappedByProperty)) {
/**
* Get the columns of the mapped-by property
* copy them and link the copy to the actual value
*/
LOG.debugf("Retrieving property %s.%s", associatedClass.getEntityName(), mappedByProperty);
final Property property = associatedClass.getRecursiveProperty(columns[0].getMappedBy());
Iterator mappedByColumns;
if (property.getValue() instanceof Collection) {
Collection collection = ((Collection) property.getValue());
Value element = collection.getElement();
if (element == null) {
throw new AnnotationException("Illegal use of mappedBy on both sides of the relationship: " + associatedClass.getEntityName() + "." + mappedByProperty);
}
mappedByColumns = element.getColumnIterator();
} else {
mappedByColumns = property.getValue().getColumnIterator();
}
while (mappedByColumns.hasNext()) {
Column column = (Column) mappedByColumns.next();
columns[0].overrideFromReferencedColumnIfNecessary(column);
columns[0].linkValueUsingAColumnCopy(column, value);
}
} else if (columns[0].isImplicit()) {
/**
* if columns are implicit, then create the columns based on the
* referenced entity id columns
*/
Iterator idColumns;
if (referencedEntity instanceof JoinedSubclass) {
idColumns = referencedEntity.getKey().getColumnIterator();
} else {
idColumns = referencedEntity.getIdentifier().getColumnIterator();
}
while (idColumns.hasNext()) {
Column column = (Column) idColumns.next();
columns[0].linkValueUsingDefaultColumnNaming(column, referencedEntity, value);
columns[0].overrideFromReferencedColumnIfNecessary(column);
}
} else {
int fkEnum = Ejb3JoinColumn.checkReferencedColumnsType(columns, referencedEntity, buildingContext);
if (Ejb3JoinColumn.NON_PK_REFERENCE == fkEnum) {
String referencedPropertyName;
if (value instanceof ToOne) {
referencedPropertyName = ((ToOne) value).getReferencedPropertyName();
} else if (value instanceof DependantValue) {
String propertyName = columns[0].getPropertyName();
if (propertyName != null) {
Collection collection = (Collection) referencedEntity.getRecursiveProperty(propertyName).getValue();
referencedPropertyName = collection.getReferencedPropertyName();
} else {
throw new AnnotationException("SecondaryTable JoinColumn cannot reference a non primary key");
}
} else {
throw new AssertionFailure("Do a property ref on an unexpected Value type: " + value.getClass().getName());
}
if (referencedPropertyName == null) {
throw new AssertionFailure("No property ref found while expected");
}
Property synthProp = referencedEntity.getReferencedProperty(referencedPropertyName);
if (synthProp == null) {
throw new AssertionFailure("Cannot find synthProp: " + referencedEntity.getEntityName() + "." + referencedPropertyName);
}
linkJoinColumnWithValueOverridingNameIfImplicit(referencedEntity, synthProp.getColumnIterator(), columns, value);
} else {
if (Ejb3JoinColumn.NO_REFERENCE == fkEnum) {
// implicit case, we hope PK and FK columns are in the same order
if (columns.length != referencedEntity.getIdentifier().getColumnSpan()) {
throw new AnnotationException("A Foreign key refering " + referencedEntity.getEntityName() + " from " + associatedClass.getEntityName() + " has the wrong number of column. should be " + referencedEntity.getIdentifier().getColumnSpan());
}
linkJoinColumnWithValueOverridingNameIfImplicit(referencedEntity, referencedEntity.getIdentifier().getColumnIterator(), columns, value);
} else {
// explicit referencedColumnName
Iterator idColItr = referencedEntity.getKey().getColumnIterator();
org.hibernate.mapping.Column col;
// works cause the pk has to be on the primary table
Table table = referencedEntity.getTable();
if (!idColItr.hasNext()) {
LOG.debug("No column in the identifier!");
}
while (idColItr.hasNext()) {
boolean match = false;
// for each PK column, find the associated FK column.
col = (org.hibernate.mapping.Column) idColItr.next();
for (Ejb3JoinColumn joinCol : columns) {
String referencedColumn = joinCol.getReferencedColumn();
referencedColumn = buildingContext.getMetadataCollector().getPhysicalColumnName(table, referencedColumn);
// In JPA 2 referencedColumnName is case insensitive
if (referencedColumn.equalsIgnoreCase(col.getQuotedName(buildingContext.getMetadataCollector().getDatabase().getJdbcEnvironment().getDialect()))) {
// proper join column
if (joinCol.isNameDeferred()) {
joinCol.linkValueUsingDefaultColumnNaming(col, referencedEntity, value);
} else {
joinCol.linkWithValue(value);
}
joinCol.overrideFromReferencedColumnIfNecessary(col);
match = true;
break;
}
}
if (!match) {
throw new AnnotationException("Column name " + col.getName() + " of " + referencedEntity.getEntityName() + " not found in JoinColumns.referencedColumnName");
}
}
}
}
}
value.createForeignKey();
if (unique) {
createUniqueConstraint(value);
}
}
use of org.hibernate.mapping.JoinedSubclass in project hibernate-orm by hibernate.
the class ModelBinder method bindJoinedSubclassEntities.
private void bindJoinedSubclassEntities(AbstractEntitySourceImpl entitySource, PersistentClass superEntityDescriptor) {
for (IdentifiableTypeSource subType : entitySource.getSubTypes()) {
final JoinedSubclass subEntityDescriptor = new JoinedSubclass(superEntityDescriptor, metadataBuildingContext);
bindJoinedSubclassEntity((JoinedSubclassEntitySourceImpl) subType, subEntityDescriptor);
superEntityDescriptor.addSubclass(subEntityDescriptor);
entitySource.getLocalMetadataBuildingContext().getMetadataCollector().addEntityBinding(subEntityDescriptor);
}
}
use of org.hibernate.mapping.JoinedSubclass in project hibernate-orm by hibernate.
the class IdTableHelper method needsIdTable.
public boolean needsIdTable(PersistentClass entityBinding) {
// need id table if the entity has secondary tables (joins)
if (entityBinding.getJoinClosureSpan() > 0) {
return true;
}
// need an id table if the entity is part of either a JOINED or UNION inheritance
// hierarchy. We do not allow inheritance strategy mixing, so work on that assumption
// here...
final RootClass rootEntityBinding = entityBinding.getRootClass();
final Iterator itr = rootEntityBinding.getSubclassIterator();
if (itr.hasNext()) {
final Subclass subclassEntityBinding = (Subclass) itr.next();
if (subclassEntityBinding instanceof JoinedSubclass || subclassEntityBinding instanceof UnionSubclass) {
return true;
}
}
return false;
}
use of org.hibernate.mapping.JoinedSubclass in project hibernate-orm by hibernate.
the class PersistentClassVisitorTest method testProperCallbacks.
@Test
public void testProperCallbacks() {
PersistentClassVisitorValidator vv = new PersistentClassVisitorValidator();
new RootClass(metadataBuildingContext).accept(vv);
new Subclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
new JoinedSubclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
new SingleTableSubclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
new UnionSubclass(new RootClass(metadataBuildingContext), metadataBuildingContext).accept(vv);
}
use of org.hibernate.mapping.JoinedSubclass in project jbosstools-hibernate by jbosstools.
the class ServiceImpl method newJoinedSubclass.
@Override
public IPersistentClass newJoinedSubclass(IPersistentClass persistentClass) {
assert persistentClass instanceof IFacade;
IPersistentClass result = facadeFactory.createPersistentClass(new JoinedSubclass((PersistentClass) ((IFacade) persistentClass).getTarget()));
((AbstractPersistentClassFacade) result).setSuperClass(persistentClass);
return result;
}
Aggregations