Search in sources :

Example 6 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 7 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 8 with StatelessSession

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

the class StatelessSessionQueryTest method testCriteria.

@Test
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) Test(org.junit.Test)

Example 9 with StatelessSession

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

the class StatelessSessionQueryTest method testHQL.

@Test
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) Test(org.junit.Test)

Example 10 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)

Aggregations

StatelessSession (org.hibernate.StatelessSession)10 Test (org.junit.Test)9 Transaction (org.hibernate.Transaction)6 Date (java.util.Date)2 ScrollableResults (org.hibernate.ScrollableResults)2 Session (org.hibernate.Session)2 EntityTransaction (javax.persistence.EntityTransaction)1 SessionFactory (org.hibernate.SessionFactory)1 Person (org.hibernate.userguide.model.Person)1