Search in sources :

Example 1 with OneToOne

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

the class JPAXMLOverriddenAnnotationReader method getOneToOne.

/**
 * As per section 12.2 of the JPA 2.0 specification, the association
 * subelements (many-to-one, one-to-many, one-to-one, many-to-many,
 * element-collection) completely override the mapping for the specified
 * field or property.  Thus, any methods which might in some contexts merge
 * with annotations must not do so in this context.
 *
 * @see #getElementCollection(List, XMLContext.Default)
 */
private void getOneToOne(List<Annotation> annotationList, XMLContext.Default defaults) {
    Class<OneToOne> annotationType = OneToOne.class;
    List<JaxbOneToOne> elements = elementsForProperty.getOneToOne();
    for (JaxbOneToOne 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());
        Annotation annotation = getPrimaryKeyJoinColumns(element.getPrimaryKeyJoinColumn(), defaults, false);
        addIfNotNull(annotationList, annotation);
        copyAttribute(ad, "optional", element.isOptional(), false);
        copyAttribute(ad, "orphan-removal", element.isOrphanRemoval(), false);
        copyAttribute(ad, "mapped-by", element.getMappedBy(), false);
        annotationList.add(AnnotationFactory.create(ad));
        getAssociationId(annotationList, element.isId());
        getMapsId(annotationList, element.getMapsId());
        getAccessType(annotationList, element.getAccess());
    }
    afterGetAssociation(annotationType, annotationList, defaults);
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) OneToOne(jakarta.persistence.OneToOne) JaxbOneToOne(org.hibernate.boot.jaxb.mapping.spi.JaxbOneToOne) JaxbOneToOne(org.hibernate.boot.jaxb.mapping.spi.JaxbOneToOne) Annotation(java.lang.annotation.Annotation)

Example 2 with OneToOne

use of jakarta.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(jakarta.persistence.OneToOne) Test(org.junit.Test)

Example 3 with OneToOne

use of jakarta.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(jakarta.persistence.OneToOne) Test(org.junit.Test)

Example 4 with OneToOne

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

the class Ejb3XmlOneToOneTest method testCascadeSomeWithDefaultPersist.

@Test
public void testCascadeSomeWithDefaultPersist() throws Exception {
    reader = getReader(Entity1.class, "field1", "one-to-one.orm9.xml");
    assertAnnotationPresent(OneToOne.class);
    OneToOne relAnno = reader.getAnnotation(OneToOne.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 : OneToOne(jakarta.persistence.OneToOne) Test(org.junit.Test)

Example 5 with OneToOne

use of jakarta.persistence.OneToOne 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;
}
Also used : OneToOne(jakarta.persistence.OneToOne) ManyToMany(jakarta.persistence.ManyToMany) ElementCollection(jakarta.persistence.ElementCollection) OneToMany(jakarta.persistence.OneToMany) ManyToOne(jakarta.persistence.ManyToOne)

Aggregations

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