Search in sources :

Example 1 with AbstractHANADialect

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

the class ASTParserLoadingTest method testExpressionWithParamInFunction.

@Test
public void testExpressionWithParamInFunction() {
    Session s = openSession();
    s.beginTransaction();
    s.createQuery("from Animal a where abs(a.bodyWeight-:param) < 2.0").setLong("param", 1).list();
    s.createQuery("from Animal a where abs(:param - a.bodyWeight) < 2.0").setLong("param", 1).list();
    if ((getDialect() instanceof HSQLDialect) || (getDialect() instanceof DB2Dialect)) {
        // HSQLDB and DB2 don't like the abs(? - ?) syntax. bit work if at least one parameter is typed...
        s.createQuery("from Animal where abs(cast(:x as long) - :y) < 2.0").setLong("x", 1).setLong("y", 1).list();
        s.createQuery("from Animal where abs(:x - cast(:y as long)) < 2.0").setLong("x", 1).setLong("y", 1).list();
        s.createQuery("from Animal where abs(cast(:x as long) - cast(:y as long)) < 2.0").setLong("x", 1).setLong("y", 1).list();
    } else {
        s.createQuery("from Animal where abs(:x - :y) < 2.0").setLong("x", 1).setLong("y", 1).list();
    }
    if (getDialect() instanceof DB2Dialect) {
        s.createQuery("from Animal where lower(upper(cast(:foo as string))) like 'f%'").setString("foo", "foo").list();
    } else {
        s.createQuery("from Animal where lower(upper(:foo)) like 'f%'").setString("foo", "foo").list();
    }
    s.createQuery("from Animal a where abs(abs(a.bodyWeight - 1.0 + :param) * abs(length('ffobar')-3)) = 3.0").setLong("param", 1).list();
    if (getDialect() instanceof DB2Dialect) {
        s.createQuery("from Animal where lower(upper('foo') || upper(cast(:bar as string))) like 'f%'").setString("bar", "xyz").list();
    } else {
        s.createQuery("from Animal where lower(upper('foo') || upper(:bar)) like 'f%'").setString("bar", "xyz").list();
    }
    if (getDialect() instanceof AbstractHANADialect) {
        s.createQuery("from Animal where abs(cast(1 as double) - cast(:param as double)) = 1.0").setLong("param", 1).list();
    } else if (!(getDialect() instanceof PostgreSQLDialect || getDialect() instanceof PostgreSQL81Dialect || getDialect() instanceof MySQLDialect)) {
        s.createQuery("from Animal where abs(cast(1 as float) - cast(:param as float)) = 1.0").setLong("param", 1).list();
    }
    s.getTransaction().commit();
    s.close();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) AbstractHANADialect(org.hibernate.dialect.AbstractHANADialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) PostgreSQLDialect(org.hibernate.dialect.PostgreSQLDialect) DB2Dialect(org.hibernate.dialect.DB2Dialect) Session(org.hibernate.Session) PostgreSQL81Dialect(org.hibernate.dialect.PostgreSQL81Dialect) Test(org.junit.Test)

Example 2 with AbstractHANADialect

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

the class FumTest method testCompositeID.

@Test
public void testCompositeID() throws Exception {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    FumCompositeID fumKey = fumKey("fum");
    Fum fum = new Fum(fumKey);
    fum.setFum("fee fi fo");
    s.save(fum);
    assertTrue("load by composite key", fum == s.load(Fum.class, fumKey));
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    if (getDialect() instanceof AbstractHANADialect) {
        // HANA currently requires specifying table name by 'FOR UPDATE of t1.c1' if there are more than one tables/views/subqueries in the FROM clause
        fum = (Fum) s.load(Fum.class, fumKey);
    } else {
        fum = (Fum) s.load(Fum.class, fumKey, LockMode.UPGRADE);
    }
    assertTrue("load by composite key", fum != null);
    FumCompositeID fumKey2 = fumKey("fi");
    Fum fum2 = new Fum(fumKey2);
    fum2.setFum("fee fo fi");
    fum.setFo(fum2);
    s.save(fum2);
    assertTrue("find composite keyed objects", s.createQuery("from Fum fum where not fum.fum='FRIEND'").list().size() == 2);
    assertTrue("find composite keyed object", s.createQuery("select fum from Fum fum where fum.fum='fee fi fo'").list().get(0) == fum);
    fum.setFo(null);
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    Iterator iter = s.createQuery("from Fum fum where not fum.fum='FRIEND'").iterate();
    int i = 0;
    while (iter.hasNext()) {
        fum = (Fum) iter.next();
        //iter.remove();
        s.delete(fum);
        i++;
    }
    assertTrue("iterate on composite key", i == 2);
    txn.commit();
    s.close();
}
Also used : AbstractHANADialect(org.hibernate.dialect.AbstractHANADialect) Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with AbstractHANADialect

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

