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;
}
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());
}
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);
}
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));
}
}
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;
}
Aggregations