Search in sources :

Example 11 with OneToMany

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

the class CollectionBinder method bindOneToManySecondPass.

protected void bindOneToManySecondPass(Collection collection, Map<String, PersistentClass> persistentClasses, AnnotatedJoinColumn[] fkJoinColumns, XClass collectionType, boolean cascadeDeleteEnabled, boolean ignoreNotFound, MetadataBuildingContext buildingContext, Map<XClass, InheritanceState> inheritanceStatePerClass) {
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding a OneToMany: %s.%s through a foreign key", propertyHolder.getEntityName(), propertyName);
    }
    if (buildingContext == null) {
        throw new AssertionFailure("CollectionSecondPass for oneToMany should not be called with null mappings");
    }
    org.hibernate.mapping.OneToMany oneToMany = new org.hibernate.mapping.OneToMany(buildingContext, collection.getOwner());
    collection.setElement(oneToMany);
    oneToMany.setReferencedEntityName(collectionType.getName());
    oneToMany.setIgnoreNotFound(ignoreNotFound);
    String assocClass = oneToMany.getReferencedEntityName();
    PersistentClass associatedClass = persistentClasses.get(assocClass);
    handleJpaOrderBy(collection, associatedClass);
    Map<String, Join> joins = buildingContext.getMetadataCollector().getJoins(assocClass);
    if (associatedClass == null) {
        throw new MappingException(String.format("Association [%s] for entity [%s] references unmapped class [%s]", propertyName, propertyHolder.getClassName(), assocClass));
    }
    oneToMany.setAssociatedClass(associatedClass);
    for (AnnotatedJoinColumn column : fkJoinColumns) {
        column.setPersistentClass(associatedClass, joins, inheritanceStatePerClass);
        column.setJoins(joins);
        collection.setCollectionTable(column.getTable());
    }
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Mapping collection: %s -> %s", collection.getRole(), collection.getCollectionTable().getName());
    }
    bindFilters(false);
    handleWhere(false);
    bindCollectionSecondPass(collection, null, fkJoinColumns, cascadeDeleteEnabled, property, propertyHolder, buildingContext);
    if (!collection.isInverse() && !collection.getKey().isNullable()) {
        // for non-inverse one-to-many, with a not-null fk, add a backref!
        String entityName = oneToMany.getReferencedEntityName();
        PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding(entityName);
        Backref prop = new Backref();
        prop.setName('_' + fkJoinColumns[0].getPropertyName() + '_' + fkJoinColumns[0].getLogicalColumnName() + "Backref");
        prop.setUpdateable(false);
        prop.setSelectable(false);
        prop.setCollectionRole(collection.getRole());
        prop.setEntityName(collection.getOwner().getEntityName());
        prop.setValue(collection.getKey());
        referenced.addProperty(prop);
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) Join(org.hibernate.mapping.Join) Backref(org.hibernate.mapping.Backref) OneToMany(jakarta.persistence.OneToMany) MappingException(org.hibernate.MappingException) AnnotatedJoinColumn(org.hibernate.cfg.AnnotatedJoinColumn) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 12 with OneToMany

use of jakarta.persistence.OneToMany 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 13 with OneToMany

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

the class Ejb3XmlOneToManyTest method testNoChildren.

@Test
public void testNoChildren() throws Exception {
    reader = getReader(Entity2.class, "field1", "one-to-many.orm1.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);
    assertAnnotationNotPresent(Access.class);
    OneToMany relAnno = reader.getAnnotation(OneToMany.class);
    assertEquals(0, relAnno.cascade().length);
    assertEquals(FetchType.LAZY, relAnno.fetch());
    assertEquals("", relAnno.mappedBy());
    assertFalse(relAnno.orphanRemoval());
    assertEquals(void.class, relAnno.targetEntity());
}
Also used : OneToMany(jakarta.persistence.OneToMany) Test(org.junit.Test)

Example 14 with OneToMany

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

the class Ejb3XmlOneToManyTest method testCascadeAll.

@Test
public void testCascadeAll() throws Exception {
    reader = getReader(Entity2.class, "field1", "one-to-many.orm20.xml");
    assertAnnotationPresent(OneToMany.class);
    OneToMany relAnno = reader.getAnnotation(OneToMany.class);
    assertEquals(1, relAnno.cascade().length);
    assertEquals(CascadeType.ALL, relAnno.cascade()[0]);
}
Also used : OneToMany(jakarta.persistence.OneToMany) Test(org.junit.Test)

Example 15 with OneToMany

use of jakarta.persistence.OneToMany in project eclipselink by eclipse-ee4j.

the class AbstractFieldMapping method getMemberType.

@Override
protected Class<?> getMemberType() {
    Field field = getMember();
    // One to One
    OneToOne oneToOne = field.getAnnotation(OneToOne.class);
    if (oneToOne != null) {
        Class<?> targetEntity = oneToOne.targetEntity();
        if (targetEntity != void.class) {
            return targetEntity;
        }
    }
    // Many to One
    ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);
    if (manyToOne != null) {
        Class<?> targetEntity = manyToOne.targetEntity();
        if (targetEntity != void.class) {
            return targetEntity;
        }
    }
    // Many to Many
    ManyToMany manyToMany = field.getAnnotation(ManyToMany.class);
    if (manyToMany != null) {
        Class<?> targetEntity = manyToMany.targetEntity();
        if (targetEntity != void.class) {
            return targetEntity;
        }
    }
    // One to Many
    OneToMany oneToMany = field.getAnnotation(OneToMany.class);
    if (oneToMany != null) {
        Class<?> targetEntity = oneToMany.targetEntity();
        if (targetEntity != void.class) {
            return targetEntity;
        }
    }
    return field.getType();
}
Also used : Field(java.lang.reflect.Field) OneToOne(jakarta.persistence.OneToOne) ManyToMany(jakarta.persistence.ManyToMany) OneToMany(jakarta.persistence.OneToMany) ManyToOne(jakarta.persistence.ManyToOne)

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