From 9ee5392456333ef93baaf866025e5f7f00ed2e92 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Wed, 21 Aug 2013 15:15:25 -0400 Subject: [PATCH] HHH-7915 test case and cleanup --- .../hibernate/mapping/DenormalizedTable.java | 3 +- .../annotations/polymorphism/Automobile.java | 5 ++ .../test/annotations/polymorphism/Car.java | 33 ++++++++--- .../polymorphism/MarketRegion.java | 56 +++++++++++++++++++ .../annotations/polymorphism/MovingThing.java | 9 --- .../polymorphism/PolymorphismTest.java | 43 +++++++++++++- 6 files changed, 128 insertions(+), 21 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/Automobile.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/MarketRegion.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/MovingThing.java diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java b/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java index aac0be9ca1..501041b9b0 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java @@ -22,11 +22,10 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.mapping; + import java.util.ArrayList; -import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; import org.hibernate.internal.util.collections.JoinedIterator; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/Automobile.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/Automobile.java new file mode 100644 index 0000000000..b6061a4280 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/Automobile.java @@ -0,0 +1,5 @@ +//$Id$ +package org.hibernate.test.annotations.polymorphism; + +public class Automobile { +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/Car.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/Car.java index 762de81134..e9c3100713 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/Car.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/Car.java @@ -2,10 +2,11 @@ package org.hibernate.test.annotations.polymorphism; import javax.persistence.Entity; import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; import org.hibernate.annotations.PolymorphismType; @@ -15,9 +16,26 @@ import org.hibernate.annotations.PolymorphismType; @Entity @Inheritance(strategy= InheritanceType.TABLE_PER_CLASS) @org.hibernate.annotations.Entity(polymorphism = PolymorphismType.EXPLICIT) -public class Car extends MovingThing { - private Integer id; +public class Car extends Automobile { + + @Id + @GeneratedValue + private long id; + private String model; + + @ManyToOne + // purposefully refer to a non-PK column (HHH-7915) + @JoinColumn( referencedColumnName = "REGION_CODE") + private MarketRegion marketRegion; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } public String getModel() { return model; @@ -27,12 +45,11 @@ public class Car extends MovingThing { this.model = model; } - @Id @GeneratedValue(strategy = GenerationType.TABLE ) - public Integer getId() { - return id; + public MarketRegion getMarketRegion() { + return marketRegion; } - public void setId(Integer id) { - this.id = id; + public void setMarketRegion(MarketRegion marketRegion) { + this.marketRegion = marketRegion; } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/MarketRegion.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/MarketRegion.java new file mode 100644 index 0000000000..f0a3105e33 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/MarketRegion.java @@ -0,0 +1,56 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @authors tag. All rights reserved. + * See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License, v. 2.1. + * This program is distributed in the hope that it will be useful, but WITHOUT A + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * You should have received a copy of the GNU Lesser General Public License, + * v.2.1 along with this distribution; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +package org.hibernate.test.annotations.polymorphism; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Brett Meyer + */ +@Entity +public class MarketRegion { + + @Id + @GeneratedValue + private long id; + + @Column(name = "REGION_CODE") + private String regionCode; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getRegionCode() { + return regionCode; + } + + public void setRegionCode(String regionCode) { + this.regionCode = regionCode; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/MovingThing.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/MovingThing.java deleted file mode 100644 index b2ff8868c1..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/MovingThing.java +++ /dev/null @@ -1,9 +0,0 @@ -//$Id$ -package org.hibernate.test.annotations.polymorphism; - - -/** - * @author Emmanuel Bernard - */ -public class MovingThing { -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/PolymorphismTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/PolymorphismTest.java index 9e0d7c7756..211482435c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/PolymorphismTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/polymorphism/PolymorphismTest.java @@ -27,14 +27,17 @@ import org.junit.Test; import org.hibernate.Session; import org.hibernate.Transaction; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; /** * @author Emmanuel Bernard + * @author Brett Meyer */ public class PolymorphismTest extends BaseCoreFunctionalTestCase { + @Test public void testPolymorphism() throws Exception { Car car = new Car(); @@ -47,7 +50,42 @@ public class PolymorphismTest extends BaseCoreFunctionalTestCase { s.persist( car2 ); s.flush(); assertEquals( 2, s.createQuery( "select car from Car car").list().size() ); - assertEquals( 0, s.createQuery( "select count(m) from " + MovingThing.class.getName() + " m").list().size() ); + assertEquals( 0, s.createQuery( "select count(m) from " + Automobile.class.getName() + " m").list().size() ); + tx.rollback(); + s.close(); + + } + + @Test + @TestForIssue(jiraKey = "HHH-7915") + public void testNonPkInheritedFk() throws Exception { + MarketRegion region1 = new MarketRegion(); + region1.setRegionCode( "US" ); + MarketRegion region2 = new MarketRegion(); + region2.setRegionCode( "EU" ); + + Car car = new Car(); + car.setModel( "SUV" ); + car.setMarketRegion( region1 ); + + SportCar car2 = new SportCar(); + car2.setModel( "350Z" ); + car2.setMarketRegion( region2 ); + + Session s = openSession(); + Transaction tx = s.beginTransaction(); + s.persist( region1 ); + s.persist( region2 ); + s.persist( car ); + s.persist( car2 ); + + s.flush(); + + assertEquals( 1, s.createQuery( "select car from Car car where car.marketRegion.regionCode='US'") + .list().size() ); + assertEquals( 1, s.createQuery( "select car from SportCar car where car.marketRegion.regionCode='EU'") + .list().size() ); + tx.rollback(); s.close(); @@ -57,7 +95,8 @@ public class PolymorphismTest extends BaseCoreFunctionalTestCase { protected Class[] getAnnotatedClasses() { return new Class[] { Car.class, - SportCar.class + SportCar.class, + MarketRegion.class }; } }