Search in sources :

Example 16 with JoinTable

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

the class JPAXMLOverriddenAnnotationReader method buildJoinTable.

/*
	 * no partial overriding possible
	 */
private JoinTable buildJoinTable(JaxbJoinTable subelement, XMLContext.Default defaults) {
    final Class<JoinTable> annotationType = JoinTable.class;
    if (subelement == null) {
        return null;
    }
    // ignore java annotation, an element is defined
    AnnotationDescriptor annotation = new AnnotationDescriptor(annotationType);
    copyAttribute(annotation, "name", subelement.getName(), false);
    copyAttribute(annotation, "catalog", subelement.getCatalog(), false);
    if (StringHelper.isNotEmpty(defaults.getCatalog()) && StringHelper.isEmpty((String) annotation.valueOf("catalog"))) {
        annotation.setValue("catalog", defaults.getCatalog());
    }
    copyAttribute(annotation, "schema", subelement.getSchema(), false);
    if (StringHelper.isNotEmpty(defaults.getSchema()) && StringHelper.isEmpty((String) annotation.valueOf("schema"))) {
        annotation.setValue("schema", defaults.getSchema());
    }
    buildUniqueConstraints(annotation, subelement.getUniqueConstraint());
    buildIndex(annotation, subelement.getIndex());
    annotation.setValue("joinColumns", getJoinColumns(subelement.getJoinColumn(), false));
    annotation.setValue("inverseJoinColumns", getJoinColumns(subelement.getInverseJoinColumn(), true));
    return AnnotationFactory.create(annotation);
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbJoinTable(org.hibernate.boot.jaxb.mapping.spi.JaxbJoinTable) JoinTable(jakarta.persistence.JoinTable)

Example 17 with JoinTable

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

the class AnnotationBinder method bindOneToOne.

private static void bindOneToOne(PropertyHolder propertyHolder, PropertyData inferredData, boolean isIdentifierMapper, boolean inSecondPass, MetadataBuildingContext context, XProperty property, AnnotatedJoinColumn[] joinColumns, PropertyBinder propertyBinder, boolean forcePersist) {
    OneToOne ann = property.getAnnotation(OneToOne.class);
    // check validity
    if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Columns.class)) {
        throw new AnnotationException("@Column(s) not allowed on a @OneToOne property: " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    // FIXME support a proper PKJCs
    boolean trueOneToOne = property.isAnnotationPresent(PrimaryKeyJoinColumn.class) || property.isAnnotationPresent(PrimaryKeyJoinColumns.class);
    Cascade hibernateCascade = property.getAnnotation(Cascade.class);
    NotFound notFound = property.getAnnotation(NotFound.class);
    boolean ignoreNotFound = notFound != null && notFound.action() == NotFoundAction.IGNORE;
    // MapsId means the columns belong to the pk;
    // A @MapsId association (obviously) must be non-null when the entity is first persisted.
    // If a @MapsId association is not mapped with @NotFound(IGNORE), then the association
    // is mandatory (even if the association has optional=true).
    // If a @MapsId association has optional=true and is mapped with @NotFound(IGNORE) then
    // the association is optional.
    // @OneToOne(optional = true) with @PKJC makes the association optional.
    final boolean mandatory = !ann.optional() || property.isAnnotationPresent(Id.class) || property.isAnnotationPresent(MapsId.class) && !ignoreNotFound;
    matchIgnoreNotFoundWithFetchType(propertyHolder.getEntityName(), property.getName(), ignoreNotFound, ann.fetch());
    OnDelete onDeleteAnn = property.getAnnotation(OnDelete.class);
    JoinTable assocTable = propertyHolder.getJoinTable(property);
    if (assocTable != null) {
        Join join = propertyHolder.addJoin(assocTable, false);
        for (AnnotatedJoinColumn joinColumn : joinColumns) {
            joinColumn.setExplicitTableName(join.getTable().getName());
        }
    }
    bindOneToOne(getCascadeStrategy(ann.cascade(), hibernateCascade, ann.orphanRemoval(), forcePersist), joinColumns, !mandatory, getFetchMode(ann.fetch()), ignoreNotFound, onDeleteAnn != null && OnDeleteAction.CASCADE == onDeleteAnn.action(), ToOneBinder.getTargetEntity(inferredData, context), propertyHolder, inferredData, ann.mappedBy(), trueOneToOne, isIdentifierMapper, inSecondPass, propertyBinder, context);
}
Also used : AnnotatedJoinColumn.buildJoinTableJoinColumns(org.hibernate.cfg.AnnotatedJoinColumn.buildJoinTableJoinColumns) JoinColumns(jakarta.persistence.JoinColumns) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) Columns(org.hibernate.annotations.Columns) MapKeyJoinColumns(jakarta.persistence.MapKeyJoinColumns) Join(org.hibernate.mapping.Join) OneToOne(jakarta.persistence.OneToOne) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) MapKeyColumn(jakarta.persistence.MapKeyColumn) MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn) Column(jakarta.persistence.Column) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) OrderColumn(jakarta.persistence.OrderColumn) DiscriminatorColumn(jakarta.persistence.DiscriminatorColumn) AnnotatedDiscriminatorColumn.buildDiscriminatorColumn(org.hibernate.cfg.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn) AnnotationException(org.hibernate.AnnotationException) Cascade(org.hibernate.annotations.Cascade) NotFound(org.hibernate.annotations.NotFound) OnDelete(org.hibernate.annotations.OnDelete) JoinTable(jakarta.persistence.JoinTable)

