use of org.hibernate.mapping.ManyToOne in project jbosstools-hibernate by jbosstools.
the class ValueFacadeTest method testIsManyToOne.
@Test
public void testIsManyToOne() {
valueTarget = new SimpleValue(null);
valueFacade = FACADE_FACTORY.createValue(valueTarget);
Assert.assertFalse(valueFacade.isManyToOne());
ManyToOne manyToOne = new ManyToOne(null, null);
valueFacade = FACADE_FACTORY.createValue(manyToOne);
Assert.assertTrue(valueFacade.isManyToOne());
}
use of org.hibernate.mapping.ManyToOne in project jbosstools-hibernate by jbosstools.
the class ValueFacadeTest method testGetReferencedEntityName.
@Test
public void testGetReferencedEntityName() {
ManyToOne valueTarget = new ManyToOne(null, null);
valueFacade = FACADE_FACTORY.createValue(valueTarget);
Assert.assertNull(valueFacade.getReferencedEntityName());
valueTarget.setReferencedEntityName("Foo");
Assert.assertEquals("Foo", valueFacade.getReferencedEntityName());
}
use of org.hibernate.mapping.ManyToOne in project jbosstools-hibernate by jbosstools.
the class ServiceImplTest method testNewManyToOne.
@Test
public void testNewManyToOne() {
ITable table = service.newTable("foo");
IValue manyToOne = service.newManyToOne(table);
Assert.assertNotNull(manyToOne);
Object target = ((IFacade) manyToOne).getTarget();
Assert.assertNotNull(target);
Assert.assertTrue(target instanceof ManyToOne);
}
use of org.hibernate.mapping.ManyToOne in project hibernate-orm by hibernate.
the class ModelBinder method bindAllEntityAttributes.
private void bindAllEntityAttributes(MappingDocument mappingDocument, EntitySource entitySource, PersistentClass entityDescriptor) {
final EntityTableXref entityTableXref = mappingDocument.getMetadataCollector().getEntityTableXref(entityDescriptor.getEntityName());
if (entityTableXref == null) {
throw new AssertionFailure(String.format(Locale.ENGLISH, "Unable to locate EntityTableXref for entity [%s] : %s", entityDescriptor.getEntityName(), mappingDocument.getOrigin()));
}
// make sure we bind secondary tables first!
for (SecondaryTableSource secondaryTableSource : entitySource.getSecondaryTableMap().values()) {
final Join secondaryTableJoin = new Join();
secondaryTableJoin.setPersistentClass(entityDescriptor);
bindSecondaryTable(mappingDocument, secondaryTableSource, secondaryTableJoin, entityTableXref);
entityDescriptor.addJoin(secondaryTableJoin);
}
for (AttributeSource attributeSource : entitySource.attributeSources()) {
if (PluralAttributeSource.class.isInstance(attributeSource)) {
// plural attribute
final Property attribute = createPluralAttribute(mappingDocument, (PluralAttributeSource) attributeSource, entityDescriptor);
entityDescriptor.addProperty(attribute);
} else {
// singular attribute
if (SingularAttributeSourceBasic.class.isInstance(attributeSource)) {
final SingularAttributeSourceBasic basicAttributeSource = (SingularAttributeSourceBasic) attributeSource;
final Identifier tableName = determineTable(mappingDocument, basicAttributeSource.getName(), basicAttributeSource);
final AttributeContainer attributeContainer;
final Table table;
final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
if (secondaryTableJoin == null) {
table = entityDescriptor.getTable();
attributeContainer = entityDescriptor;
} else {
table = secondaryTableJoin.getTable();
attributeContainer = secondaryTableJoin;
}
final Property attribute = createBasicAttribute(mappingDocument, basicAttributeSource, new SimpleValue(mappingDocument, table), entityDescriptor.getClassName());
if (secondaryTableJoin != null) {
attribute.setOptional(secondaryTableJoin.isOptional());
}
attributeContainer.addProperty(attribute);
handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, basicAttributeSource.getNaturalIdMutability());
} else if (SingularAttributeSourceEmbedded.class.isInstance(attributeSource)) {
final SingularAttributeSourceEmbedded embeddedAttributeSource = (SingularAttributeSourceEmbedded) attributeSource;
final Identifier tableName = determineTable(mappingDocument, embeddedAttributeSource);
final AttributeContainer attributeContainer;
final Table table;
final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
if (secondaryTableJoin == null) {
table = entityDescriptor.getTable();
attributeContainer = entityDescriptor;
} else {
table = secondaryTableJoin.getTable();
attributeContainer = secondaryTableJoin;
}
final Property attribute = createEmbeddedAttribute(mappingDocument, (SingularAttributeSourceEmbedded) attributeSource, new Component(mappingDocument, table, entityDescriptor), entityDescriptor.getClassName());
if (secondaryTableJoin != null) {
attribute.setOptional(secondaryTableJoin.isOptional());
}
attributeContainer.addProperty(attribute);
handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, embeddedAttributeSource.getNaturalIdMutability());
} else if (SingularAttributeSourceManyToOne.class.isInstance(attributeSource)) {
final SingularAttributeSourceManyToOne manyToOneAttributeSource = (SingularAttributeSourceManyToOne) attributeSource;
final Identifier tableName = determineTable(mappingDocument, manyToOneAttributeSource.getName(), manyToOneAttributeSource);
final AttributeContainer attributeContainer;
final Table table;
final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
if (secondaryTableJoin == null) {
table = entityDescriptor.getTable();
attributeContainer = entityDescriptor;
} else {
table = secondaryTableJoin.getTable();
attributeContainer = secondaryTableJoin;
}
final Property attribute = createManyToOneAttribute(mappingDocument, manyToOneAttributeSource, new ManyToOne(mappingDocument, table), entityDescriptor.getClassName());
if (secondaryTableJoin != null) {
attribute.setOptional(secondaryTableJoin.isOptional());
}
attributeContainer.addProperty(attribute);
handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, manyToOneAttributeSource.getNaturalIdMutability());
} else if (SingularAttributeSourceOneToOne.class.isInstance(attributeSource)) {
final SingularAttributeSourceOneToOne oneToOneAttributeSource = (SingularAttributeSourceOneToOne) attributeSource;
final Table table = entityDescriptor.getTable();
final Property attribute = createOneToOneAttribute(mappingDocument, oneToOneAttributeSource, new OneToOne(mappingDocument, table, entityDescriptor), entityDescriptor.getClassName());
entityDescriptor.addProperty(attribute);
handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, oneToOneAttributeSource.getNaturalIdMutability());
} else if (SingularAttributeSourceAny.class.isInstance(attributeSource)) {
final SingularAttributeSourceAny anyAttributeSource = (SingularAttributeSourceAny) attributeSource;
final Identifier tableName = determineTable(mappingDocument, anyAttributeSource.getName(), anyAttributeSource.getKeySource().getRelationalValueSources());
final AttributeContainer attributeContainer;
final Table table;
final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
if (secondaryTableJoin == null) {
table = entityDescriptor.getTable();
attributeContainer = entityDescriptor;
} else {
table = secondaryTableJoin.getTable();
attributeContainer = secondaryTableJoin;
}
final Property attribute = createAnyAssociationAttribute(mappingDocument, anyAttributeSource, new Any(mappingDocument, table), entityDescriptor.getEntityName());
if (secondaryTableJoin != null) {
attribute.setOptional(secondaryTableJoin.isOptional());
}
attributeContainer.addProperty(attribute);
handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, anyAttributeSource.getNaturalIdMutability());
}
}
}
}
use of org.hibernate.mapping.ManyToOne in project hibernate-orm by hibernate.
the class ToOneFkSecondPass method doSecondPass.
public void doSecondPass(java.util.Map persistentClasses) throws MappingException {
if (value instanceof ManyToOne) {
ManyToOne manyToOne = (ManyToOne) value;
PersistentClass ref = (PersistentClass) persistentClasses.get(manyToOne.getReferencedEntityName());
if (ref == null) {
throw new AnnotationException("@OneToOne or @ManyToOne on " + StringHelper.qualify(entityClassName, path) + " references an unknown entity: " + manyToOne.getReferencedEntityName());
}
manyToOne.setPropertyName(path);
BinderHelper.createSyntheticPropertyReference(columns, ref, null, manyToOne, false, buildingContext);
TableBinder.bindFk(ref, null, columns, manyToOne, unique, buildingContext);
/*
* HbmMetadataSourceProcessorImpl does this only when property-ref != null, but IMO, it makes sense event if it is null
*/
if (!manyToOne.isIgnoreNotFound())
manyToOne.createPropertyRefConstraints(persistentClasses);
} else if (value instanceof OneToOne) {
value.createForeignKey();
} else {
throw new AssertionFailure("FkSecondPass for a wrong value type: " + value.getClass().getName());
}
}
Aggregations