Package org.hibernate.test.jpa

Examples of org.hibernate.test.jpa.Item


      return;
    }
    String check = "EJB3 Specification";
    Session s1 = getSessions().openSession();
    Transaction t1 = s1.beginTransaction();
    Item item = new Item( check );
    s1.saveitem );
    t1.commit();
    s1.close();

    Long itemId = item.getId();
    long initialVersion = item.getVersion();

    // Now, open a new Session and re-load the item...
    s1 = getSessions().openSession();
    t1 = s1.beginTransaction();
    item = ( Item ) s1.get( Item.class, itemId );

    // now that the item is associated with the persistence-context of that session,
    // open a new session and modify it "behind the back" of the first session
    Session s2 = getSessions().openSession();
    Transaction t2 = s2.beginTransaction();
    Item item2 = ( Item ) s2.get( Item.class, itemId );
    item2.setName( "EJB3 Persistence Spec" );
    t2.commit();
    s2.close();

    // at this point, s1 now contains stale data, so try an hql query which
    // returns said item and make sure we get the previously associated state
    // (i.e., the old name and the old version)
    item2 = ( Item ) s1.createQuery( "select i from Item i" ).list().get( 0 );
    assertTrue( item == item2 );
    assertEquals( "encountered non-repeatable read", check, item2.getName() );
    assertEquals( "encountered non-repeatable read", initialVersion, item2.getVersion() );

    t1.commit();
    s1.close();

    // clean up
View Full Code Here


      return;
    }
    String check = "EJB3 Specification";
    Session s1 = getSessions().openSession();
    Transaction t1 = s1.beginTransaction();
    Item item = new Item( check );
    s1.saveitem );
    t1.commit();
    s1.close();

    Long itemId = item.getId();
    long initialVersion = item.getVersion();

    // Now, open a new Session and re-load the item...
    s1 = getSessions().openSession();
    t1 = s1.beginTransaction();
    item = ( Item ) s1.get( Item.class, itemId );

    // now that the item is associated with the persistence-context of that session,
    // open a new session and modify it "behind the back" of the first session
    Session s2 = getSessions().openSession();
    Transaction t2 = s2.beginTransaction();
    Item item2 = ( Item ) s2.get( Item.class, itemId );
    item2.setName( "EJB3 Persistence Spec" );
    t2.commit();
    s2.close();

    // at this point, s1 now contains stale data, so acquire a READ lock
    // and make sure we get the already associated state (i.e., the old
    // name and the old version)
    s1.lock( item, LockMode.READ );
    item2 = ( Item ) s1.get( Item.class, itemId );
    assertTrue( item == item2 );
    assertEquals( "encountered non-repeatable read", check, item2.getName() );
    assertEquals( "encountered non-repeatable read", initialVersion, item2.getVersion() );

    // attempt to acquire an UPGRADE lock; this should fail
    try {
      s1.lock( item, LockMode.UPGRADE );
      fail( "expected UPGRADE lock failure" );
View Full Code Here

      return;
    }
    String check = "Lock Modes";
    Session s1 = getSessions().openSession();
    Transaction t1 = s1.beginTransaction();
    Part part = new Part( new Item( "EJB3 Specification" ), check, "3.3.5.3", new BigDecimal( 0.0 ) );
    s1.save( part );
    t1.commit();
    s1.close();

    Long partId = part.getId();
View Full Code Here

      return;
    }
    String check = "Lock Modes";
    Session s1 = getSessions().openSession();
    Transaction t1 = s1.beginTransaction();
    Part part = new Part( new Item( "EJB3 Specification" ), check, "3.3.5.3", new BigDecimal( 0.0 ) );
    s1.save( part );
    t1.commit();
    s1.close();

    Long partId = part.getId();
View Full Code Here

    }
    final String initialName = "lock test";
    // set up some test data
    Session s1 = getSessions().openSession();
    Transaction t1 = s1.beginTransaction();
    Item item = new Item();
    item.setName( initialName );
    s1.save( item );
    t1.commit();
    s1.close();

    Long itemId = item.getId();

    // do the isolated update
    s1 = getSessions().openSession();
    t1 = s1.beginTransaction();
    item = (Item) s1.get( Item.class, itemId );
    s1.lock( item, LockMode.UPGRADE );
    item.setName( "updated" );
    s1.flush();

    Session s2 = getSessions().openSession();
    Transaction t2 = s2.beginTransaction();
    Item item2 = (Item) s2.get( Item.class, itemId );
    assertEquals( "isolation not maintained", initialName, item2.getName() );

    t1.commit();
    s1.close();

    item2 = (Item) s2.get( Item.class, itemId );
    assertEquals( "repeatable read not maintained", initialName, item2.getName() );
    t2.commit();
    s2.close();

    s1 = getSessions().openSession();
    t1 = s1.beginTransaction();
