Search in sources :

Example 36 with Session

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

the class NativeSQLQueriesTest method testFailOnNoAddEntityOrScalar.

@Test
@SkipForDialect(H2Dialect.class)
public void testFailOnNoAddEntityOrScalar() {
    // Note: this passes, but for the wrong reason.
    //      there is actually an exception thrown, but it is the database
    //      throwing a sql exception because the SQL gets passed
    //      "un-processed"...
    //
    // Oddly, H2 accepts this query.
    Session s = openSession();
    s.beginTransaction();
    try {
        String sql = "select {org.*} " + "from organization org";
        s.createSQLQuery(sql).list();
        fail("Should throw an exception since no addEntity nor addScalar has been performed.");
    } catch (PersistenceException pe) {
    // expected behavior
    } finally {
        s.getTransaction().rollback();
        s.close();
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 37 with Session

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

the class SortTest method testOrderBy.

@Test
@SuppressWarnings({ "unchecked" })
public void testOrderBy() {
    Search s = new Search("Hibernate");
    s.getSearchResults().add("jboss.com");
    s.getSearchResults().add("hibernate.org");
    s.getSearchResults().add("HiA");
    Session sess = openSession();
    Transaction tx = sess.beginTransaction();
    sess.persist(s);
    sess.flush();
    sess.clear();
    s = (Search) sess.createCriteria(Search.class).uniqueResult();
    assertFalse(Hibernate.isInitialized(s.getSearchResults()));
    Iterator iter = s.getSearchResults().iterator();
    assertEquals(iter.next(), "HiA");
    assertEquals(iter.next(), "hibernate.org");
    assertEquals(iter.next(), "jboss.com");
    assertFalse(iter.hasNext());
    sess.clear();
    s = (Search) sess.createCriteria(Search.class).setFetchMode("searchResults", FetchMode.JOIN).uniqueResult();
    assertTrue(Hibernate.isInitialized(s.getSearchResults()));
    iter = s.getSearchResults().iterator();
    assertEquals(iter.next(), "HiA");
    assertEquals(iter.next(), "hibernate.org");
    assertEquals(iter.next(), "jboss.com");
    assertFalse(iter.hasNext());
    sess.clear();
    s = (Search) sess.createQuery("from Search s left join fetch s.searchResults").uniqueResult();
    assertTrue(Hibernate.isInitialized(s.getSearchResults()));
    iter = s.getSearchResults().iterator();
    assertEquals(iter.next(), "HiA");
    assertEquals(iter.next(), "hibernate.org");
    assertEquals(iter.next(), "jboss.com");
    assertFalse(iter.hasNext());
    sess.delete(s);
    tx.commit();
    sess.close();
}
Also used : Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 38 with Session

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

the class NativeQueryScrollableResults method prepareTest.

@Override
protected void prepareTest() throws Exception {
    try (Session session = openSession()) {
        session.getTransaction().begin();
        try {
            session.save(new MyEntity(1L, "entity_1", new BigInteger("3")));
            session.save(new MyEntity(2L, "entity_2", new BigInteger("6")));
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            throw e;
        }
    }
}
Also used : BigInteger(java.math.BigInteger) Session(org.hibernate.Session)

Example 39 with Session

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

the class AutoDiscoveryTest method testDialectGetColumnAliasExtractor.

@Test
public void testDialectGetColumnAliasExtractor() throws Exception {
    Session session = openSession();
    final SessionImplementor sessionImplementor = (SessionImplementor) session;
    session.beginTransaction();
    session.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            PreparedStatement ps = sessionImplementor.getJdbcCoordinator().getStatementPreparer().prepareStatement(QUERY_STRING);
            ResultSet rs = sessionImplementor.getJdbcCoordinator().getResultSetReturn().extract(ps);
            try {
                ResultSetMetaData metadata = rs.getMetaData();
                String column1Alias = getDialect().getColumnAliasExtractor().extractColumnAlias(metadata, 1);
                String column2Alias = getDialect().getColumnAliasExtractor().extractColumnAlias(metadata, 2);
                Assert.assertFalse("bad dialect.getColumnAliasExtractor impl", column1Alias.equals(column2Alias));
            } finally {
                sessionImplementor.getJdbcCoordinator().getResourceRegistry().release(rs, ps);
                sessionImplementor.getJdbcCoordinator().getResourceRegistry().release(ps);
            }
        }
    });
    session.getTransaction().commit();
    session.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PreparedStatement(java.sql.PreparedStatement) Session(org.hibernate.Session) Test(org.junit.Test)

Example 40 with Session

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

the class ResultCheckStyleTest method testDeleteWithParamChecking.

@Test
public void testDeleteWithParamChecking() {
    Session s = openSession();
    s.beginTransaction();
    ParamCheckingEntity e = new ParamCheckingEntity();
    e.setId(Long.valueOf(1));
    e.setName("dummy");
    s.delete(e);
    try {
        s.flush();
        fail("expection flush failure!");
    } catch (Exception ex) {
    // these should specifically be HibernateExceptions...
    }
    s.clear();
    s.getTransaction().rollback();
    s.close();
}
Also used : Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)3262 Test (org.junit.Test)2414 Transaction (org.hibernate.Transaction)1255 List (java.util.List)526 ArrayList (java.util.ArrayList)363 TestForIssue (org.hibernate.testing.TestForIssue)360 Query (org.hibernate.Query)224 Test (org.testng.annotations.Test)143 Iterator (java.util.Iterator)127 Date (java.util.Date)112 Map (java.util.Map)104 BigDecimal (java.math.BigDecimal)97 Criteria (org.hibernate.Criteria)92 HashSet (java.util.HashSet)90 HibernateException (org.hibernate.HibernateException)82 HashMap (java.util.HashMap)70 SQLException (java.sql.SQLException)66 SkipForDialect (org.hibernate.testing.SkipForDialect)66 Connection (java.sql.Connection)65 Serializable (java.io.Serializable)62