From f2f6a088534b39204d00e178347a1622f14171af Mon Sep 17 00:00:00 2001 From: Vlad Mihalcea Date: Thu, 2 Feb 2017 10:57:23 +0200 Subject: [PATCH] HHH-9460 - Removing non-optional bidirectional @OneToOne association with cascade --- .../BidirectionalOptionalOneToOneTest.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetoone/optional/BidirectionalOptionalOneToOneTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/optional/BidirectionalOptionalOneToOneTest.java b/hibernate-core/src/test/java/org/hibernate/test/onetoone/optional/BidirectionalOptionalOneToOneTest.java new file mode 100644 index 0000000000..2fe3f64a45 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/optional/BidirectionalOptionalOneToOneTest.java @@ -0,0 +1,106 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.onetoone.optional; + +import javax.persistence.Column; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.FailureExpected; +import org.junit.Test; + +import static javax.persistence.CascadeType.ALL; +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; + +/** + * @author Vlad Mihalcea + */ +public class BidirectionalOptionalOneToOneTest + extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Parent.class, + Child.class + }; + } + + @Test + @FailureExpected( jiraKey = "HHH-9460" ) + public void test() { + + doInJPA( this::entityManagerFactory, entityManager -> { + Parent a = new Parent(); + a.id = 1L; + Child b = new Child(); + b.id = 1L; + a.setChild(b); + b.setParent(a); + + entityManager.persist(a); + } ); + + doInJPA( this::entityManagerFactory, entityManager -> { + Parent a = entityManager.find(Parent.class, 1L); + + entityManager.remove(a); + } ); + } + + @javax.persistence.Entity + public static class Parent { + + @Id + @Column(unique = true, nullable = false) + private Long id; + + @OneToOne(optional = false, mappedBy = "parent", cascade = ALL) + private Child child; + + public Long getId() { + return id; + } + + public Child getChild() { + return child; + } + + public void setChild(Child child) { + this.child = child; + } + + } + + @javax.persistence.Entity + public static class Child { + + @Id + @Column(unique = true, nullable = false) + private Long id; + + @OneToOne(optional = false) + @JoinColumn(nullable = false) + private Parent parent; + + public Long getId() { + return id; + } + + public Parent getParent() { + return parent; + } + + public void setParent(Parent parent) { + this.parent = parent; + } + } + +}