Package org.hibernate.ejb.test.lock

Source Code of org.hibernate.ejb.test.lock.LockTest

//$Id: LockTest.java 9796 2006-04-26 06:46:52Z epbernard $
package org.hibernate.ejb.test.lock;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;

import org.hibernate.ejb.test.TestCase;

/**
* @author Emmanuel Bernard
*/
public class LockTest extends TestCase {

  public void testLockRead() throws Exception {
    Lock lock = new Lock();
    lock.setName( "name" );
    EntityManager em = factory.createEntityManager();
    em.getTransaction().begin();
    em.persist( lock );
    em.getTransaction().commit();

    em.getTransaction().begin();
    lock = em.getReference( Lock.class, lock.getId() );
    em.lock( lock, LockModeType.READ );
    lock.setName( "surname" );
    em.getTransaction().commit();

    em.getTransaction().begin();
    lock = em.find( Lock.class, lock.getId() );
    assertEquals( "surname", lock.getName() );
    em.remove( lock );
    em.getTransaction().commit();
  }

  public void testLockWrite() throws Exception {
    Lock lock = new Lock();
    lock.setName( "second" );
    EntityManager em = factory.createEntityManager();
    em.getTransaction().begin();
    em.persist( lock );
    em.getTransaction().commit();

    em.getTransaction().begin();
    lock = em.getReference( Lock.class, lock.getId() );
    Integer version = lock.getVersion();
    em.lock( lock, LockModeType.WRITE );
    em.getTransaction().commit();

    em.getTransaction().begin();
    lock = em.getReference( Lock.class, lock.getId() );
    try {
      assertEquals( "should increase the version number EJB-106", 1, lock.getVersion() - version );
    }
    finally {
      em.remove( lock );
      em.getTransaction().commit();
    }
  }

  public Class[] getAnnotatedClasses() {
    return new Class[]{
        Lock.class
    };
  }
}
TOP

Related Classes of org.hibernate.ejb.test.lock.LockTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.