use of jakarta.persistence.ManyToOne 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.ManyToOne 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.ManyToOne in project hibernate-orm by hibernate.
the class Ejb3XmlManyToOneTest method testAllAttributes.
@Test
public void testAllAttributes() throws Exception {
reader = getReader(Entity1.class, "field1", "many-to-one.orm6.xml");
assertAnnotationPresent(ManyToOne.class);
assertAnnotationNotPresent(JoinColumn.class);
assertAnnotationNotPresent(JoinColumns.class);
assertAnnotationNotPresent(JoinTable.class);
assertAnnotationPresent(Id.class);
assertAnnotationPresent(MapsId.class);
assertAnnotationPresent(Access.class);
ManyToOne relAnno = reader.getAnnotation(ManyToOne.class);
assertEquals(0, relAnno.cascade().length);
assertEquals(FetchType.LAZY, relAnno.fetch());
assertFalse(relAnno.optional());
assertEquals(Entity3.class, relAnno.targetEntity());
assertEquals("col1", reader.getAnnotation(MapsId.class).value());
assertEquals(AccessType.PROPERTY, reader.getAnnotation(Access.class).value());
}
use of jakarta.persistence.ManyToOne in project hibernate-orm by hibernate.
the class Ejb3XmlManyToOneTest method testCascadeSomeWithDefaultPersist.
@Test
public void testCascadeSomeWithDefaultPersist() throws Exception {
reader = getReader(Entity1.class, "field1", "many-to-one.orm8.xml");
assertAnnotationPresent(ManyToOne.class);
ManyToOne relAnno = reader.getAnnotation(ManyToOne.class);
assertEquals(4, relAnno.cascade().length);
assertEquals(CascadeType.REMOVE, relAnno.cascade()[0]);
assertEquals(CascadeType.REFRESH, relAnno.cascade()[1]);
assertEquals(CascadeType.DETACH, relAnno.cascade()[2]);
assertEquals(CascadeType.PERSIST, relAnno.cascade()[3]);
}
use of jakarta.persistence.ManyToOne in project hibernate-orm by hibernate.
the class Ejb3XmlManyToOneTest method testCascadeAllPlusMore.
/**
* Make sure that it doesn't break the handler when {@link CascadeType#ALL}
* is specified in addition to a default cascade-persist or individual
* cascade settings.
*/
@Test
public void testCascadeAllPlusMore() throws Exception {
reader = getReader(Entity1.class, "field1", "many-to-one.orm9.xml");
assertAnnotationPresent(ManyToOne.class);
ManyToOne relAnno = reader.getAnnotation(ManyToOne.class);
assertEquals(6, relAnno.cascade().length);
assertEquals(CascadeType.ALL, relAnno.cascade()[0]);
assertEquals(CascadeType.PERSIST, relAnno.cascade()[1]);
assertEquals(CascadeType.MERGE, relAnno.cascade()[2]);
assertEquals(CascadeType.REMOVE, relAnno.cascade()[3]);
assertEquals(CascadeType.REFRESH, relAnno.cascade()[4]);
assertEquals(CascadeType.DETACH, relAnno.cascade()[5]);
}
Aggregations