Search in sources :

Example 6 with SAPDBDialect

use of org.hibernate.dialect.SAPDBDialect in project hibernate-orm by hibernate.

the class FooBarTest method testQuery.

@Test
public void testQuery() throws Exception {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    Foo foo = new Foo();
    s.save(foo);
    Foo foo2 = new Foo();
    s.save(foo2);
    foo.setFoo(foo2);
    List list = s.createQuery("from Foo foo inner join fetch foo.foo").list();
    Foo foof = (Foo) list.get(0);
    assertTrue(Hibernate.isInitialized(foof.getFoo()));
    s.createQuery("from Baz baz left outer join fetch baz.fooToGlarch").list();
    list = s.createQuery("select foo, bar from Foo foo left outer join foo.foo bar where foo = ?").setParameter(0, foo, s.getTypeHelper().entity(Foo.class)).list();
    Object[] row1 = (Object[]) list.get(0);
    assertTrue(row1[0] == foo && row1[1] == foo2);
    s.createQuery("select foo.foo.foo.string from Foo foo where foo.foo = 'bar'").list();
    s.createQuery("select foo.foo.foo.foo.string from Foo foo where foo.foo = 'bar'").list();
    s.createQuery("select foo from Foo foo where foo.foo.foo = 'bar'").list();
    s.createQuery("select foo.foo.foo.foo.string from Foo foo where foo.foo.foo = 'bar'").list();
    s.createQuery("select foo.foo.foo.string from Foo foo where foo.foo.foo.foo.string = 'bar'").list();
    if (!(getDialect() instanceof HSQLDialect))
        s.createQuery("select foo.string from Foo foo where foo.foo.foo.foo = foo.foo.foo").list();
    s.createQuery("select foo.string from Foo foo where foo.foo.foo = 'bar' and foo.foo.foo.foo = 'baz'").list();
    s.createQuery("select foo.string from Foo foo where foo.foo.foo.foo.string = 'a' and foo.foo.string = 'b'").list();
    s.createQuery("from Bar bar, foo in elements(bar.baz.fooArray)").list();
    if ((getDialect() instanceof DB2Dialect) && !(getDialect() instanceof DerbyDialect)) {
        s.createQuery("from Foo foo where lower( foo.foo.string ) = 'foo'").list();
        s.createQuery("from Foo foo where lower( (foo.foo.string || 'foo') || 'bar' ) = 'foo'").list();
        s.createQuery("from Foo foo where repeat( (foo.foo.string || 'foo') || 'bar', 2 ) = 'foo'").list();
        s.createQuery("from Bar foo where foo.foo.integer is not null and repeat( (foo.foo.string || 'foo') || 'bar', (5+5)/2 ) = 'foo'").list();
        s.createQuery("from Bar foo where foo.foo.integer is not null or repeat( (foo.foo.string || 'foo') || 'bar', (5+5)/2 ) = 'foo'").list();
    }
    if (getDialect() instanceof SybaseDialect) {
        s.createQuery("select baz from Baz as baz join baz.fooArray foo group by baz order by sum(foo.float)").iterate();
    }
    s.createQuery("from Foo as foo where foo.component.glarch.name is not null").list();
    s.createQuery("from Foo as foo left outer join foo.component.glarch as glarch where glarch.name = 'foo'").list();
    list = s.createQuery("from Foo").list();
    assertTrue(list.size() == 2 && list.get(0) instanceof FooProxy);
    list = s.createQuery("from Foo foo left outer join foo.foo").list();
    assertTrue(list.size() == 2 && ((Object[]) list.get(0))[0] instanceof FooProxy);
    s.createQuery("from Bar, Bar").list();
    s.createQuery("from Foo, Bar").list();
    s.createQuery("from Baz baz left join baz.fooToGlarch, Bar bar join bar.foo").list();
    s.createQuery("from Baz baz left join baz.fooToGlarch join baz.fooSet").list();
    s.createQuery("from Baz baz left join baz.fooToGlarch join fetch baz.fooSet foo left join fetch foo.foo").list();
    list = s.createQuery("from Foo foo where foo.string='osama bin laden' and foo.boolean = true order by foo.string asc, foo.component.count desc").list();
    assertTrue("empty query", list.size() == 0);
    Iterator iter = s.createQuery("from Foo foo where foo.string='osama bin laden' order by foo.string asc, foo.component.count desc").iterate();
    assertTrue("empty iterator", !iter.hasNext());
    list = s.createQuery("select foo.foo from Foo foo").list();
    assertTrue("query", list.size() == 1);
    assertTrue("returned object", list.get(0) == foo.getFoo());
    foo.getFoo().setFoo(foo);
    foo.setString("fizard");
    //The following test is disabled for databases with no subselects...also for Interbase (not sure why).
    if (!(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect) && !(getDialect() instanceof DerbyDialect)) {
        // && !db.equals("weblogic") {
        if (!(getDialect() instanceof InterbaseDialect)) {
            list = s.createQuery("from Foo foo where ? = some elements(foo.component.importantDates)").setParameter(0, foo.getTimestamp(), StandardBasicTypes.DATE).list();
            assertTrue("component query", list.size() == 2);
        }
        if (!(getDialect() instanceof TimesTenDialect)) {
            //WAS: 4
            list = s.createQuery("from Foo foo where size(foo.component.importantDates) = 3").list();
            assertTrue("component query", list.size() == 2);
            list = s.createQuery("from Foo foo where 0 = size(foo.component.importantDates)").list();
            assertTrue("component query", list.size() == 0);
        }
        list = s.createQuery("from Foo foo where exists elements(foo.component.importantDates)").list();
        assertTrue("component query", list.size() == 2);
        s.createQuery("from Foo foo where not exists (from Bar bar where bar.id = foo.id)").list();
        s.createQuery("select foo.foo from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long)").list();
        s.createQuery("select foo.foo from Foo foo where foo = some(from Foo x where (x.long > foo.foo.long))").list();
        if (!(getDialect() instanceof TimesTenDialect)) {
            s.createQuery("select foo.foo from Foo foo where foo.long = some( select max(x.long) from Foo x where (x.long > foo.foo.long) group by x.foo )").list();
        }
        s.createQuery("from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long) and foo.foo.string='baz'").list();
        s.createQuery("from Foo foo where foo.foo.string='baz' and foo = some(select x from Foo x where x.long > foo.foo.long)").list();
        s.createQuery("from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long)").list();
        s.createQuery("select foo.string, foo.date, foo.foo.string, foo.id from Foo foo, Baz baz where foo in elements(baz.fooArray) and foo.string like 'foo'").iterate();
    }
    list = s.createQuery("from Foo foo where foo.component.count is null order by foo.component.count").list();
    assertTrue("component query", list.size() == 0);
    list = s.createQuery("from Foo foo where foo.component.name='foo'").list();
    assertTrue("component query", list.size() == 2);
    list = s.createQuery("select distinct foo.component.name, foo.component.name from Foo foo where foo.component.name='foo'").list();
    assertTrue("component query", list.size() == 1);
    list = s.createQuery("select distinct foo.component.name, foo.id from Foo foo where foo.component.name='foo'").list();
    assertTrue("component query", list.size() == 2);
    list = s.createQuery("select foo.foo from Foo foo").list();
    assertTrue("query", list.size() == 2);
    list = s.createQuery("from Foo foo where foo.id=?").setParameter(0, foo.getKey(), StandardBasicTypes.STRING).list();
    assertTrue("id query", list.size() == 1);
    list = s.createQuery("from Foo foo where foo.key=?").setParameter(0, foo.getKey(), StandardBasicTypes.STRING).list();
    assertTrue("named id query", list.size() == 1);
    assertTrue("id query", list.get(0) == foo);
    list = s.createQuery("select foo.foo from Foo foo where foo.string='fizard'").list();
    assertTrue("query", list.size() == 1);
    assertTrue("returned object", list.get(0) == foo.getFoo());
    list = s.createQuery("from Foo foo where foo.component.subcomponent.name='bar'").list();
    assertTrue("components of components", list.size() == 2);
    list = s.createQuery("select foo.foo from Foo foo where foo.foo.id=?").setParameter(0, foo.getFoo().getKey(), StandardBasicTypes.STRING).list();
    assertTrue("by id query", list.size() == 1);
    assertTrue("by id returned object", list.get(0) == foo.getFoo());
    s.createQuery("from Foo foo where foo.foo = ?").setParameter(0, foo.getFoo(), s.getTypeHelper().entity(Foo.class)).list();
    assertTrue(!s.createQuery("from Bar bar where bar.string='a string' or bar.string='a string'").iterate().hasNext());
    iter = s.createQuery("select foo.component.name, elements(foo.component.importantDates) from Foo foo where foo.foo.id=?").setParameter(0, foo.getFoo().getKey(), StandardBasicTypes.STRING).iterate();
    int i = 0;
    while (iter.hasNext()) {
        i++;
        Object[] row = (Object[]) iter.next();
        assertTrue(row[0] instanceof String && (row[1] == null || row[1] instanceof Date));
    }
    //WAS: 4
    assertTrue(i == 3);
    iter = s.createQuery("select max( elements(foo.component.importantDates) ) from Foo foo group by foo.id").iterate();
    assertTrue(iter.next() instanceof Date);
    list = s.createQuery("select foo.foo.foo.foo from Foo foo, Foo foo2 where" + " foo = foo2.foo and not not ( not foo.string='fizard' )" + " and foo2.string between 'a' and (foo.foo.string)" + ((getDialect() instanceof HSQLDialect || getDialect() instanceof InterbaseDialect || getDialect() instanceof TimesTenDialect || getDialect() instanceof TeradataDialect) ? " and ( foo2.string in ( 'fiz', 'blah') or 1=1 )" : " and ( foo2.string in ( 'fiz', 'blah', foo.foo.string, foo.string, foo2.string ) )")).list();
    assertTrue("complex query", list.size() == 1);
    assertTrue("returned object", list.get(0) == foo);
    foo.setString("from BoogieDown  -tinsel town  =!@#$^&*())");
    list = s.createQuery("from Foo foo where foo.string='from BoogieDown  -tinsel town  =!@#$^&*())'").list();
    assertTrue("single quotes", list.size() == 1);
    list = s.createQuery("from Foo foo where not foo.string='foo''bar'").list();
    assertTrue("single quotes", list.size() == 2);
    list = s.createQuery("from Foo foo where foo.component.glarch.next is null").list();
    assertTrue("query association in component", list.size() == 2);
    Bar bar = new Bar();
    Baz baz = new Baz();
    baz.setDefaults();
    bar.setBaz(baz);
    baz.setManyToAny(new ArrayList());
    baz.getManyToAny().add(bar);
    baz.getManyToAny().add(foo);
    s.save(bar);
    s.save(baz);
    list = s.createQuery(" from Bar bar where bar.baz.count=667 and bar.baz.count!=123 and not bar.baz.name='1-E-1'").list();
    assertTrue("query many-to-one", list.size() == 1);
    list = s.createQuery(" from Bar i where i.baz.name='Bazza'").list();
    assertTrue("query many-to-one", list.size() == 1);
    Iterator rs = s.createQuery("select count(distinct foo.foo) from Foo foo").iterate();
    assertTrue("count", ((Long) rs.next()).longValue() == 2);
    assertTrue(!rs.hasNext());
    rs = s.createQuery("select count(foo.foo.boolean) from Foo foo").iterate();
    assertTrue("count", ((Long) rs.next()).longValue() == 2);
    assertTrue(!rs.hasNext());
    rs = s.createQuery("select count(*), foo.int from Foo foo group by foo.int").iterate();
    assertTrue("count(*) group by", ((Object[]) rs.next())[0].equals(new Long(3)));
    assertTrue(!rs.hasNext());
    rs = s.createQuery("select sum(foo.foo.int) from Foo foo").iterate();
    assertTrue("sum", ((Long) rs.next()).longValue() == 4);
    assertTrue(!rs.hasNext());
    rs = s.createQuery("select count(foo) from Foo foo where foo.id=?").setParameter(0, foo.getKey(), StandardBasicTypes.STRING).iterate();
    assertTrue("id query count", ((Long) rs.next()).longValue() == 1);
    assertTrue(!rs.hasNext());
    s.createQuery("from Foo foo where foo.boolean = ?").setParameter(0, new Boolean(true), StandardBasicTypes.BOOLEAN).list();
    s.createQuery("select new Foo(fo.x) from Fo fo").list();
    s.createQuery("select new Foo(fo.integer) from Foo fo").list();
    list = s.createQuery("select new Foo(fo.x) from Foo fo").setCacheable(true).list();
    assertTrue(list.size() == 3);
    list = s.createQuery("select new Foo(fo.x) from Foo fo").setCacheable(true).list();
    assertTrue(list.size() == 3);
    rs = s.createQuery("select new Foo(fo.x) from Foo fo").iterate();
    assertTrue("projection iterate (results)", rs.hasNext());
    assertTrue("projection iterate (return check)", Foo.class.isAssignableFrom(rs.next().getClass()));
    ScrollableResults sr = s.createQuery("select new Foo(fo.x) from Foo fo").scroll();
    assertTrue("projection scroll (results)", sr.next());
    assertTrue("projection scroll (return check)", Foo.class.isAssignableFrom(sr.get(0).getClass()));
    list = s.createQuery("select foo.long, foo.component.name, foo, foo.foo from Foo foo").list();
    rs = list.iterator();
    int count = 0;
    while (rs.hasNext()) {
        count++;
        Object[] row = (Object[]) rs.next();
        assertTrue(row[0] instanceof Long);
        assertTrue(row[1] instanceof String);
        assertTrue(row[2] instanceof Foo);
        assertTrue(row[3] instanceof Foo);
    }
    assertTrue(count != 0);
    list = s.createQuery("select avg(foo.float), max(foo.component.name), count(distinct foo.id) from Foo foo").list();
    rs = list.iterator();
    count = 0;
    while (rs.hasNext()) {
        count++;
        Object[] row = (Object[]) rs.next();
        assertTrue(row[0] instanceof Double);
        assertTrue(row[1] instanceof String);
        assertTrue(row[2] instanceof Long);
    }
    assertTrue(count != 0);
    list = s.createQuery("select foo.long, foo.component, foo, foo.foo from Foo foo").list();
    rs = list.iterator();
    count = 0;
    while (rs.hasNext()) {
        count++;
        Object[] row = (Object[]) rs.next();
        assertTrue(row[0] instanceof Long);
        assertTrue(row[1] instanceof FooComponent);
        assertTrue(row[2] instanceof Foo);
        assertTrue(row[3] instanceof Foo);
    }
    assertTrue(count != 0);
    s.save(new Holder("ice T"));
    s.save(new Holder("ice cube"));
    assertTrue(s.createQuery("from java.lang.Object as o").list().size() == 15);
    assertTrue(s.createQuery("from Named").list().size() == 7);
    assertTrue(s.createQuery("from Named n where n.name is not null").list().size() == 4);
    iter = s.createQuery("from Named n").iterate();
    while (iter.hasNext()) {
        assertTrue(iter.next() instanceof Named);
    }
    s.save(new Holder("bar"));
    iter = s.createQuery("from Named n0, Named n1 where n0.name = n1.name").iterate();
    int cnt = 0;
    while (iter.hasNext()) {
        Object[] row = (Object[]) iter.next();
        if (row[0] != row[1])
            cnt++;
    }
    if (!(getDialect() instanceof HSQLDialect)) {
        assertTrue(cnt == 2);
        assertTrue(s.createQuery("from Named n0, Named n1 where n0.name = n1.name").list().size() == 7);
    }
    Query qu = s.createQuery("from Named n where n.name = :name");
    qu.getReturnTypes();
    qu.getNamedParameters();
    iter = s.createQuery("from java.lang.Object").iterate();
    int c = 0;
    while (iter.hasNext()) {
        iter.next();
        c++;
    }
    assertTrue(c == 16);
    s.createQuery("select baz.code, min(baz.count) from Baz baz group by baz.code").iterate();
    iter = s.createQuery("selecT baz from Baz baz where baz.stringDateMap['foo'] is not null or baz.stringDateMap['bar'] = ?").setParameter(0, new Date(), StandardBasicTypes.DATE).iterate();
    assertFalse(iter.hasNext());
    list = s.createQuery("select baz from Baz baz where baz.stringDateMap['now'] is not null").list();
    assertTrue(list.size() == 1);
    list = s.createQuery("select baz from Baz baz where baz.stringDateMap['now'] is not null and baz.stringDateMap['big bang'] < baz.stringDateMap['now']").list();
    assertTrue(list.size() == 1);
    list = s.createQuery("select index(date) from Baz baz join baz.stringDateMap date").list();
    System.out.println(list);
    assertTrue(list.size() == 2);
    s.createQuery("from Foo foo where foo.integer not between 1 and 5 and foo.string not in ('cde', 'abc') and foo.string is not null and foo.integer<=3").list();
    s.createQuery("from Baz baz inner join baz.collectionComponent.nested.foos foo where foo.string is null").list();
    if (!(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect)) {
        s.createQuery("from Baz baz inner join baz.fooSet where '1' in (from baz.fooSet foo where foo.string is not null)").list();
        s.createQuery("from Baz baz where 'a' in elements(baz.collectionComponent.nested.foos) and 1.0 in elements(baz.collectionComponent.nested.floats)").list();
        s.createQuery("from Baz baz where 'b' in elements(baz.collectionComponent.nested.foos) and 1.0 in elements(baz.collectionComponent.nested.floats)").list();
    }
    s.createQuery("from Foo foo join foo.foo where foo.foo in ('1','2','3')").list();
    if (!(getDialect() instanceof HSQLDialect))
        s.createQuery("from Foo foo left join foo.foo where foo.foo in ('1','2','3')").list();
    s.createQuery("select foo.foo from Foo foo where foo.foo in ('1','2','3')").list();
    s.createQuery("select foo.foo.string from Foo foo where foo.foo in ('1','2','3')").list();
    s.createQuery("select foo.foo.string from Foo foo where foo.foo.string in ('1','2','3')").list();
    s.createQuery("select foo.foo.long from Foo foo where foo.foo.string in ('1','2','3')").list();
    s.createQuery("select count(*) from Foo foo where foo.foo.string in ('1','2','3') or foo.foo.long in (1,2,3)").list();
    s.createQuery("select count(*) from Foo foo where foo.foo.string in ('1','2','3') group by foo.foo.long").list();
    s.createQuery("from Foo foo1 left join foo1.foo foo2 left join foo2.foo where foo1.string is not null").list();
    s.createQuery("from Foo foo1 left join foo1.foo.foo where foo1.string is not null").list();
    s.createQuery("from Foo foo1 left join foo1.foo foo2 left join foo1.foo.foo foo3 where foo1.string is not null").list();
    s.createQuery("select foo.formula from Foo foo where foo.formula > 0").list();
    int len = s.createQuery("from Foo as foo join foo.foo as foo2 where foo2.id >'a' or foo2.id <'a'").list().size();
    assertTrue(len == 2);
    for (Object entity : s.createQuery("from Holder").list()) {
        s.delete(entity);
    }
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    baz = (Baz) s.createQuery("from Baz baz left outer join fetch baz.manyToAny").uniqueResult();
    assertTrue(Hibernate.isInitialized(baz.getManyToAny()));
    assertTrue(baz.getManyToAny().size() == 2);
    BarProxy barp = (BarProxy) baz.getManyToAny().get(0);
    s.createQuery("from Baz baz join baz.manyToAny").list();
    assertTrue(s.createQuery("select baz from Baz baz join baz.manyToAny a where index(a) = 0").list().size() == 1);
    FooProxy foop = (FooProxy) s.get(Foo.class, foo.getKey());
    assertTrue(foop == baz.getManyToAny().get(1));
    barp.setBaz(baz);
    assertTrue(s.createQuery("select bar from Bar bar where bar.baz.stringDateMap['now'] is not null").list().size() == 1);
    assertTrue(s.createQuery("select bar from Bar bar join bar.baz b where b.stringDateMap['big bang'] < b.stringDateMap['now'] and b.stringDateMap['now'] is not null").list().size() == 1);
    assertTrue(s.createQuery("select bar from Bar bar where bar.baz.stringDateMap['big bang'] < bar.baz.stringDateMap['now'] and bar.baz.stringDateMap['now'] is not null").list().size() == 1);
    list = s.createQuery("select foo.string, foo.component, foo.id from Bar foo").list();
    assertTrue(((FooComponent) ((Object[]) list.get(0))[1]).getName().equals("foo"));
    list = s.createQuery("select elements(baz.components) from Baz baz").list();
    assertTrue(list.size() == 2);
    list = s.createQuery("select bc.name from Baz baz join baz.components bc").list();
    assertTrue(list.size() == 2);
    //list = s.find("select bc from Baz baz join baz.components bc");
    s.createQuery("from Foo foo where foo.integer < 10 order by foo.string").setMaxResults(12).list();
    s.delete(barp);
    s.delete(baz);
    s.delete(foop.getFoo());
    s.delete(foop);
    txn.commit();
    s.close();
}
Also used : TimesTenDialect(org.hibernate.dialect.TimesTenDialect) Query(org.hibernate.Query) ArrayList(java.util.ArrayList) SybaseDialect(org.hibernate.dialect.SybaseDialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) TeradataDialect(org.hibernate.dialect.TeradataDialect) List(java.util.List) ArrayList(java.util.ArrayList) ScrollableResults(org.hibernate.ScrollableResults) HSQLDialect(org.hibernate.dialect.HSQLDialect) MckoiDialect(org.hibernate.dialect.MckoiDialect) DerbyDialect(org.hibernate.dialect.DerbyDialect) InterbaseDialect(org.hibernate.dialect.InterbaseDialect) PointbaseDialect(org.hibernate.dialect.PointbaseDialect) DB2Dialect(org.hibernate.dialect.DB2Dialect) Date(java.util.Date) SAPDBDialect(org.hibernate.dialect.SAPDBDialect) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with SAPDBDialect

