Fixed files truncated by merge from master

This commit is contained in:
Steve Ebersole 2012-06-08 14:32:09 -05:00
parent 758c2491b5
commit 55abc2e357
7 changed files with 651 additions and 0 deletions

View File

@ -0,0 +1,122 @@
package org.hibernate.test.annotations.manytoone;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* @author Emmanuel Bernard
*/
@Entity
public class Carz implements Serializable {
@Id
private Integer id;
@Column( name = "make", nullable = false )
private String make;
@Column( name = "model", nullable = false )
private String model;
@Column( name = "manufactured", nullable = false )
@Temporal( TemporalType.TIMESTAMP )
private Date manufactured;
@ManyToOne( fetch = FetchType.LAZY )
@JoinColumn( name = "loc_code", referencedColumnName = "loc_code" )
private Lotz lot;
public Carz() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Lotz getLot() {
return this.lot;
}
public void setLot(Lotz lot) {
this.lot = lot;
}
public String getMake() {
return this.make;
}
public void setMake(String make) {
this.make = make;
}
public Date getManufactured() {
return this.manufactured;
}
public void setManufactured(Date manufactured) {
this.manufactured = manufactured;
}
public String getModel() {
return this.model;
}
public void setModel(String model) {
this.model = model;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ( ( this.id == null ) ?
0 :
this.id.hashCode() );
result = PRIME * result + ( ( this.make == null ) ?
0 :
this.make.hashCode() );
result = PRIME * result + ( ( this.manufactured == null ) ?
0 :
this.manufactured.hashCode() );
result = PRIME * result + ( ( this.model == null ) ?
0 :
this.model.hashCode() );
return result;
}
@Override
public boolean equals(Object obj) {
if ( this == obj ) return true;
if ( obj == null ) return false;
if ( getClass() != obj.getClass() ) return false;
final Carz other = (Carz) obj;
if ( this.id == null ) {
if ( other.id != null ) return false;
}
else if ( !this.id.equals( other.id ) ) return false;
if ( this.make == null ) {
if ( other.make != null ) return false;
}
else if ( !this.make.equals( other.make ) ) return false;
if ( this.manufactured == null ) {
if ( other.manufactured != null ) return false;
}
else if ( !this.manufactured.equals( other.manufactured ) ) return false;
if ( this.model == null ) {
if ( other.model != null ) return false;
}
else if ( !this.model.equals( other.model ) ) return false;
return true;
}
}

View File

@ -0,0 +1,100 @@
package org.hibernate.test.annotations.manytoone;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
/**
* @author Emmanuel Bernard
*/
@Entity
public class Lotz implements Serializable {
@EmbeddedId
protected LotzPK lotPK;
@Column( name = "name", nullable = false )
private String name;
@Column( name = "location", nullable = false )
private String location;
@OneToMany( mappedBy = "lot", fetch = FetchType.LAZY, cascade = CascadeType.ALL )
private List<Carz> cars;
public Lotz() {
}
public List<Carz> getCars() {
return this.cars;
}
public void setCars(List<Carz> cars) {
this.cars = cars;
}
public String getLocation() {
return this.location;
}
public void setLocation(String location) {
this.location = location;
}
public LotzPK getLotPK() {
return this.lotPK;
}
public void setLotPK(LotzPK lotPK) {
this.lotPK = lotPK;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ( ( this.location == null ) ?
0 :
this.location.hashCode() );
result = PRIME * result + ( ( this.lotPK == null ) ?
0 :
this.lotPK.hashCode() );
result = PRIME * result + ( ( this.name == null ) ?
0 :
this.name.hashCode() );
return result;
}
@Override
public boolean equals(Object obj) {
if ( this == obj ) return true;
if ( obj == null ) return false;
if ( getClass() != obj.getClass() ) return false;
final Lotz other = (Lotz) obj;
if ( this.location == null ) {
if ( other.location != null ) return false;
}
else if ( !this.location.equals( other.location ) ) return false;
if ( this.lotPK == null ) {
if ( other.lotPK != null ) return false;
}
else if ( !this.lotPK.equals( other.lotPK ) ) return false;
if ( this.name == null ) {
if ( other.name != null ) return false;
}
else if ( !this.name.equals( other.name ) ) return false;
return true;
}
}

View File

@ -0,0 +1,33 @@
package org.hibernate.test.annotations.manytoone;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
/**
* @author Emmanuel Bernard
*/
@Embeddable
public class LotzPK implements Serializable {
@Column( name = "id", nullable = false )
private Integer id;
@Column( name = "loc_code", nullable = false, unique = true )
private String locCode;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLocCode() {
return locCode;
}
public void setLocCode(String locCode) {
this.locCode = locCode;
}
}

