Search in sources :

Example 1 with ManyToOne

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

the class AnnotationBinder method bindManyToOne.

private static void bindManyToOne(PropertyHolder propertyHolder, PropertyData inferredData, boolean isIdentifierMapper, boolean inSecondPass, MetadataBuildingContext context, XProperty property, AnnotatedJoinColumn[] joinColumns, PropertyBinder propertyBinder, boolean forcePersist) {
    ManyToOne ann = property.getAnnotation(ManyToOne.class);
    // check validity
    if (property.isAnnotationPresent(Column.class) || property.isAnnotationPresent(Columns.class)) {
        throw new AnnotationException("@Column(s) not allowed on a @ManyToOne property: " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    Cascade hibernateCascade = property.getAnnotation(Cascade.class);
    NotFound notFound = property.getAnnotation(NotFound.class);
    boolean ignoreNotFound = notFound != null && notFound.action() == NotFoundAction.IGNORE;
    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());
        }
    }
    // 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.
    final boolean mandatory = !ann.optional() || property.isAnnotationPresent(Id.class) || property.isAnnotationPresent(MapsId.class) && !ignoreNotFound;
    bindManyToOne(getCascadeStrategy(ann.cascade(), hibernateCascade, false, forcePersist), joinColumns, !mandatory, ignoreNotFound, onDeleteAnn != null && OnDeleteAction.CASCADE == onDeleteAnn.action(), ToOneBinder.getTargetEntity(inferredData, context), propertyHolder, inferredData, false, isIdentifierMapper, inSecondPass, propertyBinder, context);
}
Also used : 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) AnnotatedJoinColumn.buildJoinTableJoinColumns(org.hibernate.cfg.AnnotatedJoinColumn.buildJoinTableJoinColumns) JoinColumns(jakarta.persistence.JoinColumns) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) Columns(org.hibernate.annotations.Columns) MapKeyJoinColumns(jakarta.persistence.MapKeyJoinColumns) AnnotationException(org.hibernate.AnnotationException) Join(org.hibernate.mapping.Join) ManyToOne(jakarta.persistence.ManyToOne) Cascade(org.hibernate.annotations.Cascade) NotFound(org.hibernate.annotations.NotFound) OnDelete(org.hibernate.annotations.OnDelete) JoinTable(jakarta.persistence.JoinTable)

Example 2 with ManyToOne

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

the class JPAXMLOverriddenAnnotationReader method getManyToOne.

/**
 * @see #getOneToOne(List, XMLContext.Default)
 * @see #getElementCollection(List, XMLContext.Default)
 */
