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