use of org.hibernate.dialect.SAPDBDialect in project hibernate-orm by hibernate.

the class FooBarTest method testAutoFlushCollections.

@Test
public void testAutoFlushCollections() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Baz baz = new Baz();
    baz.setDefaults();
    s.save(baz);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    baz = (Baz) s.load(Baz.class, baz.getCode());
    baz.getStringArray()[0] = "bark";
    Iterator i = s.createQuery("select elements(baz.stringArray) from Baz baz").iterate();
    boolean found = false;
    while (i.hasNext()) {
        if ("bark".equals(i.next()))
            found = true;
    }
    assertTrue(found);
    baz.setStringArray(null);
    i = s.createQuery("select distinct elements(baz.stringArray) from Baz baz").iterate();
    assertTrue(!i.hasNext());
    baz.setStringArray(new String[] { "foo", "bar" });
    i = s.createQuery("select elements(baz.stringArray) from Baz baz").iterate();
    assertTrue(i.hasNext());
    Foo foo = new Foo();
    s.save(foo);
    s.flush();
    baz.setFooArray(new Foo[] { foo });
    i = s.createQuery("select foo from Baz baz join baz.fooArray foo").iterate();
    found = false;
    while (i.hasNext()) {
        if (foo == i.next())
            found = true;
    }
    assertTrue(found);
    baz.getFooArray()[0] = null;
    i = s.createQuery("select foo from Baz baz join baz.fooArray foo").iterate();
    assertTrue(!i.hasNext());
    baz.getFooArray()[0] = foo;
    i = s.createQuery("select elements(baz.fooArray) from Baz baz").iterate();
    assertTrue(i.hasNext());
    if (!(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof InterbaseDialect) && !(getDialect() instanceof PointbaseDialect) && !(getDialect() instanceof SAPDBDialect)) {
        baz.getFooArray()[0] = null;
        i = s.createQuery("from Baz baz where ? in elements(baz.fooArray)").setParameter(0, foo, s.getTypeHelper().entity(Foo.class)).iterate();
        assertTrue(!i.hasNext());
        baz.getFooArray()[0] = foo;
        i = s.createQuery("select foo from Foo foo where foo in (select elt from Baz baz join baz.fooArray elt)").iterate();
        assertTrue(i.hasNext());
    }
    s.delete(foo);
    s.delete(baz);
    tx.commit();
    s.close();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) SAPDBDialect(org.hibernate.dialect.SAPDBDialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) Transaction(org.hibernate.Transaction) InterbaseDialect(org.hibernate.dialect.InterbaseDialect) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) PointbaseDialect(org.hibernate.dialect.PointbaseDialect) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with SAPDBDialect