Example 18 with JoinTable

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

the class ColumnsBuilder method buildDefaultJoinColumnsForXToOne.

AnnotatedJoinColumn[] buildDefaultJoinColumnsForXToOne(XProperty property, PropertyData inferredData) {
    AnnotatedJoinColumn[] joinColumns;
    JoinTable joinTableAnn = propertyHolder.getJoinTable(property);
    Comment comment = property.getAnnotation(Comment.class);
    if (joinTableAnn != null) {
        joinColumns = AnnotatedJoinColumn.buildJoinColumns(joinTableAnn.inverseJoinColumns(), comment, null, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
        if (StringHelper.isEmpty(joinTableAnn.name())) {
            throw new AnnotationException("JoinTable.name() on a @ToOne association has to be explicit: " + BinderHelper.getPath(propertyHolder, inferredData));
        }
    } else {
        OneToOne oneToOneAnn = property.getAnnotation(OneToOne.class);
        String mappedBy = oneToOneAnn != null ? oneToOneAnn.mappedBy() : null;
        joinColumns = AnnotatedJoinColumn.buildJoinColumns(null, comment, mappedBy, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
    }
    return joinColumns;
}
Also used : Comment(org.hibernate.annotations.Comment) OneToOne(jakarta.persistence.OneToOne) AnnotationException(org.hibernate.AnnotationException) JoinTable(jakarta.persistence.JoinTable)

Example 19 with JoinTable

use of jakarta.persistence.JoinTable 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 20 with JoinTable

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

Aggregations

JoinTable (jakarta.persistence.JoinTable)23 JoinColumn (jakarta.persistence.JoinColumn)11 Test (org.junit.Test)9 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)7 UniqueConstraint (jakarta.persistence.UniqueConstraint)6 PrimaryKeyJoinColumn (jakarta.persistence.PrimaryKeyJoinColumn)5 AnnotationException (org.hibernate.AnnotationException)5 OnDelete (org.hibernate.annotations.OnDelete)4 Join (org.hibernate.mapping.Join)4 AssociationOverride (jakarta.persistence.AssociationOverride)3 CollectionTable (jakarta.persistence.CollectionTable)3 JoinColumns (jakarta.persistence.JoinColumns)3 MapKeyJoinColumns (jakarta.persistence.MapKeyJoinColumns)3 PrimaryKeyJoinColumns (jakarta.persistence.PrimaryKeyJoinColumns)3 Cascade (org.hibernate.annotations.Cascade)3 FilterJoinTable (org.hibernate.annotations.FilterJoinTable)3 WhereJoinTable (org.hibernate.annotations.WhereJoinTable)3 AnnotatedJoinColumn (org.hibernate.cfg.AnnotatedJoinColumn)3 Column (jakarta.persistence.Column)2 DiscriminatorColumn (jakarta.persistence.DiscriminatorColumn)2