Search in sources :

Example 16 with Join

use of org.hibernate.mapping.Join in project hibernate-orm by hibernate.

the class AbstractJPAIndexTest method testSecondaryTableIndex.

@Test
public void testSecondaryTableIndex() {
    PersistentClass entity = metadata().getEntityBinding(Car.class.getName());
    Join join = (Join) entity.getJoinIterator().next();
    Iterator<Index> itr = join.getTable().getIndexIterator();
    assertTrue(itr.hasNext());
    Index index = itr.next();
    assertFalse(itr.hasNext());
    assertTrue("index name is not generated", StringHelper.isNotEmpty(index.getName()));
    assertEquals(2, index.getColumnSpan());
    Iterator<Column> columnIterator = index.getColumnIterator();
    Column column = columnIterator.next();
    assertEquals("dealer_name", column.getName());
    column = columnIterator.next();
    assertEquals("rate", column.getName());
    assertSame(join.getTable(), index.getTable());
}
Also used : Column(org.hibernate.mapping.Column) Join(org.hibernate.mapping.Join) Index(org.hibernate.mapping.Index) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 17 with Join

use of org.hibernate.mapping.Join in project hibernate-orm by hibernate.

the class AuditMetadataGenerator method addJoins.

@SuppressWarnings({ "unchecked" })
private void addJoins(PersistentClass pc, CompositeMapperBuilder currentMapper, ClassAuditingData auditingData, String entityName, EntityXmlMappingData xmlMappingData, boolean firstPass) {
    final Iterator<Join> joins = pc.getJoinIterator();
    while (joins.hasNext()) {
        final Join join = joins.next();
        final Element joinElement = entitiesJoins.get(entityName).get(join);
        if (joinElement != null) {
            addProperties(joinElement, join.getPropertyIterator(), currentMapper, auditingData, entityName, xmlMappingData, firstPass);
        }
    }
}
Also used : Element(org.dom4j.Element) Join(org.hibernate.mapping.Join)

Example 18 with Join

use of org.hibernate.mapping.Join in project hibernate-orm by hibernate.

the class OneToOneTest method testJoinColumnConfiguredInXml.

@Test
@TestForIssue(jiraKey = "HHH-4606")
public void testJoinColumnConfiguredInXml() {
    PersistentClass pc = metadata().getEntityBinding(Son.class.getName());
    Iterator iter = pc.getJoinIterator();
    Table table = ((Join) iter.next()).getTable();
    Iterator columnIter = table.getColumnIterator();
    boolean fooFound = false;
    boolean barFound = false;
    while (columnIter.hasNext()) {
        Column column = (Column) columnIter.next();
        if (column.getName().equals("foo")) {
            fooFound = true;
        }
        if (column.getName().equals("bar")) {
            barFound = true;
        }
    }
    assertTrue("The mapping defines join columns which could not be found in the metadata.", fooFound && barFound);
}
Also used : Table(org.hibernate.mapping.Table) Column(org.hibernate.mapping.Column) Iterator(java.util.Iterator) Join(org.hibernate.mapping.Join) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 19 with Join

use of org.hibernate.mapping.Join in project hibernate-orm by hibernate.

the class EntityBinder method processComplementaryTableDefinitions.

public void processComplementaryTableDefinitions(org.hibernate.annotations.Table table) {
    //comment and index are processed here
    if (table == null)
        return;
    String appliedTable = table.appliesTo();
    Iterator tables = persistentClass.getTableClosureIterator();
    Table hibTable = null;
    while (tables.hasNext()) {
        Table pcTable = (Table) tables.next();
        if (pcTable.getQuotedName().equals(appliedTable)) {
            //we are in the correct table to find columns
            hibTable = pcTable;
            break;
        }
        hibTable = null;
    }
    if (hibTable == null) {
        //maybe a join/secondary table
        for (Join join : secondaryTables.values()) {
            if (join.getTable().getQuotedName().equals(appliedTable)) {
                hibTable = join.getTable();
                break;
            }
        }
    }
    if (hibTable == null) {
        throw new AnnotationException("@org.hibernate.annotations.Table references an unknown table: " + appliedTable);
    }
    if (!BinderHelper.isEmptyAnnotationValue(table.comment()))
        hibTable.setComment(table.comment());
    TableBinder.addIndexes(hibTable, table.indexes(), context);
}
Also used : JoinTable(javax.persistence.JoinTable) SecondaryTable(javax.persistence.SecondaryTable) Table(org.hibernate.mapping.Table) Iterator(java.util.Iterator) Join(org.hibernate.mapping.Join) AnnotationException(org.hibernate.AnnotationException)