private void getManyToOne(List<Annotation> annotationList, XMLContext.Default defaults) {
    Class<ManyToOne> annotationType = ManyToOne.class;
    List<JaxbManyToOne> elements = elementsForProperty.getManyToOne();
    for (JaxbManyToOne element : elements) {
        AnnotationDescriptor ad = new AnnotationDescriptor(annotationType);
        addTargetClass(element.getTargetEntity(), ad, "target-entity", defaults);
        getFetchType(ad, element.getFetch());
        getCascades(ad, element.getCascade(), defaults);
        getJoinTable(annotationList, element, defaults);
        buildJoinColumns(annotationList, element.getJoinColumn());
        copyAttribute(ad, "optional", element.isOptional(), false);
        annotationList.add(AnnotationFactory.create(ad));
        getAssociationId(annotationList, element.isId());
        getMapsId(annotationList, element.getMapsId());
        getAccessType(annotationList, element.getAccess());
    }
    afterGetAssociation(annotationType, annotationList, defaults);
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbManyToOne(org.hibernate.boot.jaxb.mapping.spi.JaxbManyToOne) JaxbManyToOne(org.hibernate.boot.jaxb.mapping.spi.JaxbManyToOne) ManyToOne(jakarta.persistence.ManyToOne)

Example 3 with ManyToOne

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

the class Ejb3XmlManyToOneTest method testCascadeAll.

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

Example 4 with ManyToOne

use of jakarta.persistence.ManyToOne 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 5 with ManyToOne

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

the class JPAXMLOverriddenAnnotationReader method overridesDefaultCascadePersist.

private Annotation overridesDefaultCascadePersist(Annotation annotation, XMLContext.Default defaults) {
    if (Boolean.TRUE.equals(defaults.getCascadePersist())) {
        final Class<? extends Annotation> annotationType = annotation.annotationType();
        if (annotationType == ManyToOne.class) {
            ManyToOne manyToOne = (ManyToOne) annotation;
            List<CascadeType> cascades = new ArrayList<>(Arrays.asList(manyToOne.cascade()));
            if (!cascades.contains(CascadeType.ALL) && !cascades.contains(CascadeType.PERSIST)) {
                cascades.add(CascadeType.PERSIST);
            } else {
                return annotation;
            }
            AnnotationDescriptor ad = new AnnotationDescriptor(annotationType);
            ad.setValue("cascade", cascades.toArray(new CascadeType[] {}));
            ad.setValue("targetEntity", manyToOne.targetEntity());
            ad.setValue("fetch", manyToOne.fetch());
            ad.setValue("optional", manyToOne.optional());
            return AnnotationFactory.create(ad);
        } else if (annotationType == OneToOne.class) {
            OneToOne oneToOne = (OneToOne) annotation;
            List<CascadeType> cascades = new ArrayList<>(Arrays.asList(oneToOne.cascade()));
            if (!cascades.contains(CascadeType.ALL) && !cascades.contains(CascadeType.PERSIST)) {
                cascades.add(CascadeType.PERSIST);
            } else {
                return annotation;
            }
            AnnotationDescriptor ad = new AnnotationDescriptor(annotationType);
            ad.setValue("cascade", cascades.toArray(new CascadeType[] {}));
            ad.setValue("targetEntity", oneToOne.targetEntity());
            ad.setValue("fetch", oneToOne.fetch());
            ad.setValue("optional", oneToOne.optional());
            ad.setValue("mappedBy", oneToOne.mappedBy());
            ad.setValue("orphanRemoval", oneToOne.orphanRemoval());
            return AnnotationFactory.create(ad);
        }
    }
    return annotation;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) OneToOne(jakarta.persistence.OneToOne) JaxbOneToOne(org.hibernate.boot.jaxb.mapping.spi.JaxbOneToOne) CascadeType(jakarta.persistence.CascadeType) JaxbCascadeType(org.hibernate.boot.jaxb.mapping.spi.JaxbCascadeType) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) JaxbManyToOne(org.hibernate.boot.jaxb.mapping.spi.JaxbManyToOne) ManyToOne(jakarta.persistence.ManyToOne)

Aggregations

ManyToOne (jakarta.persistence.ManyToOne)13 OneToOne (jakarta.persistence.OneToOne)6 Test (org.junit.Test)5 ManyToMany (jakarta.persistence.ManyToMany)3 OneToMany (jakarta.persistence.OneToMany)3 AnnotationException (org.hibernate.AnnotationException)2 AssertionFailure (org.hibernate.AssertionFailure)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 JaxbManyToOne (org.hibernate.boot.jaxb.mapping.spi.JaxbManyToOne)2 CascadeType (jakarta.persistence.CascadeType)1 Column (jakarta.persistence.Column)1 DiscriminatorColumn (jakarta.persistence.DiscriminatorColumn)1 ElementCollection (jakarta.persistence.ElementCollection)1 FetchType (jakarta.persistence.FetchType)1 JoinColumn (jakarta.persistence.JoinColumn)1 JoinColumns (jakarta.persistence.JoinColumns)1 JoinTable (jakarta.persistence.JoinTable)1 MapKeyColumn (jakarta.persistence.MapKeyColumn)1 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)1 MapKeyJoinColumns (jakarta.persistence.MapKeyJoinColumns)1