Search in sources :

Example 21 with JoinColumn

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

the class Ejb3XmlElementCollectionTest method testMultipleAssociationOverridesJoinColumns.

@Test
public void testMultipleAssociationOverridesJoinColumns() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm25.xml");
    assertAnnotationPresent(ElementCollection.class);
    assertAnnotationNotPresent(AssociationOverride.class);
    assertAnnotationPresent(AssociationOverrides.class);
    AssociationOverrides overridesAnno = reader.getAnnotation(AssociationOverrides.class);
    AssociationOverride[] overrides = overridesAnno.value();
    assertEquals(2, overrides.length);
    // First, an association using join table
    assertEquals("association1", overrides[0].name());
    assertEquals(0, overrides[0].joinColumns().length);
    JoinTable joinTableAnno = overrides[0].joinTable();
    assertEquals("catalog1", 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]);
    // Second, an association using join columns
    assertEquals("association2", overrides[1].name());
    // JoinColumns
    joinColumns = overrides[1].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("col8", joinColumns[1].name());
    assertEquals("col9", joinColumns[1].referencedColumnName());
    assertEquals("table4", 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 : AssociationOverrides(jakarta.persistence.AssociationOverrides) AssociationOverride(jakarta.persistence.AssociationOverride) MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn) UniqueConstraint(jakarta.persistence.UniqueConstraint) JoinTable(jakarta.persistence.JoinTable) Test(org.junit.Test)

Example 22 with JoinColumn

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

the class Ejb3XmlElementCollectionTest method testCollectionTableAllChildren.

@Test
public void testCollectionTableAllChildren() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm27.xml");
    assertAnnotationPresent(ElementCollection.class);
    assertAnnotationPresent(CollectionTable.class);
    CollectionTable tableAnno = reader.getAnnotation(CollectionTable.class);
    assertEquals("table1", tableAnno.name());
    assertEquals("catalog1", tableAnno.catalog());
    assertEquals("schema1", tableAnno.schema());
    // JoinColumns
    JoinColumn[] joinColumns = tableAnno.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());
    // UniqueConstraints
    UniqueConstraint[] uniqueConstraints = tableAnno.uniqueConstraints();
    assertEquals(2, uniqueConstraints.length);
    assertEquals("", uniqueConstraints[0].name());
    assertEquals(1, uniqueConstraints[0].columnNames().length);
    assertEquals("col3", uniqueConstraints[0].columnNames()[0]);
    assertEquals("uq1", uniqueConstraints[1].name());
    assertEquals(2, uniqueConstraints[1].columnNames().length);
    assertEquals("col4", uniqueConstraints[1].columnNames()[0]);
    assertEquals("col5", uniqueConstraints[1].columnNames()[1]);
}
Also used : MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn) CollectionTable(jakarta.persistence.CollectionTable) UniqueConstraint(jakarta.persistence.UniqueConstraint) Test(org.junit.Test)

Example 23 with JoinColumn

use of jakarta.persistence.JoinColumn 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 24 with JoinColumn

use of jakarta.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(jakarta.persistence.JoinColumn) UniqueConstraint(jakarta.persistence.UniqueConstraint) JoinTable(jakarta.persistence.JoinTable) Test(org.junit.Test)

Example 25 with JoinColumn

use of jakarta.persistence.JoinColumn 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)

Aggregations

JoinColumn (jakarta.persistence.JoinColumn)28 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)16 PrimaryKeyJoinColumn (jakarta.persistence.PrimaryKeyJoinColumn)16 JoinColumns (jakarta.persistence.JoinColumns)12 Test (org.junit.Test)12 JoinTable (jakarta.persistence.JoinTable)10 UniqueConstraint (jakarta.persistence.UniqueConstraint)9 MapKeyJoinColumns (jakarta.persistence.MapKeyJoinColumns)8 PrimaryKeyJoinColumns (jakarta.persistence.PrimaryKeyJoinColumns)7 OrderColumn (jakarta.persistence.OrderColumn)6 Column (jakarta.persistence.Column)5 DiscriminatorColumn (jakarta.persistence.DiscriminatorColumn)5 MapKeyColumn (jakarta.persistence.MapKeyColumn)5 AnnotationException (org.hibernate.AnnotationException)5 AnnotatedDiscriminatorColumn.buildDiscriminatorColumn (org.hibernate.cfg.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn)5 AnnotatedJoinColumn.buildJoinTableJoinColumns (org.hibernate.cfg.AnnotatedJoinColumn.buildJoinTableJoinColumns)5 OnDelete (org.hibernate.annotations.OnDelete)4 CollectionTable (jakarta.persistence.CollectionTable)3 Cascade (org.hibernate.annotations.Cascade)3 Columns (org.hibernate.annotations.Columns)3