HHH-4552 - Support generated value within composite keys
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18582 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
63d5a97854
commit
aa9dd93555
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, 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.annotations.idclassgeneratedvalue;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.test.annotations.TestCase;
|
||||
|
||||
/**
|
||||
* A test.
|
||||
*
|
||||
* @author <a href="mailto:stale.pedersen@jboss.org">Stale W. Pedersen</a>
|
||||
*/
|
||||
public class IdClassGeneratedValueTest extends TestCase {
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
public void testBaseLine() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Simple s1 = new Simple( 1L, 2L, 10 );
|
||||
s.persist( s1 );
|
||||
Simple s2 = new Simple( 2L, 3L, 20 );
|
||||
s.persist( s2 );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
List<Simple> simpleList = s.createQuery( "select s from Simple s" ).list();
|
||||
assertEquals( simpleList.size(), 2 );
|
||||
s1 = (Simple) s.load( Simple.class, new SimplePK( 1L, 2L ) );
|
||||
assertEquals( s1.getQuantity(), 10 );
|
||||
s.clear();
|
||||
s.createQuery( "delete Simple" ).executeUpdate();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
public void testSingleGeneratedValue() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Simple2 s1 = new Simple2( 200L, 10 );
|
||||
s.persist( s1 );
|
||||
Long s1Id1 = s1.getId1();
|
||||
Simple2 s2 = new Simple2( 300L, 20 );
|
||||
s.persist( s2 );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
List<Simple> simpleList = s.createQuery( "select s from Simple2 s" ).list();
|
||||
assertEquals( simpleList.size(), 2 );
|
||||
s1 = (Simple2) s.load( Simple2.class, new SimplePK( s1Id1, 2L ) );
|
||||
assertEquals( s1.getQuantity(), 10 );
|
||||
s.clear();
|
||||
s.createQuery( "delete Simple2" ).executeUpdate();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
// public void testComplexIdClass() {
|
||||
// Session s = openSession();
|
||||
// Transaction tx = s.beginTransaction();
|
||||
//
|
||||
// Customer c1 = new Customer(
|
||||
// "foo", "bar", "contact1", "100", new BigDecimal( 1000 ), new BigDecimal( 1000 ), new BigDecimal( 1000 )
|
||||
// );
|
||||
//
|
||||
// s.persist( c1 );
|
||||
// Item boat = new Item();
|
||||
// boat.setId( "1" );
|
||||
// boat.setName( "cruiser" );
|
||||
// boat.setPrice( new BigDecimal( 500 ) );
|
||||
// boat.setDescription( "a boat" );
|
||||
// boat.setCategory( 42 );
|
||||
//
|
||||
// s.persist( boat );
|
||||
// s.flush();
|
||||
// s.clear();
|
||||
//
|
||||
// c1.addInventory( boat, 10, new BigDecimal( 5000 ) );
|
||||
// s.merge( c1 );
|
||||
// s.flush();
|
||||
// s.clear();
|
||||
//
|
||||
// Customer c2 = (Customer) s.createQuery( "select c from Customer c" ).uniqueResult();
|
||||
//
|
||||
// List<CustomerInventory> inventory = c2.getInventories();
|
||||
//
|
||||
// assertEquals( 1, inventory.size() );
|
||||
// assertEquals( 10, inventory.get( 0 ).getQuantity() );
|
||||
//
|
||||
// tx.rollback();
|
||||
// s.close();
|
||||
//
|
||||
// assertTrue( true );
|
||||
// }
|
||||
|
||||
protected Class[] getMappings() {
|
||||
return new Class[] {
|
||||
// Customer.class,
|
||||
// CustomerInventory.class,
|
||||
// Item.class,
|
||||
Simple.class,
|
||||
Simple2.class
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, 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.annotations.idclassgeneratedvalue;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
/**
|
||||
* A Simple entity class.
|
||||
*
|
||||
* @author <a href="mailto:stale.pedersen@jboss.org">Stale W. Pedersen</a>
|
||||
*/
|
||||
@Entity
|
||||
@IdClass(SimplePK.class)
|
||||
@SuppressWarnings("serial")
|
||||
public class Simple implements Serializable {
|
||||
@Id
|
||||
private Long id1;
|
||||
|
||||
@Id
|
||||
private Long id2;
|
||||
|
||||
private int quantity;
|
||||
|
||||
public Simple() {
|
||||
}
|
||||
|
||||
public Simple(Long id1, Long id2, int quantity) {
|
||||
this.id1 = id1;
|
||||
this.id2 = id2;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public Long getId1() {
|
||||
return id1;
|
||||
}
|
||||
|
||||
public Long getId2() {
|
||||
return id2;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, 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.annotations.idclassgeneratedvalue;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
/**
|
||||
* The {@link Simple} entity redone with a generated value {@link #id1} }as part of its
|
||||
* composite pk
|
||||
*
|
||||
* @author <a href="mailto:stale.pedersen@jboss.org">Stale W. Pedersen</a>
|
||||
*/
|
||||
@Entity
|
||||
@IdClass(SimplePK.class)
|
||||
@SuppressWarnings("serial")
|
||||
public class Simple2 implements Serializable {
|
||||
@Id
|
||||
@GenericGenerator(name = "increment", strategy = "increment")
|
||||
@GeneratedValue(generator = "increment")
|
||||
private Long id1;
|
||||
|
||||
@Id
|
||||
private Long id2;
|
||||
|
||||
private int quantity;
|
||||
|
||||
public Simple2() {
|
||||
}
|
||||
|
||||
public Simple2(Long id, int quantity) {
|
||||
this.id2 = id;
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public Long getId1() {
|
||||
return id1;
|
||||
}
|
||||
|
||||
public Long getId2() {
|
||||
return id2;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, 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.annotations.idclassgeneratedvalue;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* A SimplePK.
|
||||
*
|
||||
* @author <a href="mailto:stale.pedersen@jboss.org">Stale W. Pedersen</a>
|
||||
*/
|
||||
public class SimplePK implements Serializable {
|
||||
private final Long id1;
|
||||
private final Long id2;
|
||||
// AnnotationBinder (incorrectly) requires this to be transient; see HHH-4819 and HHH-4820
|
||||
private final transient int cachedHashCode;
|
||||
|
||||
private SimplePK() {
|
||||
// required by Hibernate, though never used; see HHH-4818
|
||||
id1 = null;
|
||||
id2 = null;
|
||||
cachedHashCode = super.hashCode();
|
||||
}
|
||||
|
||||
public SimplePK(Long id1, Long id2) {
|
||||
this.id1 = id1;
|
||||
this.id2 = id2;
|
||||
this.cachedHashCode = calculateHashCode();
|
||||
}
|
||||
|
||||
private int calculateHashCode() {
|
||||
int result = id1.hashCode();
|
||||
result = 31 * result + id2.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
public Long getId1() {
|
||||
return id1;
|
||||
}
|
||||
|
||||
public Long getId2() {
|
||||
return id2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SimplePK simplePK = (SimplePK) o;
|
||||
|
||||
return id1.equals( simplePK.id1 )
|
||||
&& id2.equals( simplePK.id2 );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return cachedHashCode;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue