[HHH-4552]

added multiple generated values to the test


git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18588 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Ståle W. Pedersen 2010-01-20 15:35:26 +00:00
parent cd3c152f44
commit e655b54eb8
3 changed files with 222 additions and 1 deletions

View File

@ -83,6 +83,31 @@ public class IdClassGeneratedValueTest extends TestCase {
s.getTransaction().commit();
s.close();
}
@SuppressWarnings({ "unchecked" })
public void testMultipleGeneratedValue() {
Session s = openSession();
s.beginTransaction();
Multiple m1 = new Multiple( 1000L, 10 );
s.persist( m1 );
Long m1Id1 = m1.getId1();
Long m1Id2 = m1.getId2();
Multiple m2 = new Multiple( 2000L, 20 );
s.persist( m2 );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
List<Simple> simpleList = s.createQuery( "select m from Multiple m" ).list();
assertEquals( simpleList.size(), 2 );
m1 = (Multiple) s.load( Multiple.class, new MultiplePK( m1Id1, m1Id2, 2L ) );
assertEquals( m1.getQuantity(), 10 );
s.clear();
s.createQuery( "delete Multiple" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
// public void testComplexIdClass() {
// Session s = openSession();
@ -128,7 +153,9 @@ public class IdClassGeneratedValueTest extends TestCase {
// CustomerInventory.class,
// Item.class,
Simple.class,
Simple2.class
Simple2.class,
Multiple.class
};
}
}

View File

@ -0,0 +1,96 @@
/*
* 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 org.hibernate.annotations.GenericGenerator;
/**
* An Entity containing a composite key with two generated values.
*
* @author <a href="mailto:stale.pedersen@jboss.org">Stale W. Pedersen</a>
*/
@Entity
@IdClass(MultiplePK.class)
@SuppressWarnings("serial")
public class Multiple implements Serializable
{
@Id
@GenericGenerator(name = "increment", strategy = "increment")
@GeneratedValue(generator = "increment")
private Long id1;
@Id
@GenericGenerator(name = "increment2", strategy = "increment")
@GeneratedValue(generator = "increment2")
private Long id2;
@Id
private Long id3;
private int quantity;
public Multiple()
{
}
public Multiple(Long id3, int quantity)
{
this.id3 = id3;
this.quantity = quantity;
}
public Long getId1()
{
return id1;
}
public Long getId2()
{
return id2;
}
public Long getId3()
{
return id3;
}
public int getQuantity()
{
return quantity;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
}

View File

@ -0,0 +1,98 @@
/*
* 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;
/**
* MultiplePK
*
* @author <a href="mailto:stale.pedersen@jboss.org">Stale W. Pedersen</a>
*/
public class MultiplePK implements Serializable
{
private final Long id1;
private final Long id2;
private final Long id3;
// AnnotationBinder (incorrectly) requires this to be transient; see HHH-4819 and HHH-4820
private final transient int cachedHashCode;
private MultiplePK()
{
id1 = null;
id2 = null;
id3 = null;
cachedHashCode = super.hashCode();
}
public MultiplePK(Long id1, Long id2, Long id3)
{
this.id1 = id1;
this.id2 = id2;
this.id3 = id3;
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;
}
public Long getId3() {
return id3;
}
@Override
public boolean equals(Object o)
{
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
MultiplePK multiplePK = (MultiplePK) o;
return id1.equals( multiplePK.id1 )
&& id2.equals( multiplePK.id2 )
&& id3.equals( multiplePK.id3);
}
@Override
public int hashCode() {
return cachedHashCode;
}
}