Package org.hibernate

Examples of org.hibernate.Transaction


          .writeConcern( WriteConcernType.ACKNOWLEDGED );

    sessions = configuration.buildSessionFactory();

    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 tuple and association operations using the configured write concerns
    verify( mockClient.getCollection( "GolfPlayer" ) ).insert( any( List.class ), eq( WriteConcern.REPLICA_ACKNOWLEDGED ) );
    verify( mockClient.getCollection( "Associations" ) ).update( any( DBObject.class ), any( DBObject.class ), anyBoolean(), anyBoolean(), eq( WriteConcern.ACKNOWLEDGED ) );
View Full Code Here


  }

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

    // given
    Snack nachos = new Snack( "nachos" );
    Snack frozenYogurt = new Snack( "frozen yogurt" );
    Ingredient milk = new Ingredient( "milk" );
    Ingredient salt = new Ingredient( "salt" );

    nachos.getIngredients().add( salt );
    salt.getContainedIn().add( nachos );

    frozenYogurt.getIngredients().add( milk );
    milk.getContainedIn().add( frozenYogurt );

    frozenYogurt.getIngredients().add( salt );
    salt.getContainedIn().add( frozenYogurt );

    // when
    session.persist( nachos );
    session.persist( frozenYogurt );
    session.persist( milk );
    session.persist( salt );

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

    // then
    Snack frozenYogurtLoaded = (Snack) session.load( Snack.class, frozenYogurt.getId() );

    assertThat( frozenYogurtLoaded.getName() ).isEqualTo( "frozen yogurt" );
    assertThat( frozenYogurtLoaded.getIngredients() ).onProperty( "name" ).containsOnly( "salt", "milk" );

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

    Ingredient milkLoaded = (Ingredient) session.load( Ingredient.class, milk.getId() );
    assertThat( milkLoaded.getName() ).isEqualTo( "milk" );
    assertThat( milkLoaded.getContainedIn() ).onProperty( "name" ).containsOnly( "frozen yogurt" );

    Ingredient saltLoaded = (Ingredient) session.load( Ingredient.class, salt.getId() );
    assertThat( saltLoaded.getName() ).isEqualTo( "salt" );
    assertThat( saltLoaded.getContainedIn() ).onProperty( "name" ).containsOnly( "nachos", "frozen yogurt" );

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

        .build();

    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ) );

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

    // when merging the player with two associated courses
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1, new GolfCourse( 1L, "Bepple Peach" ), new GolfCourse( 2L, "Ant Sandrews" ) );
    session.merge( ben );

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

    // then expect updates to the player document using the configured write concern
    verify( mockClient.getCollection( "GolfPlayer" ), times( 2 ) ).update( any( DBObject.class ), any( DBObject.class ), anyBoolean(), anyBoolean(), eq( WriteConcern.MAJORITY ) );
  }
View Full Code Here

        .build();

    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ), AssociationStorageType.ASSOCIATION_DOCUMENT );

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

    // when merging the player with two associated courses
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1, new GolfCourse( 1L, "Bepple Peach" ), new GolfCourse( 2L, "Ant Sandrews" ) );
    session.merge( ben );

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

    // then expect one update to the association collection
    verify( mockClient.getCollection( "Associations" ), times( 1 ) ).update( any( DBObject.class ), any( DBObject.class ), anyBoolean(), anyBoolean(), eq( WriteConcern.MAJORITY ) );
  }
View Full Code Here

  }

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

    // given
    Singer gloria = new Singer( "Gloria" );

    // when
    session.persist( gloria );

    tx.commit();
    assertThat( gloria.getId() ).isNotNull();
    session.clear();
    tx = session.beginTransaction();

    Singer singerLoaded = (Singer) session.load( Singer.class, gloria.getId() );

    // then
    assertThat( singerLoaded.getName() ).isEqualTo( "Gloria" );

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

  }

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

    // given
    Comedian monty = new Comedian( "Monty" );

    // when
    session.persist( monty );

    tx.commit();
    assertThat( monty.getId() ).isNotNull();
    session.clear();
    tx = session.beginTransaction();

    // then
    assertCountQueryResult( session, "db.Comedian.count({ \"_id\" : { \"$oid\" : \"" + monty.getId() + "\" }, \"name\" : \"Monty\" })", 1L);

    Comedian montyLoaded = (Comedian) session.load( Comedian.class, monty.getId() );
    assertThat( ObjectId.isValid( montyLoaded.getId() ) ).isTrue();
    assertThat( montyLoaded.getName() ).isEqualTo( "Monty" );

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

        .build();

    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ) );

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

    // when removing the association
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1 );
    session.merge( ben );

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

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

        .build();

    setupSessionFactory( new MongoDBDatastoreProvider( mockClient.getClient() ) , AssociationStorageType.ASSOCIATION_DOCUMENT );

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

    // when removing the association
    GolfPlayer ben = new GolfPlayer( 1L, "Ben", 0.1 );
    session.merge( ben );

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

    // then expect one call to remove with the configured write concern
    verify( mockClient.getCollection( "Associations" ) ).remove( any( DBObject.class ), eq( WriteConcern.MAJORITY ) );
  }
View Full Code Here

public class OneToOneInEntityMappingTest extends OgmTestCase {

  @Test
  public void testBidirectionalManyToOneMapping() 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

    }
  }

  private Transaction beginTransaction(Session session) throws ClassNotFoundException, NoSuchMethodException,
      IllegalAccessException, InvocationTargetException {
    Transaction transaction = Helper.getTransactionAndMarkForJoin( session );
    transaction.begin();
    return transaction;
  }
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.