Search in sources :

Example 21 with ManyToOne

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());
}
Also used : ManyToOne(org.hibernate.mapping.ManyToOne) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test)

Example 22 with ManyToOne

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());
}
Also used : ManyToOne(org.hibernate.mapping.ManyToOne) Test(org.junit.Test)

Example 23 with ManyToOne

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);
}
Also used : IValue(org.jboss.tools.hibernate.runtime.spi.IValue) ITable(org.jboss.tools.hibernate.runtime.spi.ITable) IFacade(org.jboss.tools.hibernate.runtime.common.IFacade) ManyToOne(org.hibernate.mapping.ManyToOne) Test(org.junit.Test)

Example 24 with 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());
            }
        }
    }
}
Also used : SingularAttributeSourceBasic(org.hibernate.boot.model.source.spi.SingularAttributeSourceBasic) AssertionFailure(org.hibernate.AssertionFailure) VersionAttributeSource(org.hibernate.boot.model.source.spi.VersionAttributeSource) AttributeSource(org.hibernate.boot.model.source.spi.AttributeSource) PluralAttributeSource(org.hibernate.boot.model.source.spi.PluralAttributeSource) SingularAttributeSource(org.hibernate.boot.model.source.spi.SingularAttributeSource) Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) Join(org.hibernate.mapping.Join) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) AttributeContainer(org.hibernate.mapping.AttributeContainer) SecondaryTableSource(org.hibernate.boot.model.source.spi.SecondaryTableSource) SingularAttributeSourceOneToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceOneToOne) PluralAttributeElementSourceManyToAny(org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny) Any(org.hibernate.mapping.Any) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) SingularAttributeSourceEmbedded(org.hibernate.boot.model.source.spi.SingularAttributeSourceEmbedded) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) ManyToOne(org.hibernate.mapping.ManyToOne) SimpleValue(org.hibernate.mapping.SimpleValue) OneToOne(org.hibernate.mapping.OneToOne) SingularAttributeSourceOneToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceOneToOne) Identifier(org.hibernate.boot.model.naming.Identifier) EntityTableXref(org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Example 25 with ManyToOne

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());
    }
}
Also used : OneToOne(org.hibernate.mapping.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) AnnotationException(org.hibernate.AnnotationException) ManyToOne(org.hibernate.mapping.ManyToOne) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

ManyToOne (org.hibernate.mapping.ManyToOne)48 Test (org.junit.Test)37 SimpleValue (org.hibernate.mapping.SimpleValue)22 Collection (org.hibernate.mapping.Collection)10 Bag (org.hibernate.mapping.Bag)9 IdentifierBag (org.hibernate.mapping.IdentifierBag)9 Component (org.hibernate.mapping.Component)8 Property (org.hibernate.mapping.Property)7 AnnotationException (org.hibernate.AnnotationException)5 AssertionFailure (org.hibernate.AssertionFailure)5 Any (org.hibernate.mapping.Any)5 OneToOne (org.hibernate.mapping.OneToOne)5 PersistentClass (org.hibernate.mapping.PersistentClass)5 XProperty (org.hibernate.annotations.common.reflection.XProperty)4 Ejb3Column (org.hibernate.cfg.Ejb3Column)4 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)4 Join (org.hibernate.mapping.Join)4 OneToMany (org.hibernate.mapping.OneToMany)4 Iterator (java.util.Iterator)3 Column (org.hibernate.mapping.Column)3