[hibernate-commits] Hibernate SVN: r18588 - core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Jan 20 10:35:26 EST 2010


Author: stalep
Date: 2010-01-20 10:35:26 -0500 (Wed, 20 Jan 2010)
New Revision: 18588

Added:
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/MultiplePK.java
Modified:
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/IdClassGeneratedValueTest.java
Log:
[HHH-4552]
added multiple generated values to the test


Modified: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/IdClassGeneratedValueTest.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/IdClassGeneratedValueTest.java	2010-01-20 15:23:45 UTC (rev 18587)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/IdClassGeneratedValueTest.java	2010-01-20 15:35:26 UTC (rev 18588)
@@ -83,7 +83,32 @@
 		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();
 //		Transaction tx = s.beginTransaction();
@@ -128,7 +153,9 @@
 //				CustomerInventory.class,
 //				Item.class,
 				Simple.class,
-				Simple2.class
+				Simple2.class,
+				Multiple.class
+				
 		};
 	}
 }

Added: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java	                        (rev 0)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/Multiple.java	2010-01-20 15:35:26 UTC (rev 18588)
@@ -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 at jboss.org">Stale W. Pedersen</a>
+ */
+ at Entity
+ at IdClass(MultiplePK.class)
+ at 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;
+   }
+   
+   
+}

Added: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/MultiplePK.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/MultiplePK.java	                        (rev 0)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/idclassgeneratedvalue/MultiplePK.java	2010-01-20 15:35:26 UTC (rev 18588)
@@ -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 at 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;
+   }
+}



More information about the hibernate-commits mailing list