Search in sources :

Example 11 with OneToOne

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

the class Ejb3XmlOneToOneTest 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", "one-to-one.orm10.xml");
    assertAnnotationPresent(OneToOne.class);
    OneToOne relAnno = reader.getAnnotation(OneToOne.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 : OneToOne(javax.persistence.OneToOne) Test(org.junit.Test)

Example 12 with OneToOne

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

the class Ejb3XmlOneToOneTest method testNoChildren.

@Test
public void testNoChildren() throws Exception {
    reader = getReader(Entity1.class, "field1", "one-to-one.orm1.xml");
    assertAnnotationPresent(OneToOne.class);
    assertAnnotationNotPresent(MapsId.class);
    assertAnnotationNotPresent(Id.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumns.class);
    assertAnnotationNotPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationNotPresent(JoinTable.class);
    assertAnnotationNotPresent(Access.class);
    OneToOne relAnno = reader.getAnnotation(OneToOne.class);
    assertEquals(0, relAnno.cascade().length);
    assertEquals(FetchType.EAGER, relAnno.fetch());
    assertEquals("", relAnno.mappedBy());
    assertTrue(relAnno.optional());
    assertFalse(relAnno.orphanRemoval());
    assertEquals(void.class, relAnno.targetEntity());
}
Also used : OneToOne(javax.persistence.OneToOne) Test(org.junit.Test)

Example 13 with OneToOne

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

the class Ejb3XmlOneToOneTest method testAllAttributes.

@Test
public void testAllAttributes() throws Exception {
    reader = getReader(Entity1.class, "field1", "one-to-one.orm11.xml");
    assertAnnotationPresent(OneToOne.class);
    assertAnnotationPresent(MapsId.class);
    assertAnnotationPresent(Id.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumns.class);
    assertAnnotationNotPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationNotPresent(JoinTable.class);
    assertAnnotationPresent(Access.class);
    OneToOne relAnno = reader.getAnnotation(OneToOne.class);
    assertEquals(0, relAnno.cascade().length);
    assertEquals(FetchType.LAZY, relAnno.fetch());
    assertEquals("field2", relAnno.mappedBy());
    assertFalse(relAnno.optional());
    assertTrue(relAnno.orphanRemoval());
    assertEquals(Entity3.class, relAnno.targetEntity());
    assertEquals(AccessType.PROPERTY, reader.getAnnotation(Access.class).value());
    assertEquals("field3", reader.getAnnotation(MapsId.class).value());
}
Also used : OneToOne(javax.persistence.OneToOne) Test(org.junit.Test)

Example 14 with OneToOne

use of javax.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(javax.persistence.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) ManyToOne(javax.persistence.ManyToOne)

Example 15 with OneToOne

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

the class AnnotationBinder method defineFetchingStrategy.

protected 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(false);
    }
    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(javax.persistence.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) FetchType(javax.persistence.FetchType) AnnotationException(org.hibernate.AnnotationException) ManyToOne(javax.persistence.ManyToOne) LazyToOne(org.hibernate.annotations.LazyToOne)

Aggregations

OneToOne (javax.persistence.OneToOne)21 ManyToOne (javax.persistence.ManyToOne)13 Field (java.lang.reflect.Field)9 JoinColumn (javax.persistence.JoinColumn)8 ReflectUtil (org.eweb4j.util.ReflectUtil)8 Method (java.lang.reflect.Method)7 ManyToMany (javax.persistence.ManyToMany)6 OneToMany (javax.persistence.OneToMany)6 Test (org.junit.Test)5 AnnotationException (org.hibernate.AnnotationException)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Column (javax.persistence.Column)2 FetchType (javax.persistence.FetchType)2 Id (javax.persistence.Id)2 JoinTable (javax.persistence.JoinTable)2 AssertionFailure (org.hibernate.AssertionFailure)2 OAccess (com.orientechnologies.orient.core.annotation.OAccess)1 ODocumentInstance (com.orientechnologies.orient.core.annotation.ODocumentInstance)1 OId (com.orientechnologies.orient.core.annotation.OId)1