Refine search
Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User userA = (User)session.get(User.class, 1101); transaction.commit(); session.close(); // Since session is closed, userA is detached. session = sessionFactory.openSession(); transaction = session.beginTransaction(); User userB = (User)session.get(User.class, 1101); //Now here, userB represents the same persistent row as userA. //When an attempt to reattach userA occurs, an exception is thrown session.update(userA); transaction.commit(); session.close();
public void testNothinToUpdate() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple( Long.valueOf(10) ); simple.setName("Simple 1"); s.save( simple ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update( simple ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update( simple ); s.delete(simple); t.commit(); s.close(); }
@Test public void testVeto() throws Exception { Session s = openSession(); s.beginTransaction(); Vetoer v = new Vetoer(); s.save(v); s.save(v); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); s.update( v ); s.update( v ); s.delete( v ); s.delete( v ); s.getTransaction().commit(); s.close(); }
@Test public void testNothinToUpdate() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple( Long.valueOf(10) ); simple.setName("Simple 1"); s.save( simple ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update( simple ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update( simple ); s.delete(simple); t.commit(); s.close(); }
public void testBroken() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Broken b = new Fixed(); b.setId( Long.valueOf( 123 )); b.setOtherId("foobar"); s.save(b); s.flush(); b.setTimestamp( new Date() ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update(b); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); b = (Broken) s.load( Broken.class, b ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.delete(b); t.commit(); s.close(); }
@Test public void testDeleteUpdatedTransient() throws Exception { Fee fee = new Fee(); Fee fee2 = new Fee(); fee2.setAnotherFee(fee); Session s = openSession(); Transaction tx = s.beginTransaction(); s.save(fee); s.save(fee2); s.flush(); fee.setCount(123); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); s.update(fee); //fee2.setAnotherFee(null); s.update(fee2); s.delete(fee); s.delete(fee2); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); assertTrue( s.createQuery( "from Fee fee" ).list().size()==0 ); tx.commit(); s.close(); }
@Test public void testBroken() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Broken b = new Fixed(); b.setId( new Long(123)); b.setOtherId("foobar"); s.save(b); s.flush(); b.setTimestamp( new Date() ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update(b); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); b = (Broken) s.load( Broken.class, b ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.delete(b); t.commit(); s.close(); }
@Test public void testUpdateAfterEvict() { Session s = openSession(); s.beginTransaction(); Parent p = new Parent( "p" ); s.save( p ); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); p = ( Parent ) s.load( Parent.class, "p" ); // evict... s.evict( p ); // now try to reattach... s.update( p ); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); s.delete( p ); s.getTransaction().commit(); s.close(); }
@Test public void testUpdateAfterClear() { Session s = openSession(); s.beginTransaction(); Parent p = new Parent( "p" ); s.save( p ); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); p = ( Parent ) s.load( Parent.class, "p" ); // clear... s.clear(); // now try to reattach... s.update( p ); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); s.delete( p ); s.getTransaction().commit(); s.close(); }
@Test public void testNoUpdateManyToOne() throws Exception { Session s = openSession(); s.beginTransaction(); W w1 = new W(); W w2 = new W(); Z z = new Z(); z.setW(w1); s.save(z); s.flush(); z.setW(w2); s.getTransaction().commit(); s.close(); s = openSession(); s.beginTransaction(); s.update(z); s.flush(); s.delete(z); for ( Object entity : s.createQuery( "from W" ).list() ) { s.delete( entity ); } s.getTransaction().commit(); s.close(); }
@Test public void testMultiPathUpdateModifiedDetached() throws Exception { // persist a simple A in the database Session s = openSession(); s.beginTransaction(); A a = new A(); a.setData( "Anna" ); s.save( a ); s.getTransaction().commit(); s.close(); // modify detached entity modifyEntity( a ); s = openSession(); s.beginTransaction(); s.update( a ); s.getTransaction().commit(); s.close(); verifyModifications( a.getId() ); }
public void testClearMap() { Session s = openSession(); s.beginTransaction(); s.clear(); s.beginTransaction(); s.update( user ); Query q = s.createQuery( "DELETE FROM " + UserData.class.getName() + " d WHERE d.user = :user" ); q.setParameter( "user", user );
@Test public void testUpdate() { // prepare some test data... Session session = openSession(); session.beginTransaction(); User user = new User(); user.setUserName( "steve" ); user.setEmail( "steve@hibernate.org" ); user.setPassword( "brewhaha" ); session.save( user ); session.getTransaction().commit(); session.close(); // 'user' is now a detached entity, so lets change a property and reattch... user.setPassword( "homebrew" ); session = openSession(); session.beginTransaction(); session.update( user ); session.getTransaction().commit(); session.close(); // clean up session = openSession(); session.beginTransaction(); session.delete( user ); session.getTransaction().commit(); session.close(); }
@Test public void testUpdate() { // prepare some test data... Session session = openSession(); session.beginTransaction(); User user = new User(); user.setUserName( "steve" ); user.setEmail( "steve@hibernate.org" ); user.setPassword( "brewhaha" ); session.save( user ); session.getTransaction().commit(); session.close(); // 'user' is now a detached entity, so lets change a property and reattch... user.setPassword( "homebrew" ); session = openSession(); session.beginTransaction(); session.update( user ); session.getTransaction().commit(); session.close(); // clean up session = openSession(); session.beginTransaction(); session.delete( user ); session.getTransaction().commit(); session.close(); }
public void testEmbeddableWithNullables() { Session s = openSession(); s.beginTransaction(); Query q = new Query( new Location( null, Location.Type.COMMUNE ) ); s.save( q ); s.clear(); s.beginTransaction(); q.getIncludedLocations().add( new Location( null, Location.Type.COUNTY ) ); s.update( q ); s.getTransaction().commit(); s.clear(); s.beginTransaction(); q = (Query) s.get( Query.class, q.getId() ); itr.next(); itr.remove(); s.update( q ); s.getTransaction().commit(); s.clear();
@Test public void testUpdateOrder() throws Exception { Session s = openSession(); s.beginTransaction(); Fee fee1 = new Fee(); s.save(fee1); s.beginTransaction(); fee1.setCount(10); fee2.setCount(20); fee3.setCount(30); s.update(fee1); s.update(fee2); s.update(fee3); s.flush(); s.delete(fee1); s.beginTransaction(); assertTrue( s.createQuery( "from Fee fee" ).list().size()==0 ); s.getTransaction().commit();
@Test public void testUpdateNullOptionalJoinToNonNull() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); t = s.beginTransaction(); List things = s.createQuery("from Thing").list(); assertEquals(1, things.size()); s.update( thing ); t.commit(); s.close(); t = s.beginTransaction(); things = s.createQuery("from Thing").list(); assertEquals(1, things.size());
public void testCollectionReplaceOnUpdate() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Category c = new Category(); List list = new ArrayList(); t = s.beginTransaction(); s.update(c); t.commit(); s.close(); t = s.beginTransaction(); c = (Category) s.load( Category.class, new Long( c.getId() ), LockMode.UPGRADE ); List list2 = c.getSubcategories(); s = openSession(); t = s.beginTransaction(); s.update(c); t.commit(); s.close();
@Test public void testUpdateFailureWithExceptionChecking() { Session s = openSession(); s.beginTransaction(); ExceptionCheckingEntity e = new ExceptionCheckingEntity(); e.setId( Long.valueOf( 1 ) ); e.setName( "dummy" ); s.update( e ); try { s.flush(); fail( "expection flush failure!" ); } catch( Exception ex ) { // these should specifically be JDBCExceptions... } s.clear(); s.getTransaction().rollback(); s.close(); }
@Test public void testUpdateFailureWithParamChecking() { Session s = openSession(); s.beginTransaction(); ParamCheckingEntity e = new ParamCheckingEntity(); e.setId( Long.valueOf( 1 ) ); e.setName( "dummy" ); s.update( e ); try { s.flush(); fail( "expection flush failure!" ); } catch( Exception ex ) { // these should specifically be HibernateExceptions... } s.clear(); s.getTransaction().rollback(); s.close(); }