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