Search in sources :

Example 11 with JoinColumns

use of jakarta.persistence.JoinColumns in project hibernate-orm by hibernate.

the class ColumnsBuilder method extractMetadata.

public ColumnsBuilder extractMetadata() {
    columns = null;
    joinColumns = buildExplicitJoinColumns(property, inferredData);
    Comment comment = property.getAnnotation(Comment.class);
    if (property.isAnnotationPresent(Column.class)) {
        columns = buildColumnFromAnnotation(property.getAnnotation(Column.class), comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    } else if (property.isAnnotationPresent(Formula.class)) {
        columns = buildFormulaFromAnnotation(getOverridableAnnotation(property, Formula.class, buildingContext), comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    } else if (property.isAnnotationPresent(Columns.class)) {
        columns = buildColumnsFromAnnotations(property.getAnnotation(Columns.class).columns(), comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    }
    // set default values if needed
    if (joinColumns == null && (property.isAnnotationPresent(ManyToOne.class) || property.isAnnotationPresent(OneToOne.class))) {
        joinColumns = buildDefaultJoinColumnsForXToOne(property, inferredData);
    } else if (joinColumns == null && (property.isAnnotationPresent(OneToMany.class) || property.isAnnotationPresent(ElementCollection.class))) {
        OneToMany oneToMany = property.getAnnotation(OneToMany.class);
        joinColumns = AnnotatedJoinColumn.buildJoinColumns(null, comment, oneToMany != null ? oneToMany.mappedBy() : "", entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
    } else if (joinColumns == null && property.isAnnotationPresent(org.hibernate.annotations.Any.class)) {
        throw new AnnotationException("@Any requires an explicit @JoinColumn(s): " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    if (columns == null && !property.isAnnotationPresent(ManyToMany.class)) {
        // useful for collection of embedded elements
        columns = buildColumnFromNoAnnotation(comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    }
    if (nullability == Nullability.FORCED_NOT_NULL) {
        // force columns to not null
        for (AnnotatedColumn col : columns) {
            col.forceNotNull();
        }
    }
    return this;
}
Also used : Comment(org.hibernate.annotations.Comment) Formula(org.hibernate.annotations.Formula) JoinColumnOrFormula(org.hibernate.annotations.JoinColumnOrFormula) JoinFormula(org.hibernate.annotations.JoinFormula) OneToOne(jakarta.persistence.OneToOne) Columns(org.hibernate.annotations.Columns) JoinColumns(jakarta.persistence.JoinColumns) AnnotationException(org.hibernate.AnnotationException) OneToMany(jakarta.persistence.OneToMany) ManyToOne(jakarta.persistence.ManyToOne)

Example 12 with JoinColumns

use of jakarta.persistence.JoinColumns in project hibernate-orm by hibernate.

the class Ejb3XmlManyToOneTest method testMultipleJoinColumns.

@Test
public void testMultipleJoinColumns() throws Exception {
    reader = getReader(Entity1.class, "field1", "many-to-one.orm3.xml");
    assertAnnotationPresent(ManyToOne.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(jakarta.persistence.JoinColumn) JoinColumns(jakarta.persistence.JoinColumns) Test(org.junit.Test)

Example 13 with JoinColumns

use of jakarta.persistence.JoinColumns in project hibernate-orm by hibernate.

the class Ejb3XmlManyToOneTest 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", "many-to-one.orm2.xml");
    assertAnnotationPresent(ManyToOne.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationPresent(JoinColumns.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(jakarta.persistence.JoinColumn) JoinColumns(jakarta.persistence.JoinColumns) Test(org.junit.Test)

Example 14 with JoinColumns

use of jakarta.persistence.JoinColumns 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(jakarta.persistence.JoinColumn) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) JoinColumns(jakarta.persistence.JoinColumns) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) Test(org.junit.Test)

Example 15 with JoinColumns

use of jakarta.persistence.JoinColumns in project hibernate-orm by hibernate.

the class Ejb3XmlOneToManyTest 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(Entity2.class, "field1", "one-to-many.orm18.xml");
    assertAnnotationPresent(OneToMany.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationPresent(JoinColumns.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 : MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn) JoinColumns(jakarta.persistence.JoinColumns) MapKeyJoinColumns(jakarta.persistence.MapKeyJoinColumns) Test(org.junit.Test)

Aggregations

JoinColumns (jakarta.persistence.JoinColumns)15 JoinColumn (jakarta.persistence.JoinColumn)14 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)8 MapKeyJoinColumns (jakarta.persistence.MapKeyJoinColumns)8 PrimaryKeyJoinColumn (jakarta.persistence.PrimaryKeyJoinColumn)8 PrimaryKeyJoinColumns (jakarta.persistence.PrimaryKeyJoinColumns)8 AnnotationException (org.hibernate.AnnotationException)6 Test (org.junit.Test)6 OrderColumn (jakarta.persistence.OrderColumn)5 AnnotatedJoinColumn.buildJoinTableJoinColumns (org.hibernate.cfg.AnnotatedJoinColumn.buildJoinTableJoinColumns)5 Column (jakarta.persistence.Column)4 DiscriminatorColumn (jakarta.persistence.DiscriminatorColumn)4 MapKeyColumn (jakarta.persistence.MapKeyColumn)4 Columns (org.hibernate.annotations.Columns)4 JoinTable (jakarta.persistence.JoinTable)3 ManyToOne (jakarta.persistence.ManyToOne)3 OneToMany (jakarta.persistence.OneToMany)3 Cascade (org.hibernate.annotations.Cascade)3 Comment (org.hibernate.annotations.Comment)3 NotFound (org.hibernate.annotations.NotFound)3