Search in sources :

Example 6 with OneToOne

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

Example 7 with OneToOne

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

the class ToOneBinder method getTargetEntityClass.

private static Class<?> getTargetEntityClass(XProperty property) {
    final ManyToOne mTo = property.getAnnotation(ManyToOne.class);
    if (mTo != null) {
        return mTo.targetEntity();
    }
    final OneToOne oTo = property.getAnnotation(OneToOne.class);
    if (oTo != null) {
        return oTo.targetEntity();
    }
    throw new AssertionFailure("Unexpected discovery of a targetEntity: " + property.getName());
}
Also used : OneToOne(jakarta.persistence.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) ManyToOne(jakarta.persistence.ManyToOne)

Example 8 with OneToOne

use of jakarta.persistence.OneToOne 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 9 with OneToOne

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

the class AnnotationBinder method defineFetchingStrategy.

static void defineFetchingStrategy(ToOne toOne, XProperty property) {
    LazyToOne lazy = property.getAnnotation(LazyToOne.class);
    Fetch fetch = property.getAnnotation(Fetch.class);
    ManyToOne manyToOne = property.getAnnotation(ManyToOne.class);
    OneToOne oneToOne = property.getAnnotation(OneToOne.class);
    FetchType fetchType;
    if (manyToOne != null) {
        fetchType = manyToOne.fetch();
    } else if (oneToOne != null) {
        fetchType = oneToOne.fetch();
    } else {
        throw new AssertionFailure("Define fetch strategy on a property not annotated with @OneToMany nor @OneToOne");
    }
    if (lazy != null) {
        toOne.setLazy(!(lazy.value() == LazyToOneOption.FALSE));
        toOne.setUnwrapProxy((lazy.value() == LazyToOneOption.NO_PROXY));
    } else {
        toOne.setLazy(fetchType == FetchType.LAZY);
        toOne.setUnwrapProxy(fetchType != FetchType.LAZY);
        toOne.setUnwrapProxyImplicit(true);
    }
    if (fetch != null) {
        if (fetch.value() == org.hibernate.annotations.FetchMode.JOIN) {
            toOne.setFetchMode(FetchMode.JOIN);
            toOne.setLazy(false);
            toOne.setUnwrapProxy(false);
        } else if (fetch.value() == org.hibernate.annotations.FetchMode.SELECT) {
            toOne.setFetchMode(FetchMode.SELECT);
        } else if (fetch.value() == org.hibernate.annotations.FetchMode.SUBSELECT) {
            throw new AnnotationException("Use of FetchMode.SUBSELECT not allowed on ToOne associations");
        } else {
            throw new AssertionFailure("Unknown FetchMode: " + fetch.value());
        }
    } else {
        toOne.setFetchMode(getFetchMode(fetchType));
    }
}
Also used : Fetch(org.hibernate.annotations.Fetch) OneToOne(jakarta.persistence.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) FetchType(jakarta.persistence.FetchType) AnnotationException(org.hibernate.AnnotationException) ManyToOne(jakarta.persistence.ManyToOne) LazyToOne(org.hibernate.annotations.LazyToOne)

Example 10 with OneToOne

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

Aggregations

OneToOne (jakarta.persistence.OneToOne)14 ManyToOne (jakarta.persistence.ManyToOne)6 Test (org.junit.Test)5 ManyToMany (jakarta.persistence.ManyToMany)3 OneToMany (jakarta.persistence.OneToMany)3 AnnotationException (org.hibernate.AnnotationException)3 JoinTable (jakarta.persistence.JoinTable)2 AssertionFailure (org.hibernate.AssertionFailure)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 JaxbOneToOne (org.hibernate.boot.jaxb.mapping.spi.JaxbOneToOne)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 MapKeyColumn (jakarta.persistence.MapKeyColumn)1 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)1 MapKeyJoinColumns (jakarta.persistence.MapKeyJoinColumns)1