the class MultiTableTest method testMultiTable.

@Test
public void testMultiTable() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Multi multi = new Multi();
    multi.setExtraProp("extra");
    multi.setName("name");
    Top simp = new Top();
    simp.setDate(new Date());
    simp.setName("simp");
    Serializable mid = s.save(multi);
    Serializable sid = s.save(simp);
    SubMulti sm = new SubMulti();
    sm.setAmount(66.5f);
    Serializable smid = s.save(sm);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    multi.setExtraProp(multi.getExtraProp() + "2");
    //multi.setCount( multi.getCount() + 1 );
    multi.setName("new name");
    s.update(multi);
    simp.setName("new name");
    s.update(simp);
    sm.setAmount(456.7f);
    s.update(sm);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    multi = (Multi) s.load(Multi.class, mid);
    assertTrue(multi.getExtraProp().equals("extra2"));
    multi.setExtraProp(multi.getExtraProp() + "3");
    //multi.setCount( multi.getCount() + 1 );
    assertTrue(multi.getName().equals("new name"));
    multi.setName("newer name");
    sm = (SubMulti) s.load(SubMulti.class, smid);
    assertTrue(sm.getAmount() == 456.7f);
    sm.setAmount(23423f);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    multi = (Multi) s.load(Top.class, mid);
    simp = (Top) s.load(Top.class, sid);
    assertTrue(!(simp instanceof Multi));
    assertTrue(multi.getExtraProp().equals("extra23"));
    //multi.setCount( multi.getCount() + 1 );
    assertTrue(multi.getName().equals("newer name"));
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Iterator iter = s.createQuery("select\n\nt from Top t where t.count>0").iterate();
    boolean foundSimp = false;
    boolean foundMulti = false;
    boolean foundSubMulti = false;
    while (iter.hasNext()) {
        Object o = iter.next();
        if ((o instanceof Top) && !(o instanceof Multi))
            foundSimp = true;
        if (o instanceof Multi && !(o instanceof SubMulti))
            foundMulti = true;
        if (o instanceof SubMulti)
            foundSubMulti = true;
    }
    assertTrue(foundSimp && foundMulti && foundSubMulti);
    s.createQuery("from Multi m where m.count>0 and m.extraProp is not null").list();
    s.createQuery("from Top m where m.count>0 and m.name is not null").list();
    s.createQuery("from Lower m where m.other is not null").list();
    s.createQuery("from Multi m where m.other.id = 1").list();
    s.createQuery("from SubMulti m where m.amount > 0.0").list();
    assertTrue(s.createQuery("from Multi").list().size() == 2);
    assertTrue(s.createQuery("from Multi m where m.class = SubMulti").list().size() == 1);
    assertTrue(s.createQuery("from Top m where m.class = Multi").list().size() == 1);
    assertTrue(s.createQuery("from Top").list().size() == 3);
    assertTrue(s.createQuery("from Lower").list().size() == 0);
    assertTrue(s.createQuery("from SubMulti").list().size() == 1);
    s.createQuery("from Lower ls join ls.bag s where s.id is not null").list();
    s.createQuery("from Lower ls join ls.set s where s.id is not null").list();
    if (!(getDialect() instanceof MySQLDialect))
        s.createQuery("from SubMulti sm where exists elements(sm.children)").list();
    List l = s.createCriteria(Top.class).list();
    assertTrue(l.size() == 3);
    assertTrue(s.createCriteria(SubMulti.class).list().size() == 1);
    assertTrue(s.createCriteria(SubMulti.class).add(Restrictions.lt("amount", new Float(0))).list().size() == 0);
    assertTrue(s.createCriteria(SubMulti.class).add(Restrictions.ge("amount", new Float(0))).list().size() == 1);
    t.commit();
    s.close();
    // if there are more than one tables/views/subqueries in the FROM clause
    if (!(getDialect() instanceof AbstractHANADialect)) {
        s = openSession();
        t = s.beginTransaction();
        multi = (Multi) s.load(Top.class, mid, LockMode.UPGRADE);
        simp = (Top) s.load(Top.class, sid);
        s.lock(simp, LockMode.UPGRADE_NOWAIT);
        t.commit();
        s.close();
    }
    s = openSession();
    t = s.beginTransaction();
    s.update(multi);
    s.delete(multi);
    assertEquals(2, doDelete(s, "from Top"));
    t.commit();
    s.close();
}
Also used : Serializable(java.io.Serializable) Date(java.util.Date) AbstractHANADialect(org.hibernate.dialect.AbstractHANADialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with AbstractHANADialect

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

the class MultiTableTest method testMultiTableGeneratedId.

@Test
public void testMultiTableGeneratedId() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Multi multi = new Multi();
    multi.setExtraProp("extra");
    //multi.setCount(666);
    multi.setName("name");
    Top simp = new Top();
    simp.setDate(new Date());
    simp.setName("simp");
    //simp.setCount(132);
    Serializable multiId = s.save(multi);
    Serializable simpId = s.save(simp);
    SubMulti sm = new SubMulti();
    sm.setAmount(66.5f);
    Serializable smId = s.save(sm);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    multi.setExtraProp(multi.getExtraProp() + "2");
    //multi.setCount( multi.getCount() + 1 );
    multi.setName("new name");
    s.update(multi);
    simp.setName("new name");
    s.update(simp);
    sm.setAmount(456.7f);
    s.update(sm);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    multi = (Multi) s.load(Multi.class, multiId);
    assertTrue(multi.getExtraProp().equals("extra2"));
    multi.setExtraProp(multi.getExtraProp() + "3");
    //multi.setCount( multi.getCount() + 1 );
    assertTrue(multi.getName().equals("new name"));
    multi.setName("newer name");
    sm = (SubMulti) s.load(SubMulti.class, smId);
    assertTrue(sm.getAmount() == 456.7f);
    sm.setAmount(23423f);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    multi = (Multi) s.load(Top.class, multiId);
    simp = (Top) s.load(Top.class, simpId);
    assertTrue(!(simp instanceof Multi));
    assertTrue(multi.getExtraProp().equals("extra23"));
    //multi.setCount( multi.getCount() + 1 );
    assertTrue(multi.getName().equals("newer name"));
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Iterator iter = s.createQuery("select\n\nt from Top t where t.count>0").iterate();
    boolean foundSimp = false;
    boolean foundMulti = false;
    boolean foundSubMulti = false;
    while (iter.hasNext()) {
        Object o = iter.next();
        if ((o instanceof Top) && !(o instanceof Multi))
            foundSimp = true;
        if (o instanceof Multi && !(o instanceof SubMulti))
            foundMulti = true;
        if (o instanceof SubMulti)
            foundSubMulti = true;
    }
    assertTrue(foundSimp && foundMulti && foundSubMulti);
    s.createQuery("from Multi m where m.count>0 and m.extraProp is not null").list();
    s.createQuery("from Top m where m.count>0 and m.name is not null").list();
    s.createQuery("from Lower m where m.other is not null").list();
    s.createQuery("from Multi m where m.other.id = 1").list();
    s.createQuery("from SubMulti m where m.amount > 0.0").list();
    assertTrue(s.createQuery("from Multi").list().size() == 2);
    /*assertTrue(
			s.find("from m in class Multi where m.class = Multi").size()==1
		);*/
    assertTrue(s.createQuery("from Top").list().size() == 3);
    assertTrue(s.createQuery("from Lower").list().size() == 0);
    assertTrue(s.createQuery("from SubMulti").list().size() == 1);
    s.createQuery("from Lower ls join ls.bag s where s.id is not null").list();
    if (!(getDialect() instanceof MySQLDialect))
        s.createQuery("from SubMulti sm where exists elements(sm.children)").list();
    t.commit();
    s.close();
    // if there are more than one tables/views/subqueries in the FROM clause
    if (!(getDialect() instanceof AbstractHANADialect)) {
        s = openSession();
        t = s.beginTransaction();
        multi = (Multi) s.load(Top.class, multiId, LockMode.UPGRADE);
        simp = (Top) s.load(Top.class, simpId);
        s.lock(simp, LockMode.UPGRADE_NOWAIT);
        t.commit();
        s.close();
    }
    s = openSession();
    t = s.beginTransaction();
    s.update(multi);
    s.delete(multi);
    assertEquals(2, doDelete(s, "from Top"));
    t.commit();
    s.close();
}
Also used : AbstractHANADialect(org.hibernate.dialect.AbstractHANADialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) Serializable(java.io.Serializable) Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with AbstractHANADialect

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

