HHH-7915 test case and cleanup

This commit is contained in:
Brett Meyer 2013-08-21 15:15:25 -04:00
parent 7ef045ae2c
commit 9ee5392456
6 changed files with 128 additions and 21 deletions

View File

@ -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;

View File

@ -0,0 +1,5 @@
//$Id$
package org.hibernate.test.annotations.polymorphism;
public class Automobile {
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,9 +0,0 @@
//$Id$
package org.hibernate.test.annotations.polymorphism;
/**
* @author Emmanuel Bernard
*/
public class MovingThing {
}

View File

@ -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
};
}
}