Package org.hibernate

Examples of org.hibernate.Transaction


  }

  @Test
  public void insertUpdateAndDelete() throws Exception {
    Session session = openSession();
    Transaction transaction = session.beginTransaction();

    // insert
    StockItem item = new StockItem();
    item.setId( "item-1" );
    item.setItemName( "Fairway Wood 19°" );
    item.setCount( 25 );
    session.persist( item );

    transaction.commit();
    session.clear();
    transaction = session.beginTransaction();

    // load and update
    StockItem loadedItem = (StockItem) session.get( StockItem.class, item.getId() );
    assertNotNull( "Cannot load persisted object", loadedItem );
    loadedItem.setCount( 24 );

    transaction.commit();
    session.clear();
    transaction = session.beginTransaction();

    // delete
    loadedItem = (StockItem) session.get( StockItem.class, item.getId() );
    assertNotNull( "Cannot load persisted object", loadedItem );
    session.delete( loadedItem );

    transaction.commit();
    session.clear();
    session.close();

    assertThat( getOperations() ).containsExactly(
        "getTuple",
View Full Code Here


  }

  @Test
  public void insertGetOfEntityAndElementCollectionAndDelete() throws Exception {
    Session session = openSession();
    Transaction transaction = session.beginTransaction();

    // insert
    Product product = new Product(
        "product-1",
        "Hybrid 25°",
        new Vendor( "Leaveland", 10 ), new Vendor( "Headaway", 9 )
    );
    session.persist( product );

    transaction.commit();
    session.clear();
    transaction = session.beginTransaction();

    // load and update
    Product loadedProduct = (Product) session.get( Product.class, "product-1" );
    assertNotNull( "Cannot load persisted object", loadedProduct );
    assertThat( loadedProduct.getVendors() ).onProperty( "name" ).containsOnly( "Leaveland", "Headaway" );

    transaction.commit();
    session.clear();
    transaction = session.beginTransaction();

    // delete
    loadedProduct = (Product) session.get( Product.class, "product-1" );
    assertNotNull( "Cannot load persisted object", loadedProduct );
    session.delete( loadedProduct );

    transaction.commit();
    session.clear();
    session.close();

    assertThat( getOperations() ).containsExactly(
        "getTuple",
View Full Code Here

  }

  @Test
  public void insertAndUpdateInSameTransaction() throws Exception {
    Session session = openSession();
    Transaction transaction = session.beginTransaction();

    // insert
    StockItem item = new StockItem();
    item.setId( "item-1" );
    item.setItemName( "Fairway Wood 19°" );
    item.setCount( 25 );
    session.persist( item );

    session.flush();

    // update
    item.setCount( 24 );

    transaction.commit();
    session.clear();
    transaction = session.beginTransaction();

    StockItem loadedItem = (StockItem) session.get( StockItem.class, item.getId() );
    assertNotNull( "Cannot load persisted object", loadedItem );
    session.delete( loadedItem );

    transaction.commit();
    session.close();

    assertThat( getOperations() ).containsExactly(
        "getTuple",
        "createTuple",
View Full Code Here

  }

  @Test
  public void insertSearchUpdateDelete() throws Exception {
    Session session = openSession();
    Transaction transaction = session.beginTransaction();

    // insert
    StockItem item = new StockItem();
    item.setId( "item-1" );
    item.setItemName( "Fairway Wood 19°" );
    item.setCount( 25 );
    session.persist( item );

    transaction.commit();
    session.clear();
    transaction = session.beginTransaction();

    // search and update
    StockItem loadedItem = (StockItem) session.createQuery( "from StockItem si" ).uniqueResult();
    assertNotNull( "Cannot load persisted object", loadedItem );
    loadedItem.setCount( 24 );

    transaction.commit();
    session.clear();
    transaction = session.beginTransaction();

    loadedItem = (StockItem) session.get( StockItem.class, item.getId() );
    assertNotNull( "Cannot load persisted object", loadedItem );
    assertThat( loadedItem.getCount() ).isEqualTo( 24 );
    session.delete( loadedItem );

    transaction.commit();
    session.close();
    assertThat( getOperations() ).containsExactly(
        "getTuple",
        "createTuple",
        "executeBatch",
View Full Code Here

   */
  @After
  public void closeOpenedSessions() {
    for ( Session session : openedSessions ) {
      if ( session.isOpen() ) {
        Transaction transaction = session.getTransaction();
        if ( transaction != null && transaction.isActive() ) {
          transaction.rollback();
        }
        session.close();
      }
    }
  }
View Full Code Here

public class TableGeneratorTest extends OgmTestCase {

  @Test
  public void canUseSpecificValueColumNames() {
    OgmSession session = openSession();
    Transaction tx = session.beginTransaction();

    // given
    PianoPlayer ken = new PianoPlayer( "Ken Gold" );
    GuitarPlayer buck = new GuitarPlayer( "Buck Cherry" );
    // when
    session.persist( ken );
    session.persist( buck );

    tx.commit();
    session.clear();
    tx = session.beginTransaction();
    ken = (PianoPlayer) session.load( PianoPlayer.class, ken.getId() );
    buck = (GuitarPlayer) session.load( GuitarPlayer.class, buck.getId() );

    // then
    assertThat( ken.getId() ).isEqualTo( 1L );
    assertThat( buck.getId() ).isEqualTo( 1L );
    assertCountQueryResult( session, "db.PianoPlayerSequence.count( { '_id' : 'pianoPlayer', 'nextPianoPlayerId' : 2 } )", 1 );
    assertCountQueryResult( session, "db.GuitarPlayerSequence.count( { '_id' : 'guitarPlayer', 'nextGuitarPlayerId' : 2 } )", 1 );

    tx.commit();
    session.close();
  }
View Full Code Here

  }

  @Test
  public void canUseObjectIdAssignedUponInsertInAssociation() {
    OgmSession session = openSession();
    Transaction tx = session.beginTransaction();

    // given
    MusicGenre classicRock = new MusicGenre( "Classic Rock" );

    Bar goldFishBar = new Bar( "Goldfisch Bar" );
    goldFishBar.setMusicGenre( classicRock );
    classicRock.getPlayedIn().add( goldFishBar );

    Bar sharkStation = new Bar( "Shark Station" );
    sharkStation.setMusicGenre( classicRock );
    classicRock.getPlayedIn().add( sharkStation );

    // when
    session.persist( classicRock );
    session.persist( goldFishBar );
    session.persist( sharkStation );

    tx.commit();
    session.clear();
    tx = session.beginTransaction();

    // then
    Bar barLoaded = (Bar) session.load( Bar.class, goldFishBar.getId() );

    assertThat( barLoaded.getName() ).isEqualTo( "Goldfisch Bar" );
    assertThat( barLoaded.getMusicGenre() ).isNotNull();
    assertThat( barLoaded.getMusicGenre().getName() ).isEqualTo( "Classic Rock" );

    tx.commit();
    session.clear();
    tx = session.beginTransaction();

    MusicGenre genreLoaded = (MusicGenre) session.load( MusicGenre.class, goldFishBar.getMusicGenre().getId() );
    assertThat( genreLoaded.getPlayedIn() ).onProperty( "name" ).containsOnly( "Goldfisch Bar", "Shark Station" );

    tx.commit();
    session.close();
  }
View Full Code Here

    // given an empty database
    MockMongoClient mockClient = mockClient().build();
    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ), AssociationStorageType.ASSOCIATION_DOCUMENT );

    Session session = sessions.openSession();
    Transaction transaction = session.beginTransaction();

    // when inserting a player with an associated course
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1, new GolfCourse( 1L, "Bepple Peach" ) );
    session.persist( ben );

    transaction.commit();
    session.close();

    // then expect association operations using the configured write concern
    verify( mockClient.getCollection( "Associations" ) ).update( any( DBObject.class ), any( DBObject.class ), anyBoolean(), anyBoolean(), eq( WriteConcern.MAJORITY ) );
  }
View Full Code Here

  }

  @Test
  public void canUseObjectIdAssignedUponInsertInOneToManyAssociation() {
    OgmSession session = openSession();
    Transaction tx = session.beginTransaction();

    // given
    Bar goldFishBar = new Bar( "Goldfisch Bar" );
    goldFishBar.getDoorMen().add( new DoorMan( "Bruce" ) );
    goldFishBar.getDoorMen().add( new DoorMan( "Dwain" ) );

    // when
    session.persist( goldFishBar );

    tx.commit();
    session.clear();
    tx = session.beginTransaction();

    // then
    Bar barLoaded = (Bar) session.load( Bar.class, goldFishBar.getId() );
    assertThat( barLoaded.getDoorMen() ).onProperty( "name" ).containsOnly( "Bruce", "Dwain" );

    tx.commit();
    session.close();
  }