the class ResultTransformerTest method testResultTransformerIsAppliedToScrollableResults.

@Test
@TestForIssue(jiraKey = "HHH-3694")
public void testResultTransformerIsAppliedToScrollableResults() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    PartnerA a = new PartnerA();
    a.setName("Partner A");
    PartnerB b = new PartnerB();
    b.setName("Partner B");
    Contract obj1 = new Contract();
    obj1.setName("Contract");
    obj1.setA(a);
    obj1.setB(b);
    s.save(a);
    s.save(b);
    s.save(obj1);
    tx.commit();
    s.close();
    s = openSession();
    Query q = s.getNamedQuery(Contract.class.getName() + ".testQuery");
    q.setFetchSize(100);
    q.setResultTransformer(new ResultTransformer() {

        private static final long serialVersionUID = -5815434828170704822L;

        public Object transformTuple(Object[] arg0, String[] arg1) {
            // return only the PartnerA object from the query
            return arg0[1];
        }

        @SuppressWarnings("unchecked")
        public List transformList(List arg0) {
            return arg0;
        }
    });
    ScrollableResults sr = q.scroll();
    // does not support java.sql.ResultSet.first()
    if (getDialect() instanceof AbstractHANADialect) {
        sr.next();
    } else {
        sr.first();
    }
    Object[] row = sr.get();
    assertEquals(1, row.length);
    Object obj = row[0];
    assertTrue(obj instanceof PartnerA);
    PartnerA obj2 = (PartnerA) obj;
    assertEquals("Partner A", obj2.getName());
    s.close();
}
Also used : Query(org.hibernate.Query) AbstractHANADialect(org.hibernate.dialect.AbstractHANADialect) Transaction(org.hibernate.Transaction) ResultTransformer(org.hibernate.transform.ResultTransformer) List(java.util.List) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

Session (org.hibernate.Session)7 AbstractHANADialect (org.hibernate.dialect.AbstractHANADialect)7 Test (org.junit.Test)7 Transaction (org.hibernate.Transaction)5 Iterator (java.util.Iterator)4 MySQLDialect (org.hibernate.dialect.MySQLDialect)4 List (java.util.List)3 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 DB2Dialect (org.hibernate.dialect.DB2Dialect)2 HSQLDialect (org.hibernate.dialect.HSQLDialect)2 PostgreSQL81Dialect (org.hibernate.dialect.PostgreSQL81Dialect)2 PostgreSQLDialect (org.hibernate.dialect.PostgreSQLDialect)2 Query (org.hibernate.Query)1 ScrollableResults (org.hibernate.ScrollableResults)1 Oracle8iDialect (org.hibernate.dialect.Oracle8iDialect)1 PointbaseDialect (org.hibernate.dialect.PointbaseDialect)1 SAPDBDialect (org.hibernate.dialect.SAPDBDialect)1 Sybase11Dialect (org.hibernate.dialect.Sybase11Dialect)1