From 71834d06565038d8dbab86260684aa952320ed4a Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 12 Jan 2018 18:04:32 -0800 Subject: [PATCH] HHH-12150 HHH-10575 : Add test for HHH-10575; add @TestForIssue to tests (cherry picked from commit 794c784c0de6439f25c25cd96ffad601dde9f11d) --- .../MapKeyColumnBiDiOneToManyFKTest.java | 188 ++++++++++++++++++ .../MapKeyColumnElementCollectionTest.java | 5 +- .../MapKeyColumnManyToManyTest.java | 5 +- .../MapKeyColumnOneToManyFKTest.java | 3 + .../MapKeyColumnOneToManyJoinTableTest.java | 4 +- 5 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnBiDiOneToManyFKTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnBiDiOneToManyFKTest.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnBiDiOneToManyFKTest.java new file mode 100644 index 0000000000..686d8209ea --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnBiDiOneToManyFKTest.java @@ -0,0 +1,188 @@ +/* + * 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 http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.test.jpa.compliance.tck2_2.mapkeycolumn; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.MapKeyColumn; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +import org.hibernate.boot.MetadataSources; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; + +/** + * @author Steve Ebersole + * @author Gail Badner + */ +public class MapKeyColumnBiDiOneToManyFKTest extends BaseNonConfigCoreFunctionalTestCase { + + @Test + @TestForIssue( jiraKey = "HHH-12150" ) + public void testReferenceToAlreadyMappedColumn() { + inTransaction( + session -> { + AddressCapable2 holder = new AddressCapable2( 1, "osd"); + Address2 address = new Address2( 1, "123 Main St" ); + + session.persist( holder ); + session.persist( address ); + } + ); + inTransaction( + session -> { + AddressCapable2 holder = session.get( AddressCapable2.class, 1 ); + Address2 address = session.get( Address2.class, 1 ); + + address.holder = holder; + holder.addresses.put( "work", address ); + + session.persist( holder ); + } + ); + inTransaction( + session -> { + AddressCapable2 holder = session.get( AddressCapable2.class, 1 ); + assertEquals( 1, holder.addresses.size() ); + final Map.Entry entry = holder.addresses.entrySet().iterator().next(); + assertEquals( "work", entry.getKey() ); + assertEquals( "work", entry.getValue().type ); + session.remove( holder ); + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-12150" ) + public void testReferenceToNonMappedColumn() { + inTransaction( + session -> { + AddressCapable holder = new AddressCapable( 1, "osd"); + Address address = new Address( 1, "123 Main St" ); + + session.persist( holder ); + session.persist( address ); + } + ); + inTransaction( + session -> { + AddressCapable holder = session.get( AddressCapable.class, 1 ); + Address address = session.get( Address.class, 1 ); + + address.holder = holder; + holder.addresses.put( "work", address ); + + session.persist( holder ); + } + ); + inTransaction( + session -> { + AddressCapable holder = session.get( AddressCapable.class, 1 ); + assertEquals( 1, holder.addresses.size() ); + final Map.Entry entry = holder.addresses.entrySet().iterator().next(); + assertEquals( "work", entry.getKey() ); + session.remove( holder ); + } + ); + } + + @Override + protected void applyMetadataSources(MetadataSources sources) { + super.applyMetadataSources( sources ); + + sources.addAnnotatedClass( AddressCapable.class ); + sources.addAnnotatedClass( AddressCapable2.class ); + sources.addAnnotatedClass( Address.class ); + sources.addAnnotatedClass( Address2.class ); + } + + @Entity( name = "AddressCapable" ) + @Table( name = "address_capables" ) + public static class AddressCapable { + @Id + public Integer id; + public String name; + @MapKeyColumn( name = "a_type" ) + @OneToMany( mappedBy = "holder", cascade = {CascadeType.PERSIST, CascadeType.REMOVE} ) + public Map addresses = new HashMap<>(); + + public AddressCapable() { + } + + public AddressCapable(Integer id, String name) { + this.id = id; + this.name = name; + } + } + + @Entity( name = "Address" ) + @Table( name = "addresses" ) + public static class Address { + @Id + public Integer id; + public String street; + @ManyToOne + public AddressCapable holder; + + public Address() { + } + + public Address(Integer id, String street) { + this.id = id; + this.street = street; + } + } + + @Entity( name = "AddressCapable2" ) + @Table( name = "address_capables2" ) + public static class AddressCapable2 { + @Id + public Integer id; + public String name; + @MapKeyColumn( name = "a_type" ) + @OneToMany( mappedBy = "holder", cascade = {CascadeType.PERSIST, CascadeType.REMOVE} ) + public Map addresses = new HashMap<>(); + + public AddressCapable2() { + } + + public AddressCapable2(Integer id, String name) { + this.id = id; + this.name = name; + } + } + + @Entity( name = "Address2" ) + @Table( name = "addresses2" ) + public static class Address2 { + @Id + public Integer id; + public String street; + @Column( name = "a_type" ) + public String type; + @ManyToOne + public AddressCapable2 holder; + + public Address2() { + } + + public Address2(Integer id, String street) { + this.id = id; + this.street = street; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnElementCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnElementCollectionTest.java index 803864322b..1620a356af 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnElementCollectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnElementCollectionTest.java @@ -14,13 +14,12 @@ import javax.persistence.ElementCollection; import javax.persistence.Embeddable; import javax.persistence.Entity; import javax.persistence.Id; -import javax.persistence.JoinColumn; import javax.persistence.MapKeyColumn; -import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.boot.MetadataSources; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; @@ -32,6 +31,7 @@ import static junit.framework.Assert.assertEquals; public class MapKeyColumnElementCollectionTest extends BaseNonConfigCoreFunctionalTestCase { @Test + @TestForIssue( jiraKey = "HHH-12150" ) public void testReferenceToAlreadyMappedColumn() { inTransaction( session -> { @@ -64,6 +64,7 @@ public class MapKeyColumnElementCollectionTest extends BaseNonConfigCoreFunction } @Test + @TestForIssue( jiraKey = "HHH-12150" ) public void testReferenceToNonMappedColumn() { inTransaction( session -> { diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnManyToManyTest.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnManyToManyTest.java index 10e8fe45eb..648d96f997 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnManyToManyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnManyToManyTest.java @@ -12,14 +12,13 @@ import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; -import javax.persistence.JoinColumn; import javax.persistence.ManyToMany; import javax.persistence.MapKeyColumn; -import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.boot.MetadataSources; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; @@ -31,6 +30,7 @@ import static junit.framework.Assert.assertEquals; public class MapKeyColumnManyToManyTest extends BaseNonConfigCoreFunctionalTestCase { @Test + @TestForIssue( jiraKey = "HHH-12150" ) public void testReferenceToAlreadyMappedColumn() { inTransaction( session -> { @@ -64,6 +64,7 @@ public class MapKeyColumnManyToManyTest extends BaseNonConfigCoreFunctionalTestC } @Test + @TestForIssue( jiraKey = "HHH-12150" ) public void testReferenceToNonMappedColumn() { inTransaction( session -> { diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnOneToManyFKTest.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnOneToManyFKTest.java index cb9568da83..3375d614d2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnOneToManyFKTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnOneToManyFKTest.java @@ -19,6 +19,7 @@ import javax.persistence.Table; import org.hibernate.boot.MetadataSources; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; @@ -30,6 +31,7 @@ import static junit.framework.Assert.assertEquals; public class MapKeyColumnOneToManyFKTest extends BaseNonConfigCoreFunctionalTestCase { @Test + @TestForIssue( jiraKey = "HHH-12150" ) public void testReferenceToAlreadyMappedColumn() { inTransaction( session -> { @@ -63,6 +65,7 @@ public class MapKeyColumnOneToManyFKTest extends BaseNonConfigCoreFunctionalTest } @Test + @TestForIssue( jiraKey = "HHH-12150" ) public void testReferenceToNonMappedColumn() { inTransaction( session -> { diff --git a/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnOneToManyJoinTableTest.java b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnOneToManyJoinTableTest.java index da14c5832f..409afc84f7 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnOneToManyJoinTableTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/jpa/compliance/tck2_2/mapkeycolumn/MapKeyColumnOneToManyJoinTableTest.java @@ -12,13 +12,13 @@ import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; -import javax.persistence.JoinColumn; import javax.persistence.MapKeyColumn; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.boot.MetadataSources; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; @@ -30,6 +30,7 @@ import static junit.framework.Assert.assertEquals; public class MapKeyColumnOneToManyJoinTableTest extends BaseNonConfigCoreFunctionalTestCase { @Test + @TestForIssue( jiraKey = "HHH-12150" ) public void testReferenceToAlreadyMappedColumn() { inTransaction( session -> { @@ -63,6 +64,7 @@ public class MapKeyColumnOneToManyJoinTableTest extends BaseNonConfigCoreFunctio } @Test + @TestForIssue( jiraKey = "HHH-12150" ) public void testReferenceToNonMappedColumn() { inTransaction( session -> {