Search in sources :

Example 21 with MySQLDialect

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

the class SQLLoaderTest method testFindBySQLMultipleObject.

@Test
public void testFindBySQLMultipleObject() throws HibernateException, SQLException {
    Session s = openSession();
    s.beginTransaction();
    for (Object entity : s.createQuery("from Assignable").list()) {
        s.delete(entity);
    }
    for (Object entity : s.createQuery("from Category").list()) {
        s.delete(entity);
    }
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    Category c = new Category();
    c.setName("NAME");
    Assignable assn = new Assignable();
    assn.setId("i.d.");
    List l = new ArrayList();
    l.add(c);
    assn.setCategories(l);
    c.setAssignable(assn);
    s.save(assn);
    s.flush();
    c = new Category();
    c.setName("NAME2");
    assn = new Assignable();
    assn.setId("i.d.2");
    l = new ArrayList();
    l.add(c);
    assn.setCategories(l);
    c.setAssignable(assn);
    s.save(assn);
    s.flush();
    assn = new Assignable();
    assn.setId("i.d.3");
    s.save(assn);
    s.getTransaction().commit();
    s.close();
    if (getDialect() instanceof MySQLDialect) {
        return;
    }
    s = openSession();
    s.beginTransaction();
    String sql = "select {category.*}, {assignable.*} from category {category}, \"assign-able\" {assignable}";
    List list = s.createSQLQuery(sql).addEntity("category", Category.class).addEntity("assignable", Assignable.class).list();
    // crossproduct of 2 categories x 3 assignables
    assertTrue(list.size() == 6);
    assertTrue(list.get(0) instanceof Object[]);
    s.getTransaction().commit();
    s.close();
}
Also used : MySQLDialect(org.hibernate.dialect.MySQLDialect) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 22 with MySQLDialect

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

the class FooBarTest method testCollectionsInSelect.

