Search in sources :

Example 6 with OneToMany

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

the class Ejb3XmlOneToManyTest method testAllAttributes.

@Test
public void testAllAttributes() throws Exception {
    reader = getReader(Entity2.class, "field1", "one-to-many.orm23.xml");
    assertAnnotationPresent(OneToMany.class);
    assertAnnotationNotPresent(OrderBy.class);
    assertAnnotationNotPresent(OrderColumn.class);
    assertAnnotationNotPresent(MapKey.class);
    assertAnnotationNotPresent(MapKeyClass.class);
    assertAnnotationNotPresent(MapKeyTemporal.class);
    assertAnnotationNotPresent(MapKeyEnumerated.class);
    assertAnnotationNotPresent(MapKeyColumn.class);
    assertAnnotationNotPresent(MapKeyJoinColumns.class);
    assertAnnotationNotPresent(MapKeyJoinColumn.class);
    assertAnnotationNotPresent(JoinTable.class);
    assertAnnotationNotPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationPresent(Access.class);
    OneToMany relAnno = reader.getAnnotation(OneToMany.class);
    assertEquals(0, relAnno.cascade().length);
    assertEquals(FetchType.EAGER, relAnno.fetch());
    assertEquals("field2", relAnno.mappedBy());
    assertTrue(relAnno.orphanRemoval());
    assertEquals(Entity3.class, relAnno.targetEntity());
    assertEquals(AccessType.PROPERTY, reader.getAnnotation(Access.class).value());
}
Also used : OneToMany(jakarta.persistence.OneToMany) Test(org.junit.Test)

Example 7 with OneToMany

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

the class Ejb3XmlOneToManyTest method testCascadeSomeWithDefaultPersist.

@Test
public void testCascadeSomeWithDefaultPersist() throws Exception {
    reader = getReader(Entity2.class, "field1", "one-to-many.orm21.xml");
    assertAnnotationPresent(OneToMany.class);
    OneToMany relAnno = reader.getAnnotation(OneToMany.class);
    assertEquals(4, relAnno.cascade().length);
    assertEquals(CascadeType.REMOVE, relAnno.cascade()[0]);
    assertEquals(CascadeType.REFRESH, relAnno.cascade()[1]);
    assertEquals(CascadeType.DETACH, relAnno.cascade()[2]);
    assertEquals(CascadeType.PERSIST, relAnno.cascade()[3]);
}
Also used : OneToMany(jakarta.persistence.OneToMany) Test(org.junit.Test)

Example 8 with OneToMany

use of jakarta.persistence.OneToMany in project jackson-datatype-hibernate by FasterXML.

the class PersistentCollectionSerializer method usesLazyLoading.

/**
 * Method called to see whether given property indicates it uses lazy
 * resolution of reference contained.
 */
protected boolean usesLazyLoading(BeanProperty property) {
    if (property != null) {
        // As per [Issue#36]
        ElementCollection ec = property.getAnnotation(ElementCollection.class);
        if (ec != null) {
            return (ec.fetch() == FetchType.LAZY);
        }
        OneToMany ann1 = property.getAnnotation(OneToMany.class);
        if (ann1 != null) {
            return (ann1.fetch() == FetchType.LAZY);
        }
        OneToOne ann2 = property.getAnnotation(OneToOne.class);
        if (ann2 != null) {
            return (ann2.fetch() == FetchType.LAZY);
        }
        ManyToOne ann3 = property.getAnnotation(ManyToOne.class);
        if (ann3 != null) {
            return (ann3.fetch() == FetchType.LAZY);
        }
        ManyToMany ann4 = property.getAnnotation(ManyToMany.class);
        if (ann4 != null) {
            return (ann4.fetch() == FetchType.LAZY);
        }
        // As per [Issue#53]
        return !Feature.REQUIRE_EXPLICIT_LAZY_LOADING_MARKER.enabledIn(_features);
    }
    return false;
}
Also used : OneToOne(jakarta.persistence.OneToOne) ManyToMany(jakarta.persistence.ManyToMany) ElementCollection(jakarta.persistence.ElementCollection) OneToMany(jakarta.persistence.OneToMany) ManyToOne(jakarta.persistence.ManyToOne)

