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