Search in sources :

Example 46 with Session

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

the class StatelessSessionInsertTest method cleanup.

private void cleanup() {
    Session s = openSession();
    s.beginTransaction();
    s.createQuery("delete MessageRecipient").executeUpdate();
    s.createQuery("delete Message").executeUpdate();
    s.getTransaction().commit();
    s.close();
}
Also used : StatelessSession(org.hibernate.StatelessSession) Session(org.hibernate.Session)

Example 47 with Session

use of org.hibernate.Session 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 48 with Session

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

the class SessionStatsTest method testSessionStatistics.

@Test
public void testSessionStatistics() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    boolean isStats = stats.isStatisticsEnabled();
    stats.setStatisticsEnabled(true);
    Continent europe = fillDb(s);
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    SessionStatistics sessionStats = s.getStatistics();
    assertEquals(0, sessionStats.getEntityKeys().size());
    assertEquals(0, sessionStats.getEntityCount());
    assertEquals(0, sessionStats.getCollectionKeys().size());
    assertEquals(0, sessionStats.getCollectionCount());
    europe = (Continent) s.get(Continent.class, europe.getId());
    Hibernate.initialize(europe.getCountries());
    Hibernate.initialize(europe.getCountries().iterator().next());
    assertEquals(2, sessionStats.getEntityKeys().size());
    assertEquals(2, sessionStats.getEntityCount());
    assertEquals(1, sessionStats.getCollectionKeys().size());
    assertEquals(1, sessionStats.getCollectionCount());
    tx.commit();
    s.close();
    stats.setStatisticsEnabled(isStats);
}
Also used : SessionStatistics(org.hibernate.stat.SessionStatistics) Transaction(org.hibernate.Transaction) SessionStatistics(org.hibernate.stat.SessionStatistics) Statistics(org.hibernate.stat.Statistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 49 with Session

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

the class BasicStreamTest method testQueryStream.

@Test
@TestForIssue(jiraKey = "HHH-10824")
public void testQueryStream() {
    Session session = openSession();
    try {
        session.getTransaction().begin();
        MyEntity e = new MyEntity();
        e.id = 1;
        e.name = "Test";
        session.persist(e);
        session.getTransaction().commit();
        session.clear();
        // Test stream query without type.
        Object result = session.createQuery("From MyEntity").stream().findFirst().orElse(null);
        assertTyping(MyEntity.class, result);
        // Test stream query with type.
        result = session.createQuery("From MyEntity", MyEntity.class).stream().findFirst().orElse(null);
        assertTyping(MyEntity.class, result);
        // Test stream query using forEach
        session.createQuery("From MyEntity", MyEntity.class).stream().forEach(i -> {
            assertTyping(MyEntity.class, i);
        });
        Stream<Object[]> data = session.createQuery("SELECT me.id, me.name FROM MyEntity me").stream();
        data.forEach(i -> {
            assertTyping(Integer.class, i[0]);
            assertTyping(String.class, i[1]);
        });
    } finally {
        session.close();
    }
}
Also used : Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 50 with Session

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

the class MySQL57TimestampPropertyTest method testTimeGeneratedByColumnDefault.

@Test
public void testTimeGeneratedByColumnDefault() {
    final Entity eOrig = new Entity();
    Session s = openSession();
    s.getTransaction().begin();
    s.persist(eOrig);
    s.getTransaction().commit();
    s.close();
    assertNotNull(eOrig.tsColumnDefault);
    s = openSession();
    s.getTransaction().begin();
    final Entity eGotten = (Entity) s.get(Entity.class, eOrig.id);
    final String tsColumnDefaultOrigFormatted = timestampFormat.format(eOrig.tsColumnDefault);
    final String tsColumnDefaultGottenFormatted = timestampFormat.format(eGotten.tsColumnDefault);
    assertEquals(tsColumnDefaultOrigFormatted, tsColumnDefaultGottenFormatted);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.getTransaction().begin();
    final Query queryWithParameter = s.createQuery("from MySQL57TimestampPropertyTest$Entity where tsColumnDefault=?").setParameter(0, eOrig.tsColumnDefault);
    final Entity eQueriedWithParameter = (Entity) queryWithParameter.uniqueResult();
    assertNotNull(eQueriedWithParameter);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.getTransaction().begin();
    final Query queryWithTimestamp = s.createQuery("from MySQL57TimestampPropertyTest$Entity where tsColumnDefault=?").setTimestamp(0, eOrig.tsColumnDefault);
    final Entity eQueriedWithTimestamp = (Entity) queryWithTimestamp.uniqueResult();
    assertNotNull(eQueriedWithTimestamp);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.getTransaction().begin();
    s.delete(eQueriedWithTimestamp);
    s.getTransaction().commit();
    s.close();
}
Also used : Query(org.hibernate.Query) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)4457 Test (org.junit.Test)2517 Transaction (org.hibernate.Transaction)1327 List (java.util.List)576 ArrayList (java.util.ArrayList)475 Test (org.testng.annotations.Test)411 TestForIssue (org.hibernate.testing.TestForIssue)389 Query (org.hibernate.Query)345 HibernateException (org.hibernate.HibernateException)299 DAOException (com.tomasio.projects.trainning.exception.DAOException)186 Criteria (org.hibernate.Criteria)174 Date (java.util.Date)134 Iterator (java.util.Iterator)127 SkipForDialect (org.hibernate.testing.SkipForDialect)114 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)113 SQLException (java.sql.SQLException)108 Map (java.util.Map)105 SessionFactory (org.hibernate.SessionFactory)105 GradingFactorDaoException (com.remswork.project.alice.dao.exception.GradingFactorDaoException)104 GradingFactorException (com.remswork.project.alice.exception.GradingFactorException)104