Package org.hibernate.ogm

Examples of org.hibernate.ogm.OgmSession


    }
  }

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

    String nativeQuery = "MATCH ( n:" + TABLE_NAME + " { name:'Portia', author:'Oscar Wilde' } ) RETURN n";
    OscarWildePoem poem = (OscarWildePoem) session.createNativeQuery( nativeQuery )
        .addEntity( OscarWildePoem.TABLE_NAME, OscarWildePoem.class )
        .uniqueResult();

    assertThat( poem ).isEqualTo( portia );

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


    session.close();
  }

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

    String nativeQuery = "MATCH ( n:" + TABLE_NAME + " ) RETURN n ORDER BY n.name";
    @SuppressWarnings("unchecked")
    List<OscarWildePoem> result = session.createNativeQuery( nativeQuery )
        .addEntity( OscarWildePoem.TABLE_NAME, OscarWildePoem.class )
        .list();

    assertThat( result ).as( "Unexpected number of results" ).containsExactly( athanasia, ballade, portia );

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

    session.close();
  }

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

    String nativeQuery = "MATCH ( n:" + TABLE_NAME + " ) RETURN n.name, n.author ORDER BY n.name";
    @SuppressWarnings("unchecked")
    List<Object[]> result = session.createNativeQuery( nativeQuery ).list();

    assertThat( result ).as( "Unexpected number of results" ).hasSize( 3 );

    Object[] athanasiaRow = result.get( 0 );
    assertThat( athanasiaRow[0] ).isEqualTo( athanasia.getName() );
    assertThat( athanasiaRow[1] ).isEqualTo( athanasia.getAuthor() );

    Object[] balladeRow = result.get( 1 );
    assertThat( balladeRow[0] ).isEqualTo( ballade.getName() );
    assertThat( balladeRow[1] ).isEqualTo( ballade.getAuthor() );

    Object[] portiaRow = result.get( 2 );
    assertThat( portiaRow[0] ).isEqualTo( portia.getName() );
    assertThat( portiaRow[1] ).isEqualTo( portia.getAuthor() );

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

    session.close();
  }

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

    try {
      OscarWildePoem uniqueResult = (OscarWildePoem) session.getNamedQuery( "AthanasiaQuery" )
          .uniqueResult();
      assertThat( uniqueResult ).isEqualTo( athanasia );
      transaction.commit();
    }
    finally {
      session.clear();
      session.close();
    }
  }
View Full Code Here

    }
  }

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

    try {
      String nativeQuery = "MATCH ( n:" + TABLE_NAME + " { name:{name}, author:'Oscar Wilde' } ) RETURN n";
      SQLQuery query = session.createNativeQuery( nativeQuery ).addEntity( OscarWildePoem.class );
      query.setParameter( "name", "Portia" );

      OscarWildePoem uniqueResult = (OscarWildePoem) query.uniqueResult();
      assertThat( uniqueResult ).isEqualTo( portia );
      transaction.commit();
    }
    finally {
      session.clear();
      session.close();
    }
  }
View Full Code Here

    }
  }

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

    String nativeQuery = "MATCH ( n:" + TABLE_NAME + " { author:'Oscar Wilde' } ) RETURN n ORDER BY n.name";
    Query query = session.createNativeQuery( nativeQuery )
        .addEntity( OscarWildePoem.class )
        .setFirstResult( 1 );
    @SuppressWarnings("unchecked")
    List<OscarWildePoem> result = query.list();

    assertThat( result ).containsExactly( ballade, portia );

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

  }
View Full Code Here

  }

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

    String nativeQuery = "MATCH ( n:" + TABLE_NAME + " { author:'Oscar Wilde' } ) RETURN n ORDER BY n.name";
    Query query = session.createNativeQuery( nativeQuery )
        .addEntity( OscarWildePoem.class )
        .setMaxResults( 2 );
    @SuppressWarnings("unchecked")
    List<OscarWildePoem> result = query.list();

    assertThat( result ).containsExactly( athanasia, ballade );

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

public class ObjectIdWithEmbeddableTest extends OgmTestCase {

  @Test
  @TestForIssue(jiraKey = "OGM-612")
  public void canUseObjectIdAssignedUponInsertWithEmbeddable() {
    OgmSession session = openSession();
    Transaction tx = session.beginTransaction();

    // given
    EntityWithObjectIdAndEmbeddable entity = new EntityWithObjectIdAndEmbeddable();
    AnEmbeddable anEmbeddable = new AnEmbeddable( "a very nice string", null );
    entity.setAnEmbeddable( anEmbeddable );

    // when
    session.persist( entity );

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

    EntityWithObjectIdAndEmbeddable loaded = (EntityWithObjectIdAndEmbeddable) session.load( EntityWithObjectIdAndEmbeddable.class, entity.getId() );

    // then
    assertThat( loaded.getId() ).isEqualTo( entity.getId() );
    assertThat( loaded.getAnEmbeddable().getEmbeddedString() ).isEqualTo( entity.getAnEmbeddable().getEmbeddedString() );
    assertThat( loaded.getAnEmbeddable().getAnotherEmbeddable() ).isEqualTo( entity.getAnEmbeddable().getAnotherEmbeddable() );

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

  }

  @Test
  @TestForIssue(jiraKey = "OGM-612")
  public void canUseObjectIdAssignedUponInsertWithNestedEmbeddable() {
    OgmSession session = openSession();
    Transaction tx = session.beginTransaction();

    // given
    EntityWithObjectIdAndEmbeddable entity = new EntityWithObjectIdAndEmbeddable();
    AnotherEmbeddable anotherEmbeddable = new AnotherEmbeddable( "Another nice string ... nested" );
    AnEmbeddable anEmbeddable = new AnEmbeddable( "a very nice string", anotherEmbeddable );
    entity.setAnEmbeddable( anEmbeddable );

    // when
    session.persist( entity );

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

    EntityWithObjectIdAndEmbeddable loaded = (EntityWithObjectIdAndEmbeddable) session.load( EntityWithObjectIdAndEmbeddable.class, entity.getId() );

    // then
    assertThat( loaded.getId() ).isEqualTo( entity.getId() );
    assertThat( loaded.getAnEmbeddable().getEmbeddedString() ).isEqualTo( entity.getAnEmbeddable().getEmbeddedString() );
    assertThat( loaded.getAnEmbeddable().getAnotherEmbeddable().getEmbeddedString() ).isEqualTo(
        entity.getAnEmbeddable().getAnotherEmbeddable().getEmbeddedString() );

    tx.commit();
    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

TOP

Related Classes of org.hibernate.ogm.OgmSession

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.