Search in sources :

Example 31 with JoinColumn

use of javax.persistence.JoinColumn in project hibernate-orm by hibernate.

the class Ejb3XmlManyToOneTest method testJoinTableAllChildren.

@Test
public void testJoinTableAllChildren() throws Exception {
    reader = getReader(Entity1.class, "field1", "many-to-one.orm5.xml");
    assertAnnotationPresent(ManyToOne.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationNotPresent(JoinColumns.class);
    assertAnnotationPresent(JoinTable.class);
    JoinTable joinTableAnno = reader.getAnnotation(JoinTable.class);
    assertEquals("cat1", joinTableAnno.catalog());
    assertEquals("table1", joinTableAnno.name());
    assertEquals("schema1", joinTableAnno.schema());
    // JoinColumns
    JoinColumn[] joinColumns = joinTableAnno.joinColumns();
    assertEquals(2, joinColumns.length);
    assertEquals("", joinColumns[0].name());
    assertEquals("", joinColumns[0].referencedColumnName());
    assertEquals("", joinColumns[0].table());
    assertEquals("", joinColumns[0].columnDefinition());
    assertTrue(joinColumns[0].insertable());
    assertTrue(joinColumns[0].updatable());
    assertTrue(joinColumns[0].nullable());
    assertFalse(joinColumns[0].unique());
    assertEquals("col1", joinColumns[1].name());
    assertEquals("col2", joinColumns[1].referencedColumnName());
    assertEquals("table2", joinColumns[1].table());
    assertEquals("int", joinColumns[1].columnDefinition());
    assertFalse(joinColumns[1].insertable());
    assertFalse(joinColumns[1].updatable());
    assertFalse(joinColumns[1].nullable());
    assertTrue(joinColumns[1].unique());
    // InverseJoinColumns
    JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
    assertEquals(2, inverseJoinColumns.length);
    assertEquals("", inverseJoinColumns[0].name());
    assertEquals("", inverseJoinColumns[0].referencedColumnName());
    assertEquals("", inverseJoinColumns[0].table());
    assertEquals("", inverseJoinColumns[0].columnDefinition());
    assertTrue(inverseJoinColumns[0].insertable());
    assertTrue(inverseJoinColumns[0].updatable());
    assertTrue(inverseJoinColumns[0].nullable());
    assertFalse(inverseJoinColumns[0].unique());
    assertEquals("col3", inverseJoinColumns[1].name());
    assertEquals("col4", inverseJoinColumns[1].referencedColumnName());
    assertEquals("table3", inverseJoinColumns[1].table());
    assertEquals("int", inverseJoinColumns[1].columnDefinition());
    assertFalse(inverseJoinColumns[1].insertable());
    assertFalse(inverseJoinColumns[1].updatable());
    assertFalse(inverseJoinColumns[1].nullable());
    assertTrue(inverseJoinColumns[1].unique());
    // UniqueConstraints
    UniqueConstraint[] uniqueConstraints = joinTableAnno.uniqueConstraints();
    assertEquals(2, uniqueConstraints.length);
    assertEquals("", uniqueConstraints[0].name());
    assertEquals(1, uniqueConstraints[0].columnNames().length);
    assertEquals("col5", uniqueConstraints[0].columnNames()[0]);
    assertEquals("uq1", uniqueConstraints[1].name());
    assertEquals(2, uniqueConstraints[1].columnNames().length);
    assertEquals("col6", uniqueConstraints[1].columnNames()[0]);
    assertEquals("col7", uniqueConstraints[1].columnNames()[1]);
}
Also used : JoinColumn(javax.persistence.JoinColumn) UniqueConstraint(javax.persistence.UniqueConstraint) JoinTable(javax.persistence.JoinTable) Test(org.junit.Test)

Example 32 with JoinColumn

use of javax.persistence.JoinColumn in project hibernate-orm by hibernate.

the class Ejb3XmlOneToOneTest method testSingleJoinColumn.

/**
	 * When there's a single join column, we still wrap it with a JoinColumns
	 * annotation.
	 */
@Test
public void testSingleJoinColumn() throws Exception {
    reader = getReader(Entity1.class, "field1", "one-to-one.orm4.xml");
    assertAnnotationPresent(OneToOne.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumns.class);
    assertAnnotationPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationNotPresent(JoinTable.class);
    JoinColumns joinColumnsAnno = reader.getAnnotation(JoinColumns.class);
    JoinColumn[] joinColumns = joinColumnsAnno.value();
    assertEquals(1, joinColumns.length);
    assertEquals("col1", joinColumns[0].name());
    assertEquals("col2", joinColumns[0].referencedColumnName());
    assertEquals("table1", joinColumns[0].table());
}
Also used : JoinColumn(javax.persistence.JoinColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) Test(org.junit.Test)

Example 33 with JoinColumn

use of javax.persistence.JoinColumn in project hibernate-orm by hibernate.

the class Ejb3XmlOneToOneTest method testMultipleJoinColumns.

@Test
public void testMultipleJoinColumns() throws Exception {
    reader = getReader(Entity1.class, "field1", "one-to-one.orm5.xml");
    assertAnnotationPresent(OneToOne.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumns.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinTable.class);
    JoinColumns joinColumnsAnno = reader.getAnnotation(JoinColumns.class);
    JoinColumn[] joinColumns = joinColumnsAnno.value();
    assertEquals(2, joinColumns.length);
    assertEquals("", joinColumns[0].name());
    assertEquals("", joinColumns[0].referencedColumnName());
    assertEquals("", joinColumns[0].table());
    assertEquals("", joinColumns[0].columnDefinition());
    assertTrue(joinColumns[0].insertable());
    assertTrue(joinColumns[0].updatable());
    assertTrue(joinColumns[0].nullable());
    assertFalse(joinColumns[0].unique());
    assertEquals("col1", joinColumns[1].name());
    assertEquals("col2", joinColumns[1].referencedColumnName());
    assertEquals("table1", joinColumns[1].table());
    assertEquals("int", joinColumns[1].columnDefinition());
    assertFalse(joinColumns[1].insertable());
    assertFalse(joinColumns[1].updatable());
    assertFalse(joinColumns[1].nullable());
    assertTrue(joinColumns[1].unique());
}
Also used : JoinColumn(javax.persistence.JoinColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) Test(org.junit.Test)

Example 34 with JoinColumn

use of javax.persistence.JoinColumn in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getJoinColumns.

private JoinColumn[] getJoinColumns(Element element, boolean isInverse) {
    List<Element> subelements = element != null ? element.elements(isInverse ? "inverse-join-column" : "join-column") : null;
    List<JoinColumn> joinColumns = new ArrayList<JoinColumn>();
    if (subelements != null) {
        for (Element subelement : subelements) {
            AnnotationDescriptor column = new AnnotationDescriptor(JoinColumn.class);
            copyStringAttribute(column, subelement, "name", false);
            copyStringAttribute(column, subelement, "referenced-column-name", false);
            copyBooleanAttribute(column, subelement, "unique");
            copyBooleanAttribute(column, subelement, "nullable");
            copyBooleanAttribute(column, subelement, "insertable");
            copyBooleanAttribute(column, subelement, "updatable");
            copyStringAttribute(column, subelement, "column-definition", false);
            copyStringAttribute(column, subelement, "table", false);
            joinColumns.add((JoinColumn) AnnotationFactory.create(column));
        }
    }
    return joinColumns.toArray(new JoinColumn[joinColumns.size()]);
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList)

Example 35 with JoinColumn

use of javax.persistence.JoinColumn in project hibernate-orm by hibernate.

the class AnnotationBinder method bindManyToOne.

private static void bindManyToOne(String cascadeStrategy, Ejb3JoinColumn[] columns, boolean optional, boolean ignoreNotFound, boolean cascadeOnDelete, XClass targetEntity, PropertyHolder propertyHolder, PropertyData inferredData, boolean unique, boolean isIdentifierMapper, boolean inSecondPass, PropertyBinder propertyBinder, MetadataBuildingContext context) {
    //All FK columns should be in the same table
    org.hibernate.mapping.ManyToOne value = new org.hibernate.mapping.ManyToOne(context.getMetadataCollector(), columns[0].getTable());
    // This is a @OneToOne mapped to a physical o.h.mapping.ManyToOne
    if (unique) {
        value.markAsLogicalOneToOne();
    }
    value.setReferencedEntityName(ToOneBinder.getReferenceEntityName(inferredData, targetEntity, context));
    final XProperty property = inferredData.getProperty();
    defineFetchingStrategy(value, property);
    //value.setFetchMode( fetchMode );
    value.setIgnoreNotFound(ignoreNotFound);
    value.setCascadeDeleteEnabled(cascadeOnDelete);
    //value.setLazy( fetchMode != FetchMode.JOIN );
    if (!optional) {
        for (Ejb3JoinColumn column : columns) {
            column.setNullable(false);
        }
    }
    if (property.isAnnotationPresent(MapsId.class)) {
        //read only
        for (Ejb3JoinColumn column : columns) {
            column.setInsertable(false);
            column.setUpdatable(false);
        }
    }
    final JoinColumn joinColumn = property.getAnnotation(JoinColumn.class);
    final JoinColumns joinColumns = property.getAnnotation(JoinColumns.class);
    //Make sure that JPA1 key-many-to-one columns are read only tooj
    boolean hasSpecjManyToOne = false;
    if (context.getBuildingOptions().isSpecjProprietarySyntaxEnabled()) {
        String columnName = "";
        for (XProperty prop : inferredData.getDeclaringClass().getDeclaredProperties(AccessType.FIELD.getType())) {
            if (prop.isAnnotationPresent(Id.class) && prop.isAnnotationPresent(Column.class)) {
                columnName = prop.getAnnotation(Column.class).name();
            }
            if (property.isAnnotationPresent(ManyToOne.class) && joinColumn != null && !BinderHelper.isEmptyAnnotationValue(joinColumn.name()) && joinColumn.name().equals(columnName) && !property.isAnnotationPresent(MapsId.class)) {
                hasSpecjManyToOne = true;
                for (Ejb3JoinColumn column : columns) {
                    column.setInsertable(false);
                    column.setUpdatable(false);
                }
            }
        }
    }
    value.setTypeName(inferredData.getClassOrElementName());
    final String propertyName = inferredData.getPropertyName();
    value.setTypeUsingReflection(propertyHolder.getClassName(), propertyName);
    if ((joinColumn != null && joinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) || (joinColumns != null && joinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT)) {
        // not ideal...
        value.setForeignKeyName("none");
    } else {
        final ForeignKey fk = property.getAnnotation(ForeignKey.class);
        if (fk != null && StringHelper.isNotEmpty(fk.name())) {
            value.setForeignKeyName(fk.name());
        } else {
            final javax.persistence.ForeignKey fkOverride = propertyHolder.getOverriddenForeignKey(StringHelper.qualify(propertyHolder.getPath(), propertyName));
            if (fkOverride != null && fkOverride.value() == ConstraintMode.NO_CONSTRAINT) {
                value.setForeignKeyName("none");
            } else if (fkOverride != null) {
                value.setForeignKeyName(StringHelper.nullIfEmpty(fkOverride.name()));
                value.setForeignKeyDefinition(StringHelper.nullIfEmpty(fkOverride.foreignKeyDefinition()));
            } else if (joinColumns != null) {
                value.setForeignKeyName(StringHelper.nullIfEmpty(joinColumns.foreignKey().name()));
                value.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumns.foreignKey().foreignKeyDefinition()));
            } else if (joinColumn != null) {
                value.setForeignKeyName(StringHelper.nullIfEmpty(joinColumn.foreignKey().name()));
                value.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumn.foreignKey().foreignKeyDefinition()));
            }
        }
    }
    String path = propertyHolder.getPath() + "." + propertyName;
    FkSecondPass secondPass = new ToOneFkSecondPass(value, columns, //cannot have nullable and unique on certain DBs like Derby
    !optional && unique, propertyHolder.getEntityOwnerClassName(), path, context);
    if (inSecondPass) {
        secondPass.doSecondPass(context.getMetadataCollector().getEntityBindingMap());
    } else {
        context.getMetadataCollector().addSecondPass(secondPass);
    }
    Ejb3Column.checkPropertyConsistency(columns, propertyHolder.getEntityName() + "." + propertyName);
    //PropertyBinder binder = new PropertyBinder();
    propertyBinder.setName(propertyName);
    propertyBinder.setValue(value);
    //binder.setCascade(cascadeStrategy);
    if (isIdentifierMapper) {
        propertyBinder.setInsertable(false);
        propertyBinder.setUpdatable(false);
    } else if (hasSpecjManyToOne) {
        propertyBinder.setInsertable(false);
        propertyBinder.setUpdatable(false);
    } else {
        propertyBinder.setInsertable(columns[0].isInsertable());
        propertyBinder.setUpdatable(columns[0].isUpdatable());
    }
    propertyBinder.setColumns(columns);
    propertyBinder.setAccessType(inferredData.getDefaultAccess());
    propertyBinder.setCascade(cascadeStrategy);
    propertyBinder.setProperty(property);
    propertyBinder.setXToMany(true);
    propertyBinder.makePropertyAndBind();
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) ForeignKey(org.hibernate.annotations.ForeignKey) ManyToOne(javax.persistence.ManyToOne) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) MapKeyColumn(javax.persistence.MapKeyColumn) OrderColumn(javax.persistence.OrderColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(javax.persistence.Column) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) JoinColumn(javax.persistence.JoinColumn) MapsId(javax.persistence.MapsId) NaturalId(org.hibernate.annotations.NaturalId) EmbeddedId(javax.persistence.EmbeddedId) Id(javax.persistence.Id) CollectionId(org.hibernate.annotations.CollectionId)

Aggregations

JoinColumn (javax.persistence.JoinColumn)44 JoinTable (javax.persistence.JoinTable)17 Field (java.lang.reflect.Field)15 Method (java.lang.reflect.Method)14 ManyToOne (javax.persistence.ManyToOne)14 ReflectUtil (org.eweb4j.util.ReflectUtil)14 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)13 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)13 Test (org.junit.Test)12 JoinColumns (javax.persistence.JoinColumns)11 OneToOne (javax.persistence.OneToOne)10 UniqueConstraint (javax.persistence.UniqueConstraint)9 Column (javax.persistence.Column)8 ManyToMany (javax.persistence.ManyToMany)8 OneToMany (javax.persistence.OneToMany)6 DAOException (org.eweb4j.orm.dao.DAOException)6 Id (javax.persistence.Id)5 MapKeyJoinColumns (javax.persistence.MapKeyJoinColumns)5 HashMap (java.util.HashMap)4 CollectionTable (javax.persistence.CollectionTable)4