use of org.hibernate.dialect.SAPDBDialect in project hibernate-orm by hibernate.

the class FooBarTest method testNamedParams.

@Test
public void testNamedParams() throws Exception {
    Bar bar = new Bar();
    Bar bar2 = new Bar();
    bar.setName("Bar");
    bar2.setName("Bar Two");
    bar.setX(10);
    bar2.setX(1000);
    Baz baz = new Baz();
    baz.setCascadingBars(new HashSet());
    baz.getCascadingBars().add(bar);
    bar.setBaz(baz);
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    s.save(baz);
    s.save(bar2);
    List list = s.createQuery("from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar %'").list();
    Object row = list.iterator().next();
    assertTrue(row instanceof Object[] && ((Object[]) row).length == 3);
    Query q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar%'");
    list = q.list();
    if (!(getDialect() instanceof SAPDBDialect))
        assertTrue(list.size() == 2);
    q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where ( bar.name in (:nameList) or bar.name in (:nameList) ) and bar.string = :stringVal");
    HashSet nameList = new HashSet();
    nameList.add("bar");
    nameList.add("Bar");
    nameList.add("Bar Two");
    q.setParameterList("nameList", nameList);
    q.setParameter("stringVal", "a string");
    list = q.list();
    if (!(getDialect() instanceof SAPDBDialect))
        assertTrue(list.size() == 2);
    try {
        q.setParameterList("nameList", (Collection) null);
        fail("Should throw an queryexception when passing a null!");
    } catch (IllegalArgumentException qe) {
    //should happen
    }
    q = s.createQuery("select bar, b from Bar bar inner join bar.baz baz inner join baz.cascadingBars b where bar.name like 'Bar%'");
    Object result = q.uniqueResult();
    assertTrue(result != null);
    q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like :name and b.name like :name");
    q.setString("name", "Bar%");
    list = q.list();
    assertTrue(list.size() == 1);
    // This test added for issue HB-297 - there is an named parameter in the Order By clause
    q = s.createQuery("select bar from Bar bar order by ((bar.x - :valueX)*(bar.x - :valueX))");
    q.setInteger("valueX", bar.getX() + 1);
    list = q.list();
    assertTrue(((Bar) list.get(0)).getX() == bar.getX());
    q.setInteger("valueX", bar2.getX() + 1);
    list = q.list();
    assertTrue(((Bar) list.get(0)).getX() == bar2.getX());
    s.delete(baz);
    s.delete(bar2);
    txn.commit();
    s.close();
}
Also used : SAPDBDialect(org.hibernate.dialect.SAPDBDialect) Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)8 SAPDBDialect (org.hibernate.dialect.SAPDBDialect)8 Test (org.junit.Test)8 Transaction (org.hibernate.Transaction)7 MySQLDialect (org.hibernate.dialect.MySQLDialect)7 HSQLDialect (org.hibernate.dialect.HSQLDialect)6 ArrayList (java.util.ArrayList)5 Iterator (java.util.Iterator)5 List (java.util.List)5 PointbaseDialect (org.hibernate.dialect.PointbaseDialect)5 JoinedIterator (org.hibernate.internal.util.collections.JoinedIterator)4 Query (org.hibernate.Query)3 MckoiDialect (org.hibernate.dialect.MckoiDialect)3 Serializable (java.io.Serializable)2 HashSet (java.util.HashSet)2 DB2Dialect (org.hibernate.dialect.DB2Dialect)2 InterbaseDialect (org.hibernate.dialect.InterbaseDialect)2 SybaseDialect (org.hibernate.dialect.SybaseDialect)2 TimesTenDialect (org.hibernate.dialect.TimesTenDialect)2 Connection (java.sql.Connection)1