View File

@ -0,0 +1,60 @@
<?xml version="1.0"?>
<hibernate-mapping package="org.hibernate.test.discriminator" default-access="field"
xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping hibernate-mapping-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.test.discriminator"
default-access="field">
-->
<!--
This mapping demonstrates a simple table-per-hierarchy mapping strategy;
each subclass has simple properties
-->
<class name="Person"
discriminator-value="P">
<id name="id"
column="person_id"
unsaved-value="0">
<generator class="assigned"/>
</id>
<discriminator column="TYPE" type="character"/>
<property name="name"
not-null="true"
length="80"/>
<property name="sex"
not-null="true"
update="false"/>
<subclass name="Employee"
discriminator-value="E">
<property name="title" length="20"/>
<property name="salary" />
<!-- commented out until HHH-6551 is fixed
<subclass name="PartTimeEmployee" discriminator-value="M">
<property name="percent"/>
</subclass>
-->
</subclass>
<subclass name="Customer"
discriminator-value="C">
<property name="comments"/>
</subclass>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,273 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2006-2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* 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, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY 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
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.discriminator;
import java.math.BigDecimal;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Gavin King
*/
public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( USE_NEW_METADATA_MAPPINGS, "true");
}
@Override
public String[] getMappings() {
return new String[] { "discriminator/SimpleInheritance.hbm.xml" };
}
@Test
public void testDiscriminatorSubclass() {
Session s = openSession();
Transaction t = s.beginTransaction();
Employee mark = new Employee();
mark.setId( 1 );
mark.setName( "Mark" );
mark.setTitle( "internal sales" );
mark.setSex( 'M' );
Customer joe = new Customer();
joe.setId( 2 );
joe.setName( "Joe" );
joe.setComments( "Very demanding" );
joe.setSex( 'M' );
Person yomomma = new Person();
yomomma.setId( 3 );
yomomma.setName("mum");
yomomma.setSex('F');
s.save(yomomma);
s.save(mark);
s.save(joe);
assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 );
assertEquals( s.createQuery("from org.hibernate.test.discriminator.Person").list().size(), 3 );
assertEquals( s.createQuery("from org.hibernate.test.discriminator.Person p where p.class = org.hibernate.test.discriminator.Person").list().size(), 1 );
assertEquals( s.createQuery("from org.hibernate.test.discriminator.Person p where p.class = org.hibernate.test.discriminator.Customer").list().size(), 1 );
assertEquals( s.createQuery("from org.hibernate.test.discriminator.Person p where type(p) = :who").setParameter("who", Person.class).list().size(), 1 );
assertEquals( s.createQuery("from org.hibernate.test.discriminator.Person p where type(p) in :who").setParameterList("who", new Class[] {Customer.class, Person.class}).list().size(), 2 );
s.clear();
List customers = s.createQuery("from org.hibernate.test.discriminator.Customer").list();
for ( Object customer : customers ) {
Customer c = (Customer) customer;
assertEquals( "Very demanding", c.getComments() );
}
assertEquals( customers.size(), 1 );
s.clear();
mark = (Employee) s.get( Employee.class, mark.getId() );
joe = (Customer) s.get( Customer.class, joe.getId() );
s.delete(mark);
s.delete(joe);
s.delete(yomomma);
assertTrue( s.createQuery("from org.hibernate.test.discriminator.Person").list().isEmpty() );
t.commit();
s.close();
}
@Test
public void testAccessAsIncorrectSubclass() {
Session s = openSession();
s.beginTransaction();
Employee e = new Employee();
e.setId( 4 );
e.setName( "Steve" );
e.setSex( 'M' );
e.setTitle( "grand poobah" );
s.save( e );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
Customer c = ( Customer ) s.get( Customer.class, e.getId() );
s.getTransaction().commit();
s.close();
assertNull( c );
s = openSession();
s.beginTransaction();
e = ( Employee ) s.get( Employee.class, e.getId() );
c = ( Customer ) s.get( Customer.class, e.getId() );
s.getTransaction().commit();
s.close();
assertNotNull( e );
assertNull( c );
s = openSession();
s.beginTransaction();
s.delete( e );
s.getTransaction().commit();
s.close();
}
@Test
public void testQuerySubclassAttribute() {
Session s = openSession();
Transaction t = s.beginTransaction();
Person p = new Person();
p.setId( 5 );
p.setName("Emmanuel");
p.setSex('M');
s.save( p );
Employee q = new Employee();
q.setId( 6 );
q.setName("Steve");
q.setSex('M');
q.setTitle("Mr");
q.setSalary( new BigDecimal(1000) );
s.save( q );
List result = s.createQuery("from org.hibernate.test.discriminator.Person where salary > 100").list();
assertEquals( result.size(), 1 );
assertSame( result.get(0), q );
result = s.createQuery("from org.hibernate.test.discriminator.Person where salary > 100 or name like 'E%'").list();
assertEquals( result.size(), 2 );
result = s.createCriteria(Person.class)
.add( Property.forName("salary").gt( new BigDecimal(100) ) )
.list();
assertEquals( result.size(), 1 );
assertSame( result.get(0), q );
//TODO: make this work:
/*result = s.createQuery("select salary from Person where salary > 100").list();
assertEquals( result.size(), 1 );
assertEquals( result.get(0), new BigDecimal(1000) );*/
s.delete(p);
s.delete(q);
t.commit();
s.close();
}
@Test
public void testLoadSuperclassProxyPolymorphicAccess() {
Session s = openSession();
s.beginTransaction();
Employee e = new Employee();
e.setId( 7 );
e.setName( "Steve" );
e.setSex( 'M' );
e.setTitle( "grand poobah" );
s.save( e );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
// load the superclass proxy.
Person pLoad = ( Person ) s.load( Person.class, new Long( e.getId() ) );
assertTrue( pLoad instanceof HibernateProxy);
Person pGet = ( Person ) s.get( Person.class, e.getId());
Person pQuery = ( Person ) s.createQuery( "from org.hibernate.test.discriminator.Person where id = :id" )
.setLong( "id", e.getId() )
.uniqueResult();
Person pCriteria = ( Person ) s.createCriteria( Person.class )
.add( Restrictions.idEq( e.getId() ) )
.uniqueResult();
// assert that executing the queries polymorphically returns the same proxy
assertSame( pLoad, pGet );
assertSame( pLoad, pQuery );
assertSame( pLoad, pCriteria );
// assert that the proxy is not an instance of Employee
assertFalse( pLoad instanceof Employee );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.delete( e );
s.getTransaction().commit();
s.close();
}
@Test
public void testLoadSuperclassProxyEvictPolymorphicAccess() {
Session s = openSession();
s.beginTransaction();
Employee e = new Employee();
e.setId( 8 );
e.setName( "Steve" );
e.setSex( 'M' );
e.setTitle( "grand poobah" );
s.save( e );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
// load the superclass proxy.
Person pLoad = ( Person ) s.load( Person.class, new Long( e.getId() ) );
assertTrue( pLoad instanceof HibernateProxy);
// evict the proxy
s.evict( pLoad );
Employee pGet = ( Employee ) s.get( Person.class, e.getId() );
Employee pQuery = ( Employee ) s.createQuery( "from org.hibernate.test.discriminator.Person where id = :id" )
.setLong( "id", e.getId() )
.uniqueResult();
Employee pCriteria = ( Employee ) s.createCriteria( Person.class )
.add( Restrictions.idEq( e.getId() ) )
.uniqueResult();
// assert that executing the queries polymorphically returns the same Employee instance
assertSame( pGet, pQuery );
assertSame( pGet, pCriteria );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.delete( e );
s.getTransaction().commit();
s.close();
}
}

View File

@ -0,0 +1,49 @@
<?xml version="1.0"?>
<hibernate-mapping package="org.hibernate.test.inheritance" default-access="field"
xmlns="http://www.hibernate.org/xsd/hibernate-mapping">
<!--
This mapping demonstrates a simple table-per-hierarchy mapping strategy;
each subclass has simple properties
-->
<class name="Person"
discriminator-value="P">
<id name="id"
column="person_id"
unsaved-value="0">
<generator class="assigned"/>
</id>
<discriminator column="TYPE" type="character"/>
<property name="name"
not-null="true"
length="80"/>
<property name="sex"
not-null="true"
update="false"/>
<subclass name="Employee"
discriminator-value="E">
<property name="title" length="20"/>
<property name="salary" />
<!-- commented out until HHH-6551 is fixed
<subclass name="PartTimeEmployee" discriminator-value="M">
<property name="percent"/>
</subclass>
-->
</subclass>
<subclass name="Customer"
discriminator-value="C">
<property name="comments"/>
</subclass>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,14 @@
<?xml version="1.0"?>
<hibernate-mapping package="org.hibernate.test.ops" xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping hibernate-mapping-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<class name="SimpleEntity">
<id name="id">
<generator class="assigned"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>