/* * Hibernate, Relational Persistence for Idiomatic Java * * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. */ package org.hibernate.test.collection.bag; import java.util.ArrayList; import org.hibernate.Session; import org.hibernate.collection.internal.PersistentBag; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** * Tests related to operations on a PersistentBag. * * @author Steve Ebersole */ public class PersistentBagTest extends BaseCoreFunctionalTestCase { @Override public String[] getMappings() { return new String[] { "collection/bag/Mappings.hbm.xml" }; } @Test public void testWriteMethodDirtying() { BagOwner parent = new BagOwner( "root" ); BagOwner child = new BagOwner( "c1" ); parent.getChildren().add( child ); child.setParent( parent ); BagOwner otherChild = new BagOwner( "c2" ); Session session = openSession(); session.beginTransaction(); session.save( parent ); session.flush(); // at this point, the list on parent has now been replaced with a PersistentBag... PersistentBag children = ( PersistentBag ) parent.getChildren(); assertFalse( children.remove( otherChild ) ); assertFalse( children.isDirty() ); ArrayList otherCollection = new ArrayList(); otherCollection.add( child ); assertFalse( children.retainAll( otherCollection ) ); assertFalse( children.isDirty() ); otherCollection = new ArrayList(); otherCollection.add( otherChild ); assertFalse( children.removeAll( otherCollection ) ); assertFalse( children.isDirty() ); children.clear(); session.delete( child ); assertTrue( children.isDirty() ); session.flush(); children.clear(); assertFalse( children.isDirty() ); session.delete( parent ); session.getTransaction().commit(); session.close(); } @Test public void testMergePersistentEntityWithNewOneToManyElements() { Order order = new Order(); Session s = openSession(); s.getTransaction().begin(); s.persist( order ); s.getTransaction().commit(); s.close(); s = openSession(); s.getTransaction().begin(); order = s.get( Order.class, order.getId() ); Item item1 = new Item(); item1.setName( "i1" ); Item item2 = new Item(); item2.setName( "i2" ); order.addItem( item1 ); order.addItem( item2 ); order = (Order) s.merge( order ); //s.flush(); s.getTransaction().commit(); s.close(); s = openSession(); s.getTransaction().begin(); order = s.get( Order.class, order.getId() ); assertEquals( 2, order.getItems().size() ); s.delete( order ); s.getTransaction().commit(); s.close(); } }