Search in sources :

Example 6 with ManyToOne

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());
}
Also used : OneToOne(jakarta.persistence.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) ManyToOne(jakarta.persistence.ManyToOne)

Example 7 with ManyToOne

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));
    }
}
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 8 with ManyToOne

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());
}
Also used : ManyToOne(jakarta.persistence.ManyToOne) Test(org.junit.Test)

Example 9 with ManyToOne

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]);
}
Also used : ManyToOne(jakarta.persistence.ManyToOne) Test(org.junit.Test)

Example 10 with ManyToOne

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]);
}
Also used : ManyToOne(jakarta.persistence.ManyToOne) Test(org.junit.Test)

Aggregations

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