Example 9 with OneToMany

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

the class CollectionBinder method buildCollectionKey.

private static DependantValue buildCollectionKey(Collection collValue, AnnotatedJoinColumn[] joinColumns, boolean cascadeDeleteEnabled, boolean noConstraintByDefault, XProperty property, PropertyHolder propertyHolder, MetadataBuildingContext buildingContext) {
    // has to do that here because the referencedProperty creation happens in a FKSecondPass for Many to one yuk!
    if (joinColumns.length > 0 && StringHelper.isNotEmpty(joinColumns[0].getMappedBy())) {
        String entityName = joinColumns[0].getManyToManyOwnerSideEntityName() != null ? "inverse__" + joinColumns[0].getManyToManyOwnerSideEntityName() : joinColumns[0].getPropertyHolder().getEntityName();
        InFlightMetadataCollector metadataCollector = buildingContext.getMetadataCollector();
        String propRef = metadataCollector.getPropertyReferencedAssociation(entityName, joinColumns[0].getMappedBy());
        if (propRef != null) {
            collValue.setReferencedPropertyName(propRef);
            metadataCollector.addPropertyReference(collValue.getOwnerEntityName(), propRef);
        }
    }
    String propRef = collValue.getReferencedPropertyName();
    // binding key reference using column
    KeyValue keyVal = propRef == null ? collValue.getOwner().getIdentifier() : (KeyValue) collValue.getOwner().getReferencedProperty(propRef).getValue();
    DependantValue key = new DependantValue(buildingContext, collValue.getCollectionTable(), keyVal);
    key.setTypeName(null);
    checkPropertyConsistency(joinColumns, collValue.getOwnerEntityName());
    key.setNullable(joinColumns.length == 0 || joinColumns[0].isNullable());
    key.setUpdateable(joinColumns.length == 0 || joinColumns[0].isUpdatable());
    key.setCascadeDeleteEnabled(cascadeDeleteEnabled);
    collValue.setKey(key);
    if (property != null) {
        final ForeignKey fk = property.getAnnotation(ForeignKey.class);
        if (fk != null && !isEmptyAnnotationValue(fk.name())) {
            key.setForeignKeyName(fk.name());
        } else {
            final CollectionTable collectionTableAnn = property.getAnnotation(CollectionTable.class);
            if (collectionTableAnn != null) {
                if (collectionTableAnn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT || collectionTableAnn.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault) {
                    key.disableForeignKey();
                } else {
                    key.setForeignKeyName(StringHelper.nullIfEmpty(collectionTableAnn.foreignKey().name()));
                    key.setForeignKeyDefinition(StringHelper.nullIfEmpty(collectionTableAnn.foreignKey().foreignKeyDefinition()));
                    if (key.getForeignKeyName() == null && key.getForeignKeyDefinition() == null && collectionTableAnn.joinColumns().length == 1) {
                        JoinColumn joinColumn = collectionTableAnn.joinColumns()[0];
                        key.setForeignKeyName(StringHelper.nullIfEmpty(joinColumn.foreignKey().name()));
                        key.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumn.foreignKey().foreignKeyDefinition()));
                    }
                }
            } else {
                final JoinTable joinTableAnn = property.getAnnotation(JoinTable.class);
                if (joinTableAnn != null) {
                    String foreignKeyName = joinTableAnn.foreignKey().name();
                    String foreignKeyDefinition = joinTableAnn.foreignKey().foreignKeyDefinition();
                    ConstraintMode foreignKeyValue = joinTableAnn.foreignKey().value();
                    if (joinTableAnn.joinColumns().length != 0) {
                        final JoinColumn joinColumnAnn = joinTableAnn.joinColumns()[0];
                        if (foreignKeyName != null && foreignKeyName.isEmpty()) {
                            foreignKeyName = joinColumnAnn.foreignKey().name();
                            foreignKeyDefinition = joinColumnAnn.foreignKey().foreignKeyDefinition();
                        }
                        if (foreignKeyValue != ConstraintMode.NO_CONSTRAINT) {
                            foreignKeyValue = joinColumnAnn.foreignKey().value();
                        }
                    }
                    if (foreignKeyValue == ConstraintMode.NO_CONSTRAINT || foreignKeyValue == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault) {
                        key.disableForeignKey();
                    } else {
                        key.setForeignKeyName(StringHelper.nullIfEmpty(foreignKeyName));
                        key.setForeignKeyDefinition(StringHelper.nullIfEmpty(foreignKeyDefinition));
                    }
                } else {
                    final jakarta.persistence.ForeignKey fkOverride = propertyHolder.getOverriddenForeignKey(StringHelper.qualify(propertyHolder.getPath(), property.getName()));
                    if (fkOverride != null && (fkOverride.value() == ConstraintMode.NO_CONSTRAINT || fkOverride.value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault)) {
                        key.disableForeignKey();
                    } else if (fkOverride != null) {
                        key.setForeignKeyName(StringHelper.nullIfEmpty(fkOverride.name()));
                        key.setForeignKeyDefinition(StringHelper.nullIfEmpty(fkOverride.foreignKeyDefinition()));
                    } else {
                        final OneToMany oneToManyAnn = property.getAnnotation(OneToMany.class);
                        final OnDelete onDeleteAnn = property.getAnnotation(OnDelete.class);
                        if (oneToManyAnn != null && !oneToManyAnn.mappedBy().isEmpty() && (onDeleteAnn == null || onDeleteAnn.action() != OnDeleteAction.CASCADE)) {
                            // foreign key should be up to @ManyToOne side
                            // @OnDelete generate "on delete cascade" foreign key
                            key.disableForeignKey();
                        } else {
                            final JoinColumn joinColumnAnn = property.getAnnotation(JoinColumn.class);
                            if (joinColumnAnn != null) {
                                if (joinColumnAnn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT || joinColumnAnn.foreignKey().value() == ConstraintMode.PROVIDER_DEFAULT && noConstraintByDefault) {
                                    key.disableForeignKey();
                                } else {
                                    key.setForeignKeyName(StringHelper.nullIfEmpty(joinColumnAnn.foreignKey().name()));
                                    key.setForeignKeyDefinition(StringHelper.nullIfEmpty(joinColumnAnn.foreignKey().foreignKeyDefinition()));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return key;
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) DependantValue(org.hibernate.mapping.DependantValue) ConstraintMode(jakarta.persistence.ConstraintMode) ForeignKey(org.hibernate.annotations.ForeignKey) OneToMany(jakarta.persistence.OneToMany) InFlightMetadataCollector(org.hibernate.boot.spi.InFlightMetadataCollector) JoinColumn(jakarta.persistence.JoinColumn) AnnotatedJoinColumn(org.hibernate.cfg.AnnotatedJoinColumn) CollectionTable(jakarta.persistence.CollectionTable) JoinTable(jakarta.persistence.JoinTable) WhereJoinTable(org.hibernate.annotations.WhereJoinTable) FilterJoinTable(org.hibernate.annotations.FilterJoinTable) OnDelete(org.hibernate.annotations.OnDelete)

Example 10 with OneToMany

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

the class CollectionBinder method determineCollectionClassification.

private static CollectionClassification determineCollectionClassification(Class<?> semanticJavaType, XProperty property, MetadataBuildingContext buildingContext) {
    if (semanticJavaType.isArray()) {
        return CollectionClassification.ARRAY;
    } else if (property.isAnnotationPresent(CollectionId.class) || property.isAnnotationPresent(CollectionIdJdbcType.class) || property.isAnnotationPresent(CollectionIdJdbcTypeCode.class) || property.isAnnotationPresent(CollectionIdJavaType.class)) {
        // explicitly an ID_BAG
        return CollectionClassification.ID_BAG;
    } else if (java.util.List.class.isAssignableFrom(semanticJavaType)) {
        if (property.isAnnotationPresent(OrderColumn.class) || property.isAnnotationPresent(org.hibernate.annotations.IndexColumn.class) || property.isAnnotationPresent(ListIndexBase.class) || property.isAnnotationPresent(ListIndexJdbcType.class) || property.isAnnotationPresent(ListIndexJdbcTypeCode.class) || property.isAnnotationPresent(ListIndexJavaType.class)) {
            // it is implicitly a LIST because of presence of explicit List index config
            return CollectionClassification.LIST;
        }
        if (property.isAnnotationPresent(jakarta.persistence.OrderBy.class) || property.isAnnotationPresent(OrderBy.class)) {
            return CollectionClassification.BAG;
        }
        ManyToMany manyToMany = property.getAnnotation(ManyToMany.class);
        if (manyToMany != null && !StringHelper.isEmpty(manyToMany.mappedBy())) {
            // We don't support @OrderColumn on the non-owning side of a many-to-many association.
            return CollectionClassification.BAG;
        }
        OneToMany oneToMany = property.getAnnotation(OneToMany.class);
        if (oneToMany != null && !StringHelper.isEmpty(oneToMany.mappedBy())) {
            // Unowned to-many mappings are always considered BAG by default
            return CollectionClassification.BAG;
        }
        // otherwise, return the implicit classification for List attributes
        return buildingContext.getBuildingOptions().getMappingDefaults().getImplicitListClassification();
    } else if (java.util.SortedSet.class.isAssignableFrom(semanticJavaType)) {
        return CollectionClassification.SORTED_SET;
    } else if (java.util.Set.class.isAssignableFrom(semanticJavaType)) {
        return CollectionClassification.SET;
    } else if (java.util.SortedMap.class.isAssignableFrom(semanticJavaType)) {
        return CollectionClassification.SORTED_MAP;
    } else if (java.util.Map.class.isAssignableFrom(semanticJavaType)) {
        return CollectionClassification.MAP;
    } else if (java.util.Collection.class.isAssignableFrom(semanticJavaType)) {
        if (property.isAnnotationPresent(CollectionId.class)) {
            return CollectionClassification.ID_BAG;
        } else {
            return CollectionClassification.BAG;
        }
    } else {
        return null;
    }
}
Also used : OrderBy(org.hibernate.annotations.OrderBy) CollectionIdJdbcTypeCode(org.hibernate.annotations.CollectionIdJdbcTypeCode) ListIndexJavaType(org.hibernate.annotations.ListIndexJavaType) OrderColumn(jakarta.persistence.OrderColumn) ManyToMany(jakarta.persistence.ManyToMany) ListIndexJdbcType(org.hibernate.annotations.ListIndexJdbcType) OneToMany(jakarta.persistence.OneToMany) IndexColumn(org.hibernate.cfg.IndexColumn) CollectionId(org.hibernate.annotations.CollectionId) LazyCollection(org.hibernate.annotations.LazyCollection) ElementCollection(jakarta.persistence.ElementCollection) Collection(org.hibernate.mapping.Collection)

Aggregations

OneToMany (jakarta.persistence.OneToMany)16 ManyToMany (jakarta.persistence.ManyToMany)7 ElementCollection (jakarta.persistence.ElementCollection)5 Test (org.junit.Test)5 ManyToOne (jakarta.persistence.ManyToOne)4 OneToOne (jakarta.persistence.OneToOne)4 JoinColumn (jakarta.persistence.JoinColumn)3 JoinColumns (jakarta.persistence.JoinColumns)3 JoinTable (jakarta.persistence.JoinTable)2 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)2 MapKeyJoinColumns (jakarta.persistence.MapKeyJoinColumns)2 OrderColumn (jakarta.persistence.OrderColumn)2 PrimaryKeyJoinColumn (jakarta.persistence.PrimaryKeyJoinColumn)2 PrimaryKeyJoinColumns (jakarta.persistence.PrimaryKeyJoinColumns)2 AnnotationException (org.hibernate.AnnotationException)2 AssertionFailure (org.hibernate.AssertionFailure)2 Columns (org.hibernate.annotations.Columns)2 Comment (org.hibernate.annotations.Comment)2 Formula (org.hibernate.annotations.Formula)2 LazyCollection (org.hibernate.annotations.LazyCollection)2