Search in sources :

Example 11 with StatelessSession

use of org.hibernate.StatelessSession in project hibernate-orm by hibernate.

the class StatelessSessionTest method testCreateUpdateReadDelete.

@Test
public void testCreateUpdateReadDelete() {
    StatelessSession ss = sessionFactory().openStatelessSession();
    Transaction tx = ss.beginTransaction();
    Document doc = new Document("blah blah blah", "Blahs");
    ss.insert(doc);
    assertNotNull(doc.getName());
    Date initVersion = doc.getLastModified();
    assertNotNull(initVersion);
    tx.commit();
    tx = ss.beginTransaction();
    doc.setText("blah blah blah .... blah");
    ss.update(doc);
    assertNotNull(doc.getLastModified());
    assertNotSame(doc.getLastModified(), initVersion);
    tx.commit();
    tx = ss.beginTransaction();
    doc.setText("blah blah blah .... blah blay");
    ss.update(doc);
    tx.commit();
    Document doc2 = (Document) ss.get(Document.class.getName(), "Blahs");
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
    doc2 = (Document) ss.createQuery("from Document where text is not null").uniqueResult();
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
    ScrollableResults sr = ss.createQuery("from Document where text is not null").scroll(ScrollMode.FORWARD_ONLY);
    sr.next();
    doc2 = (Document) sr.get(0);
    sr.close();
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
    doc2 = (Document) ss.createSQLQuery("select * from Document").addEntity(Document.class).uniqueResult();
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
    doc2 = (Document) ss.createCriteria(Document.class).uniqueResult();
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
    sr = ss.createCriteria(Document.class).scroll(ScrollMode.FORWARD_ONLY);
    sr.next();
    doc2 = (Document) sr.get(0);
    sr.close();
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
    tx = ss.beginTransaction();
    ss.delete(doc);
    tx.commit();
    ss.close();
}
Also used : StatelessSession(org.hibernate.StatelessSession) Transaction(org.hibernate.Transaction) ScrollableResults(org.hibernate.ScrollableResults) Date(java.util.Date) Test(org.junit.Test)

Example 12 with StatelessSession

use of org.hibernate.StatelessSession in project hibernate-orm by hibernate.

the class StatelessSessionTest method testInitId.

@Test
public void testInitId() {
    StatelessSession ss = sessionFactory().openStatelessSession();
    Transaction tx = ss.beginTransaction();
    Paper paper = new Paper();
    paper.setColor("White");
    ss.insert(paper);
    assertNotNull(paper.getId());
    tx.commit();
    tx = ss.beginTransaction();
    ss.delete(ss.get(Paper.class, paper.getId()));
    tx.commit();
    ss.close();
}
Also used : StatelessSession(org.hibernate.StatelessSession) Transaction(org.hibernate.Transaction) Test(org.junit.Test)

Example 13 with StatelessSession

use of org.hibernate.StatelessSession in project hibernate-orm by hibernate.

the class BatchTest method withStatelessSession.

private void withStatelessSession() {
    withBatch();
    // tag::batch-stateless-session-example[]
    StatelessSession statelessSession = null;
    Transaction txn = null;
    ScrollableResults scrollableResults = null;
    try {
        SessionFactory sessionFactory = entityManagerFactory().unwrap(SessionFactory.class);
        statelessSession = sessionFactory.openStatelessSession();
        txn = statelessSession.getTransaction();
        txn.begin();
        scrollableResults = statelessSession.createQuery("select p from Person p").scroll(ScrollMode.FORWARD_ONLY);
        while (scrollableResults.next()) {
            Person Person = (Person) scrollableResults.get(0);
            processPerson(Person);
            statelessSession.update(Person);
        }
        txn.commit();
    } catch (RuntimeException e) {
        if (txn != null && txn.getStatus() == TransactionStatus.ACTIVE)
            txn.rollback();
        throw e;
    } finally {
        if (scrollableResults != null) {
            scrollableResults.close();
        }
        if (statelessSession != null) {
            statelessSession.close();
        }
    }
// end::batch-stateless-session-example[]
}
Also used : SessionFactory(org.hibernate.SessionFactory) StatelessSession(org.hibernate.StatelessSession) Transaction(org.hibernate.Transaction) EntityTransaction(javax.persistence.EntityTransaction) ScrollableResults(org.hibernate.ScrollableResults) Person(org.hibernate.userguide.model.Person)

Example 14 with StatelessSession

use of org.hibernate.StatelessSession in project hibernate-orm by hibernate.

the class StatelessSessionQueryTest method testCriteria.

@Test
@SkipForDialect(value = AbstractHANADialect.class, comment = " HANA doesn't support tables consisting of only a single auto-generated column")
public void testCriteria() {
    TestData testData = new TestData();
    testData.createData();
    StatelessSession s = sessionFactory().openStatelessSession();
    assertEquals(1, s.createCriteria(Contact.class).list().size());
    s.close();
    testData.cleanData();
}
Also used : StatelessSession(org.hibernate.StatelessSession) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 15 with StatelessSession

use of org.hibernate.StatelessSession in project hibernate-orm by hibernate.

the class StatelessSessionQueryTest method testHQL.

@Test
@SkipForDialect(value = AbstractHANADialect.class, comment = " HANA doesn't support tables consisting of only a single auto-generated column")
public void testHQL() {
    TestData testData = new TestData();
    testData.createData();
    StatelessSession s = sessionFactory().openStatelessSession();
    assertEquals(1, s.createQuery("from Contact c join fetch c.org join fetch c.org.country").list().size());
    s.close();
    testData.cleanData();
}
Also used : StatelessSession(org.hibernate.StatelessSession) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Aggregations

StatelessSession (org.hibernate.StatelessSession)17 Test (org.junit.Test)11 Transaction (org.hibernate.Transaction)7 SkipForDialect (org.hibernate.testing.SkipForDialect)3 Date (java.util.Date)2 ScrollableResults (org.hibernate.ScrollableResults)2 Session (org.hibernate.Session)2 SessionFactory (org.hibernate.SessionFactory)2 TestForIssue (org.hibernate.testing.TestForIssue)2 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)2 DeletedObject (org.hisp.dhis.deletedobject.DeletedObject)2 Serializable (java.io.Serializable)1 EntityTransaction (javax.persistence.EntityTransaction)1 HibernateException (org.hibernate.HibernateException)1 NativeQuery (org.hibernate.query.NativeQuery)1 Triggerable (org.hibernate.testing.logger.Triggerable)1 Person (org.hibernate.userguide.model.Person)1 InvalidIdentifierReferenceException (org.hisp.dhis.common.exception.InvalidIdentifierReferenceException)1 DeletedObjectQuery (org.hisp.dhis.deletedobject.DeletedObjectQuery)1