Search in sources :

Example 1 with StatelessSession

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

the class StatelessSessionTest method testRefresh.

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

Example 2 with StatelessSession

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

the class StatelessSessionTest method testHqlBulk.

@Test
public void testHqlBulk() {
    StatelessSession ss = sessionFactory().openStatelessSession();
    Transaction tx = ss.beginTransaction();
    Document doc = new Document("blah blah blah", "Blahs");
    ss.insert(doc);
    Paper paper = new Paper();
    paper.setColor("White");
    ss.insert(paper);
    tx.commit();
    tx = ss.beginTransaction();
    int count = ss.createQuery("update Document set name = :newName where name = :oldName").setString("newName", "Foos").setString("oldName", "Blahs").executeUpdate();
    assertEquals("hql-update on stateless session", 1, count);
    count = ss.createQuery("update Paper set color = :newColor").setString("newColor", "Goldenrod").executeUpdate();
    assertEquals("hql-update on stateless session", 1, count);
    tx.commit();
    tx = ss.beginTransaction();
    count = ss.createQuery("delete Document").executeUpdate();
    assertEquals("hql-delete on stateless session", 1, count);
    count = ss.createQuery("delete Paper").executeUpdate();
    assertEquals("hql-delete on stateless session", 1, count);
    tx.commit();
    ss.close();
}
Also used : StatelessSession(org.hibernate.StatelessSession) Transaction(org.hibernate.Transaction) Test(org.junit.Test)

Example 3 with StatelessSession

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

the class StatelessSessionFetchingTest method testDynamicFetch.

@Test
public void testDynamicFetch() {
    Session s = openSession();
    s.beginTransaction();
    Date now = new Date();
    User me = new User("me");
    User you = new User("you");
    Resource yourClock = new Resource("clock", you);
    // :)
    Task task = new Task(me, "clean", yourClock, now);
    s.save(me);
    s.save(you);
    s.save(yourClock);
    s.save(task);
    s.getTransaction().commit();
    s.close();
    StatelessSession ss = sessionFactory().openStatelessSession();
    ss.beginTransaction();
    Task taskRef = (Task) ss.createQuery("from Task t join fetch t.resource join fetch t.user").uniqueResult();
    assertTrue(taskRef != null);
    assertTrue(Hibernate.isInitialized(taskRef));
    assertTrue(Hibernate.isInitialized(taskRef.getUser()));
    assertTrue(Hibernate.isInitialized(taskRef.getResource()));
    assertFalse(Hibernate.isInitialized(taskRef.getResource().getOwner()));
    ss.getTransaction().commit();
    ss.close();
    cleanup();
}
Also used : StatelessSession(org.hibernate.StatelessSession) Date(java.util.Date) StatelessSession(org.hibernate.StatelessSession) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with StatelessSession

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

the class StatelessSessionInsertTest method testInsertWithForeignKey.

@Test
public void testInsertWithForeignKey() {
    Session session = sessionFactory().openSession();
    Transaction tx = session.beginTransaction();
    Message msg = new Message();
    final String messageId = "message_id";
    msg.setId(messageId);
    msg.setContent("message_content");
    msg.setSubject("message_subject");
    session.save(msg);
    tx.commit();
    session.close();
    StatelessSession statelessSession = sessionFactory().openStatelessSession();
    tx = statelessSession.beginTransaction();
    MessageRecipient signature = new MessageRecipient();
    signature.setId("recipient");
    signature.setEmail("recipient@hibernate.org");
    signature.setMessage(msg);
    statelessSession.insert(signature);
    tx.commit();
    cleanup();
}
Also used : Transaction(org.hibernate.Transaction) StatelessSession(org.hibernate.StatelessSession) StatelessSession(org.hibernate.StatelessSession) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with StatelessSession

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

the class StatelessSessionNativeQueryInsertTest method testInsertInStatelessSession.

@Test
@TestForIssue(jiraKey = "HHH-12141")
public void testInsertInStatelessSession() throws Exception {
    doInHibernate(this::sessionFactory, session -> {
        session.doWork(connection -> {
            StatelessSession sls = sessionFactory().openStatelessSession(connection);
            NativeQuery q = sls.createNativeQuery("INSERT INTO TEST_ENTITY (ID,SIMPLE_ATTRIBUTE) values (1,'red')");
            q.executeUpdate();
        });
    });
}
Also used : StatelessSession(org.hibernate.StatelessSession) NativeQuery(org.hibernate.query.NativeQuery) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

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