View Full Code Here

    }
    final String initialName = "lock test";
    // set up some test data
    Session s1 = getSessions().openSession();
    Transaction t1 = s1.beginTransaction();
    Item item = new Item();
    item.setName( initialName );
    s1.save( item );
    MyEntity myEntity = new MyEntity();
    myEntity.setName( "Test" );
    s1.save( myEntity );
    t1.commit();
    s1.close();

    Long itemId = item.getId();
    long initialVersion = item.getVersion();

    s1 = getSessions().openSession();
    t1 = s1.beginTransaction();
    item = (Item) s1.get( Item.class, itemId );
    s1.lock( item, LockMode.FORCE );
    assertEquals( "no forced version increment", initialVersion + 1, item.getVersion() );

    myEntity = (MyEntity) s1.get( MyEntity.class, myEntity.getId() );
    s1.lock( myEntity, LockMode.FORCE );
    assertTrue( "LockMode.FORCE on a un-versioned entity should degrade nicely to UPGRADE", true );

    s1.lock( item, LockMode.FORCE );
    assertEquals( "subsequent LockMode.FORCE did not no-op", initialVersion + 1, item.getVersion() );

    Session s2 = getSessions().openSession();
    Transaction t2 = s2.beginTransaction();
    Item item2 = (Item) s2.get( Item.class, itemId );
    assertEquals( "isolation not maintained", initialName, item2.getName() );

    item.setName( "updated-1" );
    s1.flush();
    // currently an unfortunate side effect...
    assertEquals( initialVersion + 2, item.getVersion() );

    t1.commit();
    s1.close();

    item2.setName( "updated" );
    try {
      t2.commit();
      fail( "optimistic lock should have failed" );
    }
    catch (Throwable ignore) {
View Full Code Here

  public void testEjb3ProxyUsage() {
    Session s = openSession();
    Transaction txn = s.beginTransaction();

    Item item = ( Item ) s.load( Item.class, new Long(-1) );
    assertFalse( Hibernate.isInitialized( item ) );
    try {
      Hibernate.initialize( item );
      fail( "proxy access did not fail on non-existent proxy" );
    }
    catch ( EntityNotFoundException e ) {
      // expected behavior
    }
    catch ( Throwable t ) {
      fail( "unexpected exception type on non-existent proxy access : " + t );
    }

    s.clear();

    Item item2 = ( Item ) s.load( Item.class, new Long(-1) );
    assertFalse( Hibernate.isInitialized( item2 ) );
    assertFalse( item == item2 );
    try {
      item2.getName();
      fail( "proxy access did not fail on non-existent proxy" );
    }
    catch ( EntityNotFoundException e ) {
      // expected behavior
    }
View Full Code Here

   */
  public void testGetSemantics() {
    Long nonExistentId = new Long( -1 );
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    Item item = ( Item ) s.get( Item.class, nonExistentId );
    assertNull( "get() of non-existent entity did not return null", item );
    txn.commit();
    s.close();

    s = openSession();
View Full Code Here

  }

  public void testRemoveThenContains() {
    Session s = openSession();
    s.beginTransaction();
    Item item = new Item();
    item.setName( "dummy" );
    s.persist( item );
    s.getTransaction().commit();
    s.close();

    s = openSession();
View Full Code Here

  }

  public void testRemoveThenGet() {
    Session s = openSession();
    s.beginTransaction();
    Item item = new Item();
    item.setName( "dummy" );
    s.persist( item );
    s.getTransaction().commit();
    s.close();

    Long id = item.getId();

    s = openSession();
    s.beginTransaction();
    s.delete( item );
    item = ( Item ) s.get( Item.class, id );
View Full Code Here

TOP

Related Classes of org.hibernate.test.jpa.Item

Copyright © 2018 www.massapicom. 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.