@Test
public void testCollectionsInSelect() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Foo[] foos = new Foo[] { null, new Foo() };
    s.save(foos[1]);
    Baz baz = new Baz();
    baz.setDefaults();
    baz.setFooArray(foos);
    s.save(baz);
    Baz baz2 = new Baz();
    baz2.setDefaults();
    s.save(baz2);
    Bar bar = new Bar();
    bar.setBaz(baz);
    s.save(bar);
    List list = s.createQuery("select new Result(foo.string, foo.long, foo.integer) from Foo foo").list();
    assertTrue(list.size() == 2 && (list.get(0) instanceof Result) && (list.get(1) instanceof Result));
    /*list = s.find("select new Result( baz.name, foo.long, count(elements(baz.fooArray)) ) from Baz baz join baz.fooArray foo group by baz.name, foo.long");
		assertTrue( list.size()==1 && ( list.get(0) instanceof Result ) );
		Result r = ((Result) list.get(0) );
		assertEquals( r.getName(), baz.getName() );
		assertEquals( r.getCount(), 1 );
		assertEquals( r.getAmount(), foos[1].getLong().longValue() );*/
    list = s.createQuery("select new Result( baz.name, max(foo.long), count(foo) ) from Baz baz join baz.fooArray foo group by baz.name").list();
    assertTrue(list.size() == 1 && (list.get(0) instanceof Result));
    Result r = ((Result) list.get(0));
    assertEquals(r.getName(), baz.getName());
    assertEquals(r.getCount(), 1);
    assertTrue(r.getAmount() > 696969696969696000l);
    //The following test is disabled for databases with no subselects...also for Interbase (not sure why).
    if (!(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && /*&& !(dialect instanceof MckoiDialect)*/
    !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect)) {
        s.createQuery("select count(*) from Baz as baz where 1 in indices(baz.fooArray)").list();
        s.createQuery("select count(*) from Bar as bar where 'abc' in elements(bar.baz.fooArray)").list();
        s.createQuery("select count(*) from Bar as bar where 1 in indices(bar.baz.fooArray)").list();
        if (!(getDialect() instanceof DB2Dialect) && !(getDialect() instanceof Oracle8iDialect) && !(getDialect() instanceof SybaseDialect) && !(getDialect() instanceof Sybase11Dialect) && !(getDialect() instanceof SybaseASE15Dialect) && !(getDialect() instanceof PostgreSQLDialect) && !(getDialect() instanceof PostgreSQL81Dialect) && !(getDialect() instanceof AbstractHANADialect)) {
            // SybaseAnywhereDialect supports implicit conversions from strings to ints
            s.createQuery("select count(*) from Bar as bar, bar.component.glarch.proxyArray as g where g.id in indices(bar.baz.fooArray)").list();
            s.createQuery("select max( elements(bar.baz.fooArray) ) from Bar as bar, bar.component.glarch.proxyArray as g where g.id in indices(bar.baz.fooArray)").list();
        }
        s.createQuery("select count(*) from Bar as bar where '1' in (from bar.component.glarch.proxyArray g where g.name='foo')").list();
        s.createQuery("select count(*) from Bar as bar where '1' in (from bar.component.glarch.proxyArray g where g.name='foo')").list();
        s.createQuery("select count(*) from Bar as bar left outer join bar.component.glarch.proxyArray as pg where '1' in (from bar.component.glarch.proxyArray)").list();
    }
    list = s.createQuery("from Baz baz left join baz.fooToGlarch join fetch baz.fooArray foo left join fetch foo.foo").list();
    assertTrue(list.size() == 1 && ((Object[]) list.get(0)).length == 2);
    s.createQuery("select baz.name from Bar bar inner join bar.baz baz inner join baz.fooSet foo where baz.name = bar.string").list();
    s.createQuery("SELECT baz.name FROM Bar AS bar INNER JOIN bar.baz AS baz INNER JOIN baz.fooSet AS foo WHERE baz.name = bar.string").list();
    if (!(getDialect() instanceof HSQLDialect))
        s.createQuery("select baz.name from Bar bar join bar.baz baz left outer join baz.fooSet foo where baz.name = bar.string").list();
    s.createQuery("select baz.name from Bar bar join bar.baz baz join baz.fooSet foo where baz.name = bar.string").list();
    s.createQuery("SELECT baz.name FROM Bar AS bar JOIN bar.baz AS baz JOIN baz.fooSet AS foo WHERE baz.name = bar.string").list();
    if (!(getDialect() instanceof HSQLDialect)) {
        s.createQuery("select baz.name from Bar bar left join bar.baz baz left join baz.fooSet foo where baz.name = bar.string").list();
        s.createQuery("select foo.string from Bar bar left join bar.baz.fooSet foo where bar.string = foo.string").list();
    }
    s.createQuery("select baz.name from Bar bar left join bar.baz baz left join baz.fooArray foo where baz.name = bar.string").list();
    s.createQuery("select foo.string from Bar bar left join bar.baz.fooArray foo where bar.string = foo.string").list();
    s.createQuery("select bar.string, foo.string from Bar bar inner join bar.baz as baz inner join baz.fooSet as foo where baz.name = 'name'").list();
    s.createQuery("select foo from Bar bar inner join bar.baz as baz inner join baz.fooSet as foo").list();
    s.createQuery("select foo from Bar bar inner join bar.baz.fooSet as foo").list();
    s.createQuery("select bar.string, foo.string from Bar bar join bar.baz as baz join baz.fooSet as foo where baz.name = 'name'").list();
    s.createQuery("select foo from Bar bar join bar.baz as baz join baz.fooSet as foo").list();
    s.createQuery("select foo from Bar bar join bar.baz.fooSet as foo").list();
    assertTrue(s.createQuery("from Bar bar join bar.baz.fooArray foo").list().size() == 1);
    assertTrue(s.createQuery("from Bar bar join bar.baz.fooSet foo").list().size() == 0);
    assertTrue(s.createQuery("from Bar bar join bar.baz.fooArray foo").list().size() == 1);
    s.delete(bar);
    if (getDialect() instanceof DB2Dialect || getDialect() instanceof PostgreSQLDialect || getDialect() instanceof PostgreSQL81Dialect) {
        s.createQuery("select one from One one join one.manies many group by one order by count(many)").iterate();
        s.createQuery("select one from One one join one.manies many group by one having count(many) < 5").iterate();
    }
    s.createQuery("from One one join one.manies many where one.id = 1 and many.id = 1").list();
    s.createQuery("select one.id, elements(one.manies) from One one").iterate();
    s.createQuery("select max( elements(one.manies) ) from One one").iterate();
    s.createQuery("select one, elements(one.manies) from One one").list();
    Iterator iter = s.createQuery("select elements(baz.fooArray) from Baz baz where baz.id=?").setParameter(0, baz.getCode(), StandardBasicTypes.STRING).iterate();
    assertTrue(iter.next() == foos[1] && !iter.hasNext());
    list = s.createQuery("select elements(baz.fooArray) from Baz baz where baz.id=?").setParameter(0, baz.getCode(), StandardBasicTypes.STRING).list();
    assertEquals(1, list.size());
    iter = s.createQuery("select indices(baz.fooArray) from Baz baz where baz.id=?").setParameter(0, baz.getCode(), StandardBasicTypes.STRING).iterate();
    assertTrue(iter.next().equals(new Integer(1)) && !iter.hasNext());
    iter = s.createQuery("select size(baz.stringSet) from Baz baz where baz.id=?").setParameter(0, baz.getCode(), StandardBasicTypes.STRING).iterate();
    assertEquals(new Integer(3), iter.next());
    s.createQuery("from Foo foo where foo.component.glarch.id is not null").list();
    iter = s.createQuery("select baz, size(baz.stringSet), count( distinct elements(baz.stringSet) ), max( elements(baz.stringSet) ) from Baz baz group by baz").iterate();
    while (iter.hasNext()) {
        Object[] arr = (Object[]) iter.next();
        log.info(arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3]);
    }
    s.delete(baz);
    s.delete(baz2);
    s.delete(foos[1]);
    t.commit();
    s.close();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) SybaseASE15Dialect(org.hibernate.dialect.SybaseASE15Dialect) SybaseDialect(org.hibernate.dialect.SybaseDialect) PointbaseDialect(org.hibernate.dialect.PointbaseDialect) DB2Dialect(org.hibernate.dialect.DB2Dialect) Oracle8iDialect(org.hibernate.dialect.Oracle8iDialect) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) SAPDBDialect(org.hibernate.dialect.SAPDBDialect) AbstractHANADialect(org.hibernate.dialect.AbstractHANADialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) PostgreSQLDialect(org.hibernate.dialect.PostgreSQLDialect) Transaction(org.hibernate.Transaction) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) Sybase11Dialect(org.hibernate.dialect.Sybase11Dialect) List(java.util.List) ArrayList(java.util.ArrayList) Session(org.hibernate.Session) Test(org.junit.Test)

