From e655b54eb80efd81f255b35388cb50044c9ab799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A5le=20W=2E=20Pedersen?= Date: Wed, 20 Jan 2010 15:35:26 +0000 Subject: [PATCH] [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 --- .../IdClassGeneratedValueTest.java | 29 +++++- .../idclassgeneratedvalue/Multiple.java | 96 ++++++++++++++++++ .../idclassgeneratedvalue/MultiplePK.java | 98 +++++++++++++++++++ 3 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/MultiplePK.java diff --git a/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/IdClassGeneratedValueTest.java b/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/IdClassGeneratedValueTest.java index 7660c3d0de..bf84cb9919 100755 --- a/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/IdClassGeneratedValueTest.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/IdClassGeneratedValueTest.java @@ -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 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 + }; } } diff --git a/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java b/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java new file mode 100644 index 0000000000..4e3d194e8c --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java @@ -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 Stale W. Pedersen + */ +@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; + } + + +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/MultiplePK.java b/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/MultiplePK.java new file mode 100644 index 0000000000..e062813436 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/MultiplePK.java @@ -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 Stale W. Pedersen + */ +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; + } +}