Example 20 with Join

use of org.hibernate.mapping.Join in project hibernate-orm by hibernate.

the class OneToOneSecondPass method buildJoinFromMappedBySide.

/**
	 * Builds the <code>Join</code> instance for the mapped by side of a <i>OneToOne</i> association using 
	 * a join tables.
	 * <p>
	 * Note:<br/>
	 * <ul>
	 * <li>From the mappedBy side we should not create the PK nor the FK, this is handled from the other side.</li>
	 * <li>This method is a dirty dupe of EntityBinder.bindSecondaryTable</i>.
	 * </p>
	 */
private Join buildJoinFromMappedBySide(PersistentClass persistentClass, Property otherSideProperty, Join originalJoin) {
    Join join = new Join();
    join.setPersistentClass(persistentClass);
    //no check constraints available on joins
    join.setTable(originalJoin.getTable());
    join.setInverse(true);
    SimpleValue key = new DependantValue(buildingContext.getMetadataCollector(), join.getTable(), persistentClass.getIdentifier());
    //TODO support @ForeignKey
    join.setKey(key);
    join.setSequentialSelect(false);
    //TODO support for inverse and optional
    //perhaps not quite per-spec, but a Good Thing anyway
    join.setOptional(true);
    key.setCascadeDeleteEnabled(false);
    Iterator mappedByColumns = otherSideProperty.getValue().getColumnIterator();
    while (mappedByColumns.hasNext()) {
        Column column = (Column) mappedByColumns.next();
        Column copy = new Column();
        copy.setLength(column.getLength());
        copy.setScale(column.getScale());
        copy.setValue(key);
        copy.setName(column.getQuotedName());
        copy.setNullable(column.isNullable());
        copy.setPrecision(column.getPrecision());
        copy.setUnique(column.isUnique());
        copy.setSqlType(column.getSqlType());
        copy.setCheckConstraint(column.getCheckConstraint());
        copy.setComment(column.getComment());
        copy.setDefaultValue(column.getDefaultValue());
        key.addColumn(copy);
    }
    persistentClass.addJoin(join);
    return join;
}
Also used : DependantValue(org.hibernate.mapping.DependantValue) Column(org.hibernate.mapping.Column) Iterator(java.util.Iterator) Join(org.hibernate.mapping.Join) SimpleValue(org.hibernate.mapping.SimpleValue)

Aggregations

Join (org.hibernate.mapping.Join)22 Iterator (java.util.Iterator)8 PersistentClass (org.hibernate.mapping.PersistentClass)7 Property (org.hibernate.mapping.Property)7 Column (org.hibernate.mapping.Column)6 AnnotationException (org.hibernate.AnnotationException)5 AssertionFailure (org.hibernate.AssertionFailure)5 Component (org.hibernate.mapping.Component)5 Table (org.hibernate.mapping.Table)5 HashMap (java.util.HashMap)4 ManyToOne (org.hibernate.mapping.ManyToOne)4 SimpleValue (org.hibernate.mapping.SimpleValue)4 JoinTable (javax.persistence.JoinTable)3 XProperty (org.hibernate.annotations.common.reflection.XProperty)3 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)3 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)3 Test (org.junit.Test)3 ElementCollection (javax.persistence.ElementCollection)2 JoinColumn (javax.persistence.JoinColumn)2 MapKeyColumn (javax.persistence.MapKeyColumn)2