/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2006-2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.version;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Max Rydahl Andersen
*/
public class VersionTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "version/PersonThing.hbm.xml" };
}
@Test
public void testVersionShortCircuitFlush() {
Session s = openSession();
Transaction t = s.beginTransaction();
Person gavin = new Person("Gavin");
new Thing("Passport", gavin);
s.persist(gavin);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Thing passp = (Thing) s.get(Thing.class, "Passport");
passp.setLongDescription("blah blah blah");
s.createQuery("from Person").list();
s.createQuery("from Person").list();
s.createQuery("from Person").list();
t.commit();
s.close();
assertEquals( passp.getVersion(), 1 );
s = openSession();
t = s.beginTransaction();
s.createQuery("delete from Thing").executeUpdate();
s.createQuery("delete from Person").executeUpdate();
t.commit();
s.close();
}
@Test
public void testCollectionVersion() {
Session s = openSession();
Transaction t = s.beginTransaction();
Person gavin = new Person("Gavin");
new Thing("Passport", gavin);
s.persist(gavin);
t.commit();
s.close();
assertEquals(0, gavin.getVersion());
s = openSession();
t = s.beginTransaction();
gavin = (Person) s.createCriteria(Person.class).uniqueResult();
new Thing("Laptop", gavin);
t.commit();
s.close();
assertEquals(1, gavin.getVersion());
assertFalse( Hibernate.isInitialized( gavin.getThings() ) );
s = openSession();
t = s.beginTransaction();
gavin = (Person) s.createCriteria(Person.class).uniqueResult();
gavin.getThings().clear();
t.commit();
s.close();
assertEquals(2, gavin.getVersion());
assertTrue( Hibernate.isInitialized( gavin.getThings() ) );
s = openSession();
t = s.beginTransaction();
s.delete(gavin);
t.commit();
s.close();
}
@Test
public void testCollectionNoVersion() {
Session s = openSession();
Transaction t = s.beginTransaction();
Person gavin = new Person("Gavin");
new Task("Code", gavin);
s.persist(gavin);
t.commit();
s.close();
assertEquals(0, gavin.getVersion());
s = openSession();
t = s.beginTransaction();
gavin = (Person) s.createCriteria(Person.class).uniqueResult();
new Task("Document", gavin);
t.commit();
s.close();
assertEquals(0, gavin.getVersion());
assertFalse( Hibernate.isInitialized( gavin.getTasks() ) );
s = openSession();
t = s.beginTransaction();
gavin = (Person) s.createCriteria(Person.class).uniqueResult();
gavin.getTasks().clear();
t.commit();
s.close();
assertEquals(0, gavin.getVersion());
assertTrue( Hibernate.isInitialized( gavin.getTasks() ) );
s = openSession();
t = s.beginTransaction();
s.delete(gavin);
t.commit();
s.close();
}
}