[hibernate-commits] Hibernate SVN: r11077 - in branches/Branch_3_2/Hibernate3/test/org/hibernate/test: keymanytoone and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Jan 23 07:54:42 EST 2007


Author: steve.ebersole at jboss.com
Date: 2007-01-23 07:54:42 -0500 (Tue, 23 Jan 2007)
New Revision: 11077

Added:
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/KeyManyToOneSuite.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/Customer.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerMapping.hbm.xml
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/LazyKeyManyToOneTest.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/LazyMapping.hbm.xml
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/Order.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Customer.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Mapping.hbm.xml
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Order.java
Removed:
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/Card.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/CardField.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/Key.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/KeyManyToOneTestCase.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/PrimaryKey.java
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/card.hbm.xml
Modified:
   branches/Branch_3_2/Hibernate3/test/org/hibernate/test/AllTests.java
Log:
HHH-2277 : key-many-to-one (bidir + eager) : tests

Modified: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/AllTests.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/AllTests.java	2007-01-23 12:54:10 UTC (rev 11076)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/AllTests.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -58,6 +58,7 @@
 import org.hibernate.test.joinedsubclass.JoinedSubclassTest;
 import org.hibernate.test.joinfetch.JoinFetchTest;
 import org.hibernate.test.jpa.JPAComplianceSuite;
+import org.hibernate.test.keymanytoone.KeyManyToOneSuite;
 import org.hibernate.test.lazycache.InstrumentCacheTest;
 import org.hibernate.test.lazycache.InstrumentCacheTest2;
 import org.hibernate.test.lazyonetoone.LazyOneToOneTest;
@@ -66,7 +67,6 @@
 import org.hibernate.test.legacy.CacheTest;
 import org.hibernate.test.legacy.ComponentNotNullTest;
 import org.hibernate.test.legacy.ConfigurationPerformanceTest;
-import org.hibernate.test.legacy.CustomSQLTest;
 import org.hibernate.test.legacy.FooBarTest;
 import org.hibernate.test.legacy.FumTest;
 import org.hibernate.test.legacy.IJ2Test;
@@ -122,12 +122,12 @@
 import org.hibernate.test.unconstrained.UnconstrainedTest;
 import org.hibernate.test.unidir.BackrefTest;
 import org.hibernate.test.unionsubclass.UnionSubclassTest;
+import org.hibernate.test.usercollection.UserCollectionTypeTest;
 import org.hibernate.test.util.UtilSuite;
 import org.hibernate.test.version.VersionTest;
 import org.hibernate.test.version.db.DbVersionTest;
 import org.hibernate.test.version.sybase.SybaseTimestampVersioningTest;
 import org.hibernate.test.where.WhereTest;
-import org.hibernate.test.usercollection.UserCollectionTypeTest;
 
 /**
  * @author Gavin King
@@ -276,9 +276,9 @@
 			suite.addTest( IdentifierPropertyReferencesTest.suite() );
 			suite.addTest( DeleteTransientEntityTest.suite() );
 			suite.addTest( UserCollectionTypeTest.suite() );
+			suite.addTest( KeyManyToOneSuite.suite() );
 
 			return filter( suite );
-			//return suite;
 		}
 
 		/**

Deleted: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/Card.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/Card.java	2007-01-23 12:54:10 UTC (rev 11076)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/Card.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -1,38 +0,0 @@
-//$Id: $
-package org.hibernate.test.keymanytoone;
-
-import java.io.Serializable;
-import java.util.Set;
-import java.util.HashSet;
-
-/**
- * @author Emmanuel Bernard
- */
-public class Card implements Serializable {
-	private String id;
-
-	private Set fields;
-
-	public Card(String id) {
-		this();
-		this.id = id;
-
-	}
-
-	Card() {
-		fields = new HashSet();
-	}
-
-	public String getId() {
-		return id;
-	}
-
-	public void setId(String id) {
-		this.id = id;
-	}
-
-	public void addField(Card card, Key key) {
-		fields.add(new CardField(card, key));
-	}
-}
-

Deleted: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/CardField.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/CardField.java	2007-01-23 12:54:10 UTC (rev 11076)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/CardField.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -1,19 +0,0 @@
-//$Id: $
-package org.hibernate.test.keymanytoone;
-
-import java.io.Serializable;
-
-/**
- * @author Emmanuel Bernard
- */
-public class CardField implements Serializable {
-
-	private PrimaryKey primaryKey;
-
-	CardField(Card card, Key key) {
-		this.primaryKey = new PrimaryKey(card, key);
-	}
-
-	CardField() {
-	}
-}