Example 23 with MySQLDialect

use of org.hibernate.dialect.MySQLDialect 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 24 with MySQLDialect

use of org.hibernate.dialect.MySQLDialect 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 25 with MySQLDialect

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

the class MasterDetailTest method testSelfManyToOne.

@Test
public void testSelfManyToOne() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Master m = new Master();
    m.setOtherMaster(m);
    s.save(m);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Iterator i = s.createQuery("from Master").iterate();
    m = (Master) i.next();
    assertTrue(m.getOtherMaster() == m);
    if (getDialect() instanceof HSQLDialect || getDialect() instanceof MySQLDialect) {
        m.setOtherMaster(null);
        s.flush();
    }
    s.delete(m);
    t.commit();
    s.close();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

MySQLDialect (org.hibernate.dialect.MySQLDialect)27 Session (org.hibernate.Session)26 Test (org.junit.Test)26 Transaction (org.hibernate.Transaction)18 HSQLDialect (org.hibernate.dialect.HSQLDialect)15 List (java.util.List)12 ArrayList (java.util.ArrayList)11 Iterator (java.util.Iterator)10 PointbaseDialect (org.hibernate.dialect.PointbaseDialect)7 SAPDBDialect (org.hibernate.dialect.SAPDBDialect)7 Serializable (java.io.Serializable)5 Query (org.hibernate.Query)5 DB2Dialect (org.hibernate.dialect.DB2Dialect)5 MckoiDialect (org.hibernate.dialect.MckoiDialect)5 Date (java.util.Date)4 AbstractHANADialect (org.hibernate.dialect.AbstractHANADialect)4 PostgreSQL81Dialect (org.hibernate.dialect.PostgreSQL81Dialect)4 TimesTenDialect (org.hibernate.dialect.TimesTenDialect)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3