Author: epbernard
Date: 2007-01-08 07:10:39 -0500 (Mon, 08 Jan 2007)
New Revision: 11021
Added:
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Competition.java
Modified:
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Competitor.java
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/MergeTest.java
Log:
more tests (related to HHH-2292
Added:
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Competition.java
===================================================================
---
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Competition.java 2007-01-08
12:00:02 UTC (rev 11020)
+++
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Competition.java 2007-01-08
12:10:39 UTC (rev 11021)
@@ -0,0 +1,47 @@
+//$Id: $
+package org.hibernate.ejb.test.emops;
+
+import java.util.Collection;
+import java.util.ArrayList;
+import java.util.List;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.ManyToMany;
+import javax.persistence.CascadeType;
+import javax.persistence.FetchType;
+import javax.persistence.JoinTable;
+import javax.persistence.JoinColumn;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+public class Competition {
+ @Id
+ @GeneratedValue
+ private Integer id;
+
+ @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE },
+ fetch = FetchType.LAZY)
+ @JoinTable(name="competition_competitor")
+ @JoinColumn
+ private List<Competitor> competitors = new ArrayList<Competitor>();
+
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public List<Competitor> getCompetitors() {
+ return competitors;
+ }
+
+ public void setCompetitors(List<Competitor> competitors) {
+ this.competitors = competitors;
+ }
+}
Modified:
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Competitor.java
===================================================================
---
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Competitor.java 2007-01-08
12:00:02 UTC (rev 11020)
+++
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/Competitor.java 2007-01-08
12:10:39 UTC (rev 11021)
@@ -11,7 +11,16 @@
@Entity
public class Competitor {
@Id @GeneratedValue public Integer id;
+ private String name;
+
+ public Competitor() {
+ }
+
+ public Competitor(String name) {
+ this.name = name;
+ }
+
public Integer getId() {
return id;
}
@@ -19,4 +28,12 @@
public void setId(Integer id) {
this.id = id;
}
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
}
Modified:
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/MergeTest.java
===================================================================
---
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/MergeTest.java 2007-01-08
12:00:02 UTC (rev 11020)
+++
branches/Branch_3_2/HibernateExt/ejb/src/test/org/hibernate/ejb/test/emops/MergeTest.java 2007-01-08
12:10:39 UTC (rev 11021)
@@ -1,6 +1,8 @@
//$Id: $
package org.hibernate.ejb.test.emops;
+import java.util.List;
+import java.util.ArrayList;
import javax.persistence.EntityManager;
import org.hibernate.ejb.test.TestCase;
@@ -11,9 +13,9 @@
public class MergeTest extends TestCase {
public void testMergeWithIndexColumn() {
Race race = new Race();
+ race.competitors.add( new Competitor("Name") );
race.competitors.add( new Competitor() );
race.competitors.add( new Competitor() );
- race.competitors.add( new Competitor() );
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
em.persist( race );
@@ -21,15 +23,67 @@
em.clear();
race.competitors.add( new Competitor() );
race.competitors.remove( 2 );
+ race.competitors.remove( 1 );
+ race.competitors.get( 0 ).setName( "Name2" );
race = em.merge( race );
em.flush();
em.clear();
race = em.find( Race.class, race.id );
- assertEquals( 3, race.competitors.size() );
+ assertEquals( 2, race.competitors.size() );
+ assertEquals( "Name2", race.competitors.get(0).getName() );
em.getTransaction().rollback();
em.close();
}
+ public void testMergeManyToMany() {
+ Competition competition = new Competition();
+ competition.getCompetitors().add( new Competitor("Name") );
+ competition.getCompetitors().add( new Competitor() );
+ competition.getCompetitors().add( new Competitor() );
+ EntityManager em = factory.createEntityManager();
+ em.getTransaction().begin();
+ em.persist( competition );
+ em.flush();
+ em.clear();
+ competition.getCompetitors().add( new Competitor() );
+ competition.getCompetitors().remove( 2 );
+ competition.getCompetitors().remove( 1 );
+ competition.getCompetitors().get(0).setName( "Name2" );
+ competition = em.merge( competition );
+ em.flush();
+ em.clear();
+ competition = em.find( Competition.class, competition.getId() );
+ assertEquals( 2, competition.getCompetitors().size() );
+ assertEquals( "Name2", competition.getCompetitors().get(0).getName() );
+ em.getTransaction().rollback();
+ em.close();
+ }
+
+ public void testMergeManyToManyWithDeference() {
+ Competition competition = new Competition();
+ competition.getCompetitors().add( new Competitor("Name") );
+ competition.getCompetitors().add( new Competitor() );
+ competition.getCompetitors().add( new Competitor() );
+ EntityManager em = factory.createEntityManager();
+ em.getTransaction().begin();
+ em.persist( competition );
+ em.flush();
+ em.clear();
+ List<Competitor> newComp = new ArrayList<Competitor>();
+ newComp.add( competition.getCompetitors().get(0) );
+ newComp.add( new Competitor() );
+ newComp.get(0).setName( "Name2" );
+ competition.setCompetitors( newComp );
+ competition = em.merge( competition );
+ em.flush();
+ em.clear();
+ competition = em.find( Competition.class, competition.getId() );
+ assertEquals( 2, competition.getCompetitors().size() );
+ assertEquals( "Name2", competition.getCompetitors().get(0).getName() );
+ em.getTransaction().rollback();
+ em.close();
+ }
+
public void testRemoveAndMerge() {
Race race = new Race();
EntityManager em = factory.createEntityManager();
@@ -89,7 +143,8 @@
public Class[] getAnnotatedClasses() {
return new Class[] {
Race.class,
- Competitor.class
+ Competitor.class,
+ Competition.class
};
}
}
Show replies by date