Search in sources :

Example 1 with Simple

use of org.hibernate.test.legacy.Simple in project hibernate-orm by hibernate.

the class SQLFunctionsInterSystemsTest method testNothinToUpdate.

public void testNothinToUpdate() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Simple simple = new Simple(Long.valueOf(10));
    simple.setName("Simple 1");
    s.save(simple);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.update(simple);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.update(simple);
    s.delete(simple);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Simple(org.hibernate.test.legacy.Simple)

Example 2 with Simple

use of org.hibernate.test.legacy.Simple in project hibernate-orm by hibernate.

the class SQLFunctionsInterSystemsTest method testCachedQuery.

public void testCachedQuery() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Simple simple = new Simple(Long.valueOf(10));
    simple.setName("Simple 1");
    s.save(simple);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Query q = s.createQuery("from Simple s where s.name=?");
    q.setCacheable(true);
    q.setString(0, "Simple 1");
    assertTrue(q.list().size() == 1);
    assertTrue(q.list().size() == 1);
    assertTrue(q.list().size() == 1);
    q = s.createQuery("from Simple s where s.name=:name");
    q.setCacheable(true);
    q.setString("name", "Simple 1");
    assertTrue(q.list().size() == 1);
    simple = (Simple) q.list().get(0);
    q.setString("name", "Simple 2");
    assertTrue(q.list().size() == 0);
    assertTrue(q.list().size() == 0);
    simple.setName("Simple 2");
    assertTrue(q.list().size() == 1);
    assertTrue(q.list().size() == 1);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    q = s.createQuery("from Simple s where s.name=:name");
    q.setString("name", "Simple 2");
    q.setCacheable(true);
    assertTrue(q.list().size() == 1);
    assertTrue(q.list().size() == 1);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    s.update(simple);
    s.delete(simple);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    q = s.createQuery("from Simple s where s.name=?");
    q.setCacheable(true);
    q.setString(0, "Simple 1");
    assertTrue(q.list().size() == 0);
    assertTrue(q.list().size() == 0);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Session(org.hibernate.Session) Simple(org.hibernate.test.legacy.Simple)

Example 3 with Simple

use of org.hibernate.test.legacy.Simple in project hibernate-orm by hibernate.

the class SQLFunctionsInterSystemsTest method testSqlFunctionAsAlias.

public void testSqlFunctionAsAlias() throws Exception {
    String functionName = locateAppropriateDialectFunctionNameForAliasTest();
    if (functionName == null) {
        log.info("Dialect does not list any no-arg functions");
        return;
    }
    log.info("Using function named [" + functionName + "] for 'function as alias' test");
    String query = "select " + functionName + " from Simple as " + functionName + " where " + functionName + ".id = 10";
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Simple simple = new Simple(Long.valueOf(10));
    simple.setName("Simple 1");
    s.save(simple);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    List result = s.createQuery(query).list();
    assertTrue(result.size() == 1);
    assertTrue(result.get(0) instanceof Simple);
    s.delete(result.get(0));
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Simple(org.hibernate.test.legacy.Simple)

Example 4 with Simple

use of org.hibernate.test.legacy.Simple in project hibernate-orm by hibernate.

the class SQLFunctionsInterSystemsTest method testDialectSQLFunctions.

@Test
public void testDialectSQLFunctions() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Simple simple = new Simple(Long.valueOf(10));
    simple.setName("Simple Dialect Function Test");
    simple.setAddress("Simple Address");
    simple.setPay(new Float(45.8));
    simple.setCount(2);
    s.save(simple);
    // Test to make sure allocating an specified object operates correctly.
    assertTrue(s.createQuery("select new org.hibernate.test.legacy.S(s.count, s.address) from Simple s").list().size() == 1);
    // Quick check the base dialect functions operate correctly
    assertTrue(s.createQuery("select max(s.count) from Simple s").list().size() == 1);
    assertTrue(s.createQuery("select count(*) from Simple s").list().size() == 1);
    List rset = s.createQuery("select s.name, sysdate, floor(s.pay), round(s.pay,0) from Simple s").list();
    assertNotNull("Name string should have been returned", (((Object[]) rset.get(0))[0]));
    assertNotNull("Todays Date should have been returned", (((Object[]) rset.get(0))[1]));
    assertEquals("floor(45.8) result was incorrect ", new Integer(45), ((Object[]) rset.get(0))[2]);
    assertEquals("round(45.8) result was incorrect ", new Float(46), ((Object[]) rset.get(0))[3]);
    simple.setPay(new Float(-45.8));
    s.update(simple);
    // Test type conversions while using nested functions (Float to Int).
    rset = s.createQuery("select abs(round(s.pay,0)) from Simple s").list();
    assertEquals("abs(round(-45.8)) result was incorrect ", new Float(46), rset.get(0));
    // Test a larger depth 3 function example - Not a useful combo other than for testing
    assertTrue(s.createQuery("select floor(round(sysdate,1)) from Simple s").list().size() == 1);
    // Test the oracle standard NVL funtion as a test of multi-param functions...
    simple.setPay(null);
    s.update(simple);
    Double value = (Double) s.createQuery("select mod( nvl(s.pay, 5000), 2 ) from Simple as s where s.id = 10").list().get(0);
    assertTrue(0 == value.intValue());
    // Test the hsql standard MOD funtion as a test of multi-param functions...
    value = (Double) s.createQuery("select MOD(s.count, 2) from Simple as s where s.id = 10").list().get(0);
    assertTrue(0 == value.intValue());
    s.delete(simple);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Simple(org.hibernate.test.legacy.Simple) Test(org.junit.Test)

Example 5 with Simple

use of org.hibernate.test.legacy.Simple in project hibernate-orm by hibernate.

the class SQLFunctionsInterSystemsTest method testSetProperties.

public void testSetProperties() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Simple simple = new Simple(Long.valueOf(10));
    simple.setName("Simple 1");
    s.save(simple);
    Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count");
    q.setProperties(simple);
    assertTrue(q.list().get(0) == simple);
    //misuse of "Single" as a propertyobject, but it was the first testclass i found with a collection ;)
    Single single = new // trivial hack to test properties with arrays.
    Single() {

        @SuppressWarnings({ "unchecked" })
        String[] getStuff() {
            return (String[]) getSeveral().toArray(new String[getSeveral().size()]);
        }
    };
    List l = new ArrayList();
    l.add("Simple 1");
    l.add("Slimeball");
    single.setSeveral(l);
    q = s.createQuery("from Simple s where s.name in (:several)");
    q.setProperties(single);
    assertTrue(q.list().get(0) == simple);
    q = s.createQuery("from Simple s where s.name in (:stuff)");
    q.setProperties(single);
    assertTrue(q.list().get(0) == simple);
    s.delete(simple);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Single(org.hibernate.test.legacy.Single) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Simple(org.hibernate.test.legacy.Simple)

Aggregations

Session (org.hibernate.Session)8 Transaction (org.hibernate.Transaction)8 Simple (org.hibernate.test.legacy.Simple)8 Query (org.hibernate.Query)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 ScrollableResults (org.hibernate.ScrollableResults)1 Single (org.hibernate.test.legacy.Single)1 Test (org.junit.Test)1