Search in sources :

Example 1 with CascadeType

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

CascadeType (jakarta.persistence.CascadeType)1 ManyToOne (jakarta.persistence.ManyToOne)1 OneToOne (jakarta.persistence.OneToOne)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 JaxbCascadeType (org.hibernate.boot.jaxb.mapping.spi.JaxbCascadeType)1 JaxbManyToOne (org.hibernate.boot.jaxb.mapping.spi.JaxbManyToOne)1 JaxbOneToOne (org.hibernate.boot.jaxb.mapping.spi.JaxbOneToOne)1