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[]
}
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();
}
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();
}
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();
}
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();
}
Aggregations