git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17820 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Hardy Ferentschik 2009-10-22 16:45:01 +00:00
parent 1a4b12967a
commit d14de01220
1 changed files with 36 additions and 20 deletions

View File

@ -1,8 +1,8 @@
//$Id$ //$Id$
package org.hibernate.ejb.test.emops; package org.hibernate.ejb.test.emops;
import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import org.hibernate.ejb.test.TestCase; import org.hibernate.ejb.test.TestCase;
@ -13,7 +13,7 @@ import org.hibernate.ejb.test.TestCase;
public class MergeTest extends TestCase { public class MergeTest extends TestCase {
public void testMergeWithIndexColumn() { public void testMergeWithIndexColumn() {
Race race = new Race(); Race race = new Race();
race.competitors.add( new Competitor("Name") ); race.competitors.add( new Competitor( "Name" ) );
race.competitors.add( new Competitor() ); race.competitors.add( new Competitor() );
race.competitors.add( new Competitor() ); race.competitors.add( new Competitor() );
EntityManager em = getOrCreateEntityManager(); EntityManager em = getOrCreateEntityManager();
@ -30,14 +30,14 @@ public class MergeTest extends TestCase {
em.clear(); em.clear();
race = em.find( Race.class, race.id ); race = em.find( Race.class, race.id );
assertEquals( 2, race.competitors.size() ); assertEquals( 2, race.competitors.size() );
assertEquals( "Name2", race.competitors.get(0).getName() ); assertEquals( "Name2", race.competitors.get( 0 ).getName() );
em.getTransaction().rollback(); em.getTransaction().rollback();
em.close(); em.close();
} }
public void testMergeManyToMany() { public void testMergeManyToMany() {
Competition competition = new Competition(); Competition competition = new Competition();
competition.getCompetitors().add( new Competitor("Name") ); competition.getCompetitors().add( new Competitor( "Name" ) );
competition.getCompetitors().add( new Competitor() ); competition.getCompetitors().add( new Competitor() );
competition.getCompetitors().add( new Competitor() ); competition.getCompetitors().add( new Competitor() );
EntityManager em = getOrCreateEntityManager(); EntityManager em = getOrCreateEntityManager();
@ -48,20 +48,28 @@ public class MergeTest extends TestCase {
competition.getCompetitors().add( new Competitor() ); competition.getCompetitors().add( new Competitor() );
competition.getCompetitors().remove( 2 ); competition.getCompetitors().remove( 2 );
competition.getCompetitors().remove( 1 ); competition.getCompetitors().remove( 1 );
competition.getCompetitors().get(0).setName( "Name2" ); competition.getCompetitors().get( 0 ).setName( "Name2" );
competition = em.merge( competition ); competition = em.merge( competition );
em.flush(); em.flush();
em.clear(); em.clear();
competition = em.find( Competition.class, competition.getId() ); competition = em.find( Competition.class, competition.getId() );
assertEquals( 2, competition.getCompetitors().size() ); assertEquals( 2, competition.getCompetitors().size() );
assertEquals( "Name2", competition.getCompetitors().get(0).getName() ); // we cannot assume that the order in the list is maintained - HHH-4516
String changedCompetitorName;
if ( competition.getCompetitors().get( 0 ).getName() != null ) {
changedCompetitorName = competition.getCompetitors().get( 0 ).getName();
}
else {
changedCompetitorName = competition.getCompetitors().get( 1 ).getName();
}
assertEquals( "Name2", changedCompetitorName );
em.getTransaction().rollback(); em.getTransaction().rollback();
em.close(); em.close();
} }
public void testMergeManyToManyWithDeference() { public void testMergeManyToManyWithDeference() {
Competition competition = new Competition(); Competition competition = new Competition();
competition.getCompetitors().add( new Competitor("Name") ); competition.getCompetitors().add( new Competitor( "Name" ) );
competition.getCompetitors().add( new Competitor() ); competition.getCompetitors().add( new Competitor() );
competition.getCompetitors().add( new Competitor() ); competition.getCompetitors().add( new Competitor() );
EntityManager em = getOrCreateEntityManager(); EntityManager em = getOrCreateEntityManager();
@ -70,16 +78,24 @@ public class MergeTest extends TestCase {
em.flush(); em.flush();
em.clear(); em.clear();
List<Competitor> newComp = new ArrayList<Competitor>(); List<Competitor> newComp = new ArrayList<Competitor>();
newComp.add( competition.getCompetitors().get(0) ); newComp.add( competition.getCompetitors().get( 0 ) );
newComp.add( new Competitor() ); newComp.add( new Competitor() );
newComp.get(0).setName( "Name2" ); newComp.get( 0 ).setName( "Name2" );
competition.setCompetitors( newComp ); competition.setCompetitors( newComp );
competition = em.merge( competition ); competition = em.merge( competition );
em.flush(); em.flush();
em.clear(); em.clear();
competition = em.find( Competition.class, competition.getId() ); competition = em.find( Competition.class, competition.getId() );
assertEquals( 2, competition.getCompetitors().size() ); assertEquals( 2, competition.getCompetitors().size() );
assertEquals( "Name2", competition.getCompetitors().get(0).getName() ); // we cannot assume that the order in the list is maintained - HHH-4516
String changedCompetitorName;
if ( competition.getCompetitors().get( 0 ).getName() != null ) {
changedCompetitorName = competition.getCompetitors().get( 0 ).getName();
}
else {
changedCompetitorName = competition.getCompetitors().get( 1 ).getName();
}
assertEquals( "Name2", changedCompetitorName );
em.getTransaction().rollback(); em.getTransaction().rollback();
em.close(); em.close();
} }
@ -91,18 +107,18 @@ public class MergeTest extends TestCase {
em.persist( race ); em.persist( race );
em.flush(); em.flush();
em.clear(); em.clear();
race = em.find(Race.class, race.id ); race = em.find( Race.class, race.id );
em.remove(race); em.remove( race );
try { try {
race = em.merge( race ); race = em.merge( race );
em.flush(); em.flush();
fail("Should raise an IllegalArgumentException"); fail( "Should raise an IllegalArgumentException" );
} }
catch (IllegalArgumentException e) { catch ( IllegalArgumentException e ) {
//all good //all good
} }
catch (Exception e) { catch ( Exception e ) {
fail("Should raise an IllegalArgumentException"); fail( "Should raise an IllegalArgumentException" );
} }
em.getTransaction().rollback(); em.getTransaction().rollback();
em.close(); em.close();
@ -122,7 +138,7 @@ public class MergeTest extends TestCase {
em = getOrCreateEntityManager(); em = getOrCreateEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
Race race2 = em.find(Race.class, race.id ); Race race2 = em.find( Race.class, race.id );
race2.name = "Mans"; race2.name = "Mans";
race = em.merge( race ); race = em.merge( race );
@ -132,7 +148,7 @@ public class MergeTest extends TestCase {
em = getOrCreateEntityManager(); em = getOrCreateEntityManager();
em.getTransaction().begin(); em.getTransaction().begin();
race2 = em.find(Race.class, race.id ); race2 = em.find( Race.class, race.id );
assertEquals( "Last commit win in merge", "Magnicourt", race2.name ); assertEquals( "Last commit win in merge", "Magnicourt", race2.name );
em.remove( race2 ); em.remove( race2 );
@ -149,11 +165,11 @@ public class MergeTest extends TestCase {
em.clear(); em.clear();
roman = em.find( Empire.class, roman.getId() ); roman = em.find( Empire.class, roman.getId() );
Colony gaule = new Colony(); Colony gaule = new Colony();
roman.getColonies().add(gaule); roman.getColonies().add( gaule );
em.merge( roman ); em.merge( roman );
em.flush(); em.flush();
em.clear(); em.clear();
roman = em.find(Empire.class, roman.getId() ); roman = em.find( Empire.class, roman.getId() );
assertEquals( 1, roman.getColonies().size() ); assertEquals( 1, roman.getColonies().size() );
em.getTransaction().rollback(); em.getTransaction().rollback();
em.close(); em.close();