View Full Code Here

public class OneToOneCollectionMappingTest extends OgmTestCase {

  @Test
  public void testAssociationStorageSettingIsIgnoredForBidirectionalManyToOneMapping() throws Exception {
    OgmSession session = openSession();
    Transaction transaction = session.beginTransaction();

    // Given, When
    Husband husband = new Husband( "alex" );
    husband.setName( "Alex" );
    session.persist( husband );

    Wife wife = new Wife( "bea" );
    wife.setName( "Bea" );
    husband.setWife( wife );
    wife.setHusband( husband );
    session.persist( wife );

    transaction.commit();
    session.clear();

    transaction = session.beginTransaction();

    // Then
    assertDbObject(
        session.getSessionFactory(),
        // collection
        "Wife",
        // query
        "{ '_id' : 'bea' }",
        // expected
        "{ " +
          "'_id' : 'bea', " +
          "'name' : 'Bea'," +
          "'husband' : 'alex'" +
        "}"
    );

    assertDbObject(
        session.getSessionFactory(),
        // collection
        "Husband",
        // query
        "{ '_id' : 'alex' }",
        // expected
        "{ " +
          "'_id' : 'alex', " +
          "'name' : 'Alex'," +
          "'wife' : 'bea'" +
        "}"
    );

    // Clean-Up
    husband = (Husband) session.get( Husband.class, husband.getId() );
    wife = (Wife) session.get( Wife.class, wife.getId() );
    session.delete( wife );
    session.delete( husband );

    transaction.commit();
    session.close();
  }
View Full Code Here

TOP

Related Classes of org.hibernate.Transaction

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.