Deleted: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/Key.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/Key.java	2007-01-23 12:54:10 UTC (rev 11076)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/Key.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -1,18 +0,0 @@
-//$Id: $
-package org.hibernate.test.keymanytoone;
-
-import java.io.Serializable;
-
-/**
- * @author Emmanuel Bernard
- */
-public class Key implements Serializable {
-	private String id;
-
-	public Key(String id) {
-		this.id = id;
-	}
-
-	Key() {
-	}
-}

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/KeyManyToOneSuite.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/KeyManyToOneSuite.java	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/KeyManyToOneSuite.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,23 @@
+package org.hibernate.test.keymanytoone;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.hibernate.test.keymanytoone.bidir.embedded.KeyManyToOneTest;
+import org.hibernate.test.keymanytoone.bidir.component.LazyKeyManyToOneTest;
+import org.hibernate.test.keymanytoone.bidir.component.EagerKeyManyToOneTest;
+
+/**
+ * {@inheritDoc}
+ *
+ * @author Steve Ebersole
+ */
+public class KeyManyToOneSuite {
+	public static Test suite() {
+		TestSuite suite = new TestSuite( "key-many-to-one mappings" );
+		suite.addTest( KeyManyToOneTest.suite() );
+		suite.addTest( LazyKeyManyToOneTest.suite() );
+		suite.addTest( EagerKeyManyToOneTest.suite() );
+		return suite;
+	}
+}

Deleted: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/KeyManyToOneTestCase.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/KeyManyToOneTestCase.java	2007-01-23 12:54:10 UTC (rev 11076)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/KeyManyToOneTestCase.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -1,34 +0,0 @@
-//$Id: $
-package org.hibernate.test.keymanytoone;
-
-import org.hibernate.test.TestCase;
-import org.hibernate.Session;
-import org.hibernate.Transaction;
-
-/**
- * @author Emmanuel Bernard
- */
-public class KeyManyToOneTestCase extends TestCase {
-	public void testInfiniteLoop() throws Exception {
-		Card card = new Card("cardId");
-		Key key = new Key("keyId");
-		card.addField(card, key);
-		Session s = openSession();
-		Transaction tx = s.beginTransaction();
-		s.persist( key );
-		s.persist( card );
-		s.flush();
-		s.clear();
-		s.get( Card.class, card.getId() );
-		tx.rollback();
-	}
-	public KeyManyToOneTestCase(String name) {
-		super( name );
-	}
-
-	protected String[] getMappings() {
-		return new String[] {
-				"keymanytoone/card.hbm.xml"
-		};
-	}
-}

Deleted: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/PrimaryKey.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/PrimaryKey.java	2007-01-23 12:54:10 UTC (rev 11076)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/PrimaryKey.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -1,21 +0,0 @@
-//$Id: $
-package org.hibernate.test.keymanytoone;
-
-import java.io.Serializable;
-
-/**
- * @author Emmanuel Bernard
- */
-public class PrimaryKey implements Serializable {
-	private Card card;
-
-	private Key key;
-
-	public PrimaryKey(Card card, Key key) {
-		this.card = card;
-		this.key = key;
-	}
-
-	PrimaryKey() {}
-
-}

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/Customer.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/Customer.java	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/Customer.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,46 @@
+package org.hibernate.test.keymanytoone.bidir.component;
+
+import java.util.Collection;
+import java.util.ArrayList;
+
+/**
+ * {@inheritDoc}
+ *
+ * @author Steve Ebersole
+ */
+public class Customer {
+	private Long id;
+	private String name;
+	private Collection orders = new ArrayList();
+
+	public Customer() {
+	}
+
+	public Customer(String name) {
+		this.name = name;
+	}
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public Collection getOrders() {
+		return orders;
+	}
+
+	public void setOrders(Collection orders) {
+		this.orders = orders;
+	}
+}

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerKeyManyToOneTest.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,92 @@
+package org.hibernate.test.keymanytoone.bidir.component;
+
+import junit.framework.Test;
+
+import org.hibernate.Session;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+import org.hibernate.test.TestCase;
+
+/**
+ * @author Steve Ebersole
+ */
+public class EagerKeyManyToOneTest extends TestCase {
+	public EagerKeyManyToOneTest(String name) {
+		super( name );
+	}
+
+	protected String[] getMappings() {
+		return new String[] { "keymanytoone/bidir/component/EagerMapping.hbm.xml" };
+	}
+
+	public static Test suite() {
+		return new FunctionalTestClassTestSuite( EagerKeyManyToOneTest.class );
+	}
+
+	protected void configure(Configuration cfg) {
+		super.configure( cfg );
+		cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
+	}
+
+	public void testSaveCascadedToKeyManyToOne() {
+		// test cascading a save to an association with a key-many-to-one which refers to a
+		// just saved entity
+		Session s = openSession();
+		s.beginTransaction();
+		Customer cust = new Customer( "Acme, Inc." );
+		Order order = new Order( new Order.Id( cust, 1 ) );
+		cust.getOrders().add( order );
+		s.save( cust );
+		s.flush();
+		assertEquals( 2, sfi().getStatistics().getEntityInsertCount() );
+		s.delete( cust );
+		s.getTransaction().commit();
+		s.close();
+	}
+
+	public void testLoadingStrategies() {
+		Session s = openSession();
+		s.beginTransaction();
+		Customer cust = new Customer( "Acme, Inc." );
+		Order order = new Order( new Order.Id( cust, 1 ) );
+		cust.getOrders().add( order );
+		s.save( cust );
+		s.getTransaction().commit();
+		s.close();
+
+		s = openSession();
+		s.beginTransaction();
+
+// Here is an example of HHH-2277
+// essentially we have a bidirectional association where one side of the
+// association is actually part of a composite PK
+//
+// The way these are mapped causes the problem because both sides
+// are defined as eager which leads to the infinite loop; if only
+// one side is marked as eager, then all is ok...
+//		cust = ( Customer ) s.get( Customer.class, cust.getId() );
+//		assertEquals( 1, cust.getOrders().size() );
+//		s.clear();
+
+		cust = ( Customer ) s.createQuery( "from Customer" ).uniqueResult();
+		assertEquals( 1, cust.getOrders().size() );
+		s.clear();
+
+		cust = ( Customer ) s.createQuery( "from Customer c join fetch c.orders" ).uniqueResult();
+		assertEquals( 1, cust.getOrders().size() );
+		s.clear();
+
+		cust = ( Customer ) s.createQuery( "from Customer c join fetch c.orders as o join fetch o.id.customer" ).uniqueResult();
+		assertEquals( 1, cust.getOrders().size() );
+		s.clear();
+
+		cust = ( Customer ) s.createCriteria( Customer.class ).uniqueResult();
+		assertEquals( 1, cust.getOrders().size() );
+		s.clear();
+
+		s.delete( cust );
+		s.getTransaction().commit();
+		s.close();
+	}
+}

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerMapping.hbm.xml
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerMapping.hbm.xml	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/EagerMapping.hbm.xml	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,42 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<!--
+    This mapping demonstrates the use of composite ids with the
+    key-many-to-one feature.  Essentially a composite id where part
+    of the composition is a foreign-key to another entity.
+
+    Here, specifically, we map the key-many-to-one as a lazy
+    association.
+-->
+
+<hibernate-mapping package="org.hibernate.test.keymanytoone.bidir.component">
+
+    <class name="Customer" table="COMP_LAZY_KM2O_CUST">
+        <id name="id" column="ID" type="long">
+            <generator class="increment" />
+        </id>
+        <property name="name" column="NAME" type="string" />
+        <bag name="orders" inverse="true" cascade="all" lazy="false" fetch="join">
+            <key column="CUST_ID" />
+            <one-to-many class="Order" />
+        </bag>
+    </class>
+
+    <class name="Order" table="COMP_LAZY_KM2O_ORDR">
+        <composite-id name="id" class="Order$Id">
+            <key-many-to-one name="customer" class="Customer" column="CUST_ID" lazy="false"/>
+            <key-property name="number" column="ORDR_NUM" type="long" />
+        </composite-id>
+        <set name="items" table="COMP_LAZY_KM2O_ITEM">
+            <key>
+                <column name="CUST_ID"/>
+                <column name="ORDER_NUM"/>
+            </key>
+            <element type="string" column="ITEM_DESC" />
+        </set>
+    </class>
+
+</hibernate-mapping>

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/LazyKeyManyToOneTest.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/LazyKeyManyToOneTest.java	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/LazyKeyManyToOneTest.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,77 @@
+package org.hibernate.test.keymanytoone.bidir.component;
+
+import junit.framework.Test;
+
+import org.hibernate.test.TestCase;
+import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.Session;
+
+/**
+ * @author Steve Ebersole
+ */
+public class LazyKeyManyToOneTest extends TestCase {
+	public LazyKeyManyToOneTest(String name) {
+		super( name );
+	}
+
+	protected String[] getMappings() {
+		return new String[] { "keymanytoone/bidir/component/LazyMapping.hbm.xml" };
+	}
+
+	public static Test suite() {
+		return new FunctionalTestClassTestSuite( LazyKeyManyToOneTest.class );
+	}
+
+	protected void configure(Configuration cfg) {
+		super.configure( cfg );
+		cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
+	}
+
+	public void testSaveCascadedToKeyManyToOne() {
+		// test cascading a save to an association with a key-many-to-one which refers to a
+		// just saved entity
+		Session s = openSession();
+		s.beginTransaction();
+		Customer cust = new Customer( "Acme, Inc." );
+		Order order = new Order( new Order.Id( cust, 1 ) );
+		cust.getOrders().add( order );
+		s.save( cust );
+		s.flush();
+		assertEquals( 2, sfi().getStatistics().getEntityInsertCount() );
+		s.delete( cust );
+		s.getTransaction().commit();
+		s.close();
+	}
+
+	public void testLoadingStrategies() {
+		Session s = openSession();
+		s.beginTransaction();
+		Customer cust = new Customer( "Acme, Inc." );
+		Order order = new Order( new Order.Id( cust, 1 ) );
+		cust.getOrders().add( order );
+		s.save( cust );
+		s.getTransaction().commit();
+		s.close();
+
+		s = openSession();
+		s.beginTransaction();
+
+		cust = ( Customer ) s.get( Customer.class, cust.getId() );
+		assertEquals( 1, cust.getOrders().size() );
+		s.clear();
+
+		cust = ( Customer ) s.createQuery( "from Customer" ).uniqueResult();
+		assertEquals( 1, cust.getOrders().size() );
+		s.clear();
+
+		cust = ( Customer ) s.createQuery( "from Customer c join fetch c.orders" ).uniqueResult();
+		assertEquals( 1, cust.getOrders().size() );
+		s.clear();
+
+		s.delete( cust );
+		s.getTransaction().commit();
+		s.close();
+	}
+}

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/LazyMapping.hbm.xml
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/LazyMapping.hbm.xml	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/LazyMapping.hbm.xml	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,42 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<!--
+    This mapping demonstrates the use of composite ids with the
+    key-many-to-one feature.  Essentially a composite id where part
+    of the composition is a foreign-key to another entity.
+
+    Here, specifically, we map the key-many-to-one as a lazy
+    association.
+-->
+
+<hibernate-mapping package="org.hibernate.test.keymanytoone.bidir.component">
+
+    <class name="Customer" table="COMP_LAZY_KM2O_CUST">
+        <id name="id" column="ID" type="long">
+            <generator class="increment" />
+        </id>
+        <property name="name" column="NAME" type="string" />
+        <bag name="orders" inverse="true" cascade="all">
+            <key column="CUST_ID" />
+            <one-to-many class="Order" />
+        </bag>
+    </class>
+
+    <class name="Order" table="COMP_LAZY_KM2O_ORDR">
+        <composite-id name="id" class="Order$Id">
+            <key-many-to-one name="customer" class="Customer" column="CUST_ID" lazy="proxy"/>
+            <key-property name="number" column="ORDR_NUM" type="long" />
+        </composite-id>
+        <set name="items" table="COMP_LAZY_KM2O_ITEM">
+            <key>
+                <column name="CUST_ID"/>
+                <column name="ORDER_NUM"/>
+            </key>
+            <element type="string" column="ITEM_DESC" />
+        </set>
+    </class>
+
+</hibernate-mapping>

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/Order.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/Order.java	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/component/Order.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,86 @@
+package org.hibernate.test.keymanytoone.bidir.component;
+
+import java.io.Serializable;
+import java.util.Set;
+import java.util.HashSet;
+
+/**
+ * {@inheritDoc}
+ *
+ * @author Steve Ebersole
+ */
+public class Order {
+	private Id id;
+	private Set items = new HashSet();
+
+	public Order() {
+	}
+
+	public Order(Id id) {
+		this.id = id;
+	}
+
+	public Id getId() {
+		return id;
+	}
+
+	public void setId(Id id) {
+		this.id = id;
+	}
+
+	public Set getItems() {
+		return items;
+	}
+
+	public void setItems(Set items) {
+		this.items = items;
+	}
+
+	public static class Id implements Serializable {
+		private Customer customer;
+		private long number;
+
+		public Id() {
+		}
+
+		public Id(Customer customer, long number) {
+			this.customer = customer;
+			this.number = number;
+		}
+
+		public Customer getCustomer() {
+			return customer;
+		}
+
+		public void setCustomer(Customer customer) {
+			this.customer = customer;
+		}
+
+		public long getNumber() {
+			return number;
+		}
+
+		public void setNumber(long number) {
+			this.number = number;
+		}
+
+		public boolean equals(Object o) {
+			if ( this == o ) {
+				return true;
+			}
+			if ( o == null || getClass() != o.getClass() ) {
+				return false;
+			}
+
+			Id id = ( Id ) o;
+			return number == id.number && customer.equals( id.customer );
+		}
+
+		public int hashCode() {
+			int result;
+			result = customer.hashCode();
+			result = 31 * result + ( int ) ( number ^ ( number >>> 32 ) );
+			return result;
+		}
+	}
+}

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Customer.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Customer.java	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Customer.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,46 @@
+package org.hibernate.test.keymanytoone.bidir.embedded;
+
+import java.util.Collection;
+import java.util.ArrayList;
+
+/**
+ * {@inheritDoc}
+ *
+ * @author Steve Ebersole
+ */
+public class Customer {
+	private Long id;
+	private String name;
+	private Collection orders = new ArrayList();
+
+	public Customer() {
+	}
+
+	public Customer(String name) {
+		this.name = name;
+	}
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public Collection getOrders() {
+		return orders;
+	}
+
+	public void setOrders(Collection orders) {
+		this.orders = orders;
+	}
+}

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,77 @@
+package org.hibernate.test.keymanytoone.bidir.embedded;
+
+import junit.framework.Test;
+
+import org.hibernate.test.TestCase;
+import org.hibernate.Session;
+import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+
+/**
+ * @author Steve Ebersole
+ */
+public class KeyManyToOneTest extends TestCase {
+	public KeyManyToOneTest(String name) {
+		super( name );
+	}
+
+	protected String[] getMappings() {
+		return new String[] { "keymanytoone/bidir/embedded/Mapping.hbm.xml" };
+	}
+
+	public static Test suite() {
+		return new FunctionalTestClassTestSuite( KeyManyToOneTest.class );
+	}
+
+	protected void configure(Configuration cfg) {
+		super.configure( cfg );
+		cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
+	}
+
+	public void testSaveCascadedToKeyManyToOne() {
+		// test cascading a save to an association with a key-many-to-one which refers to a
+		// just saved entity
+		Session s = openSession();
+		s.beginTransaction();
+		Customer cust = new Customer( "Acme, Inc." );
+		Order order = new Order( cust, 1 );
+		cust.getOrders().add( order );
+		s.save( cust );
+		s.flush();
+		assertEquals( 2, sfi().getStatistics().getEntityInsertCount() );
+		s.delete( cust );
+		s.getTransaction().commit();
+		s.close();
+	}
+
+	public void testLoadingStrategies() {
+		Session s = openSession();
+		s.beginTransaction();
+		Customer cust = new Customer( "Acme, Inc." );
+		Order order = new Order( cust, 1 );
+		cust.getOrders().add( order );
+		s.save( cust );
+		s.getTransaction().commit();
+		s.close();
+
+		s = openSession();
+		s.beginTransaction();
+
+		cust = ( Customer ) s.get( Customer.class, cust.getId() );
+		assertEquals( 1, cust.getOrders().size() );
+		s.clear();
+
+		cust = ( Customer ) s.createQuery( "from Customer" ).uniqueResult();
+		assertEquals( 1, cust.getOrders().size() );
+		s.clear();
+
+		cust = ( Customer ) s.createQuery( "from Customer c join fetch c.orders" ).uniqueResult();
+		assertEquals( 1, cust.getOrders().size() );
+		s.clear();
+
+		s.delete( cust );
+		s.getTransaction().commit();
+		s.close();
+	}
+}

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Mapping.hbm.xml
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Mapping.hbm.xml	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Mapping.hbm.xml	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,39 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<!--
+    This mapping demonstrates the use of composite ids with the
+    key-many-to-one feature where the composite-id is an embedded form.
+    Essentially a composite id where part of the composition is a
+    foreign-key to another entity.
+-->
+
+<hibernate-mapping package="org.hibernate.test.keymanytoone.bidir.embedded">
+
+    <class name="Customer" table="EMBD_KM2O_CUST">
+        <id name="id" column="ID" type="long">
+            <generator class="increment" />
+        </id>
+        <property name="name" column="NAME" type="string" />
+        <bag name="orders" inverse="true" cascade="all">
+            <key column="CUST_ID" />
+            <one-to-many class="Order" />
+        </bag>
+    </class>
+
+    <class name="Order" table="EMBD_KM2O_ORDR">
+        <composite-id mapped="false">
+            <key-many-to-one name="customer" class="Customer" column="CUST_ID" lazy="false"/>
+            <key-property name="number" column="ORDR_NUM" type="long" />
+        </composite-id>
+        <set name="items" table="EMBD_KM2O_ITEM">
+            <key>
+                <column name="CUST_ID"/>
+                <column name="ORDER_NUM"/>
+            </key>
+            <element type="string" column="ITEM_DESC" />
+        </set>
+    </class>
+</hibernate-mapping>
\ No newline at end of file

Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Order.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Order.java	                        (rev 0)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/bidir/embedded/Order.java	2007-01-23 12:54:42 UTC (rev 11077)
@@ -0,0 +1,48 @@
+package org.hibernate.test.keymanytoone.bidir.embedded;
+
+import java.util.Set;
+import java.util.HashSet;
+import java.io.Serializable;
+
+/**
+ * {@inheritDoc}
+ *
+ * @author Steve Ebersole
+ */
+public class Order implements Serializable {
+	private Customer customer;
+	private long number;
+	private Set items = new HashSet();
+
+	public Order() {
+	}
+
+	public Order(Customer customer, long number) {
+		this.customer = customer;
+		this.number = number;
+	}
+
+	public Customer getCustomer() {
+		return customer;
+	}
+
+	public void setCustomer(Customer customer) {
+		this.customer = customer;
+	}
+
+	public long getNumber() {
+		return number;
+	}
+
+	public void setNumber(long number) {
+		this.number = number;
+	}
+
+	public Set getItems() {
+		return items;
+	}
+
+	public void setItems(Set items) {
+		this.items = items;
+	}
+}

Deleted: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/card.hbm.xml
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/card.hbm.xml	2007-01-23 12:54:10 UTC (rev 11076)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/keymanytoone/card.hbm.xml	2007-01-23 12:54:42 UTC (rev 11077)
@@ -1,48 +0,0 @@
-<?xml version="1.0"?>
-<!DOCTYPE hibernate-mapping PUBLIC 
-	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
-	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
-
-<!-- 
-
-  This mapping demonstrates 
-
-     (1) use of lazy properties - this feature requires buildtime 
-         bytecode instrumentation; we don't think this is a very
-         necessary feature, but provide it for completeleness; if
-         Hibernate encounters uninstrumented classes, lazy property
-         fetching will be silently disabled, to enable testing
-     
-     (2) use of a formula to define a "derived property"
-     
--->
-
-<hibernate-mapping 
-	package="org.hibernate.test.keymanytoone"
-	default-access="field">
- 	
-	<class name="Card">
-    	<id name="id"/>
-    	<set name="fields" cascade="all" fetch="join" lazy="false">
-			<key>
-				<column name="card_id"/>
-			</key>
-			<one-to-many class="CardField"/>
-		</set>
-	</class>
-	<class name="Key" table="tbl_key">
-    	<id name="id"/>
-	</class>
-	<class name="CardField">
-    	<composite-id name="primaryKey">
-			<key-many-to-one name="card" class="Card" lazy="false">
-				<column name="card_id"/>
-			</key-many-to-one>
-			<key-many-to-one name="key" class="Key" lazy="false">
-				<column name="key_id"/>
-			</key-many-to-one>
-		</composite-id>
-	</class>
- 	
-
-</hibernate-mapping>




More information about the hibernate-commits mailing list