use of org.hibernate.dialect.MySQLDialect in project hibernate-orm by hibernate.
the class MasterDetailTest method testCollectionQuery.
@Test
public void testCollectionQuery() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
if (!(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof MckoiDialect)) {
s.createQuery("FROM Master m WHERE NOT EXISTS ( FROM m.details d WHERE NOT d.i=5 )").iterate();
s.createQuery("FROM Master m WHERE NOT 5 IN ( SELECT d.i FROM m.details AS d )").iterate();
}
s.createQuery("SELECT m FROM Master m JOIN m.details d WHERE d.i=5").iterate();
s.createQuery("SELECT m FROM Master m JOIN m.details d WHERE d.i=5").list();
s.createQuery("SELECT m.id FROM Master AS m JOIN m.details AS d WHERE d.i=5").list();
t.commit();
s.close();
}
use of org.hibernate.dialect.MySQLDialect in project hibernate-orm by hibernate.
the class MasterDetailTest method testExample.
@Test
public void testExample() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Master m = new Master();
m.setName("name");
m.setX(5);
m.setOtherMaster(m);
s.save(m);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Master m1 = (Master) s.createCriteria(Master.class).add(Example.create(m).enableLike().ignoreCase().excludeProperty("bigDecimal")).uniqueResult();
assertTrue(m1.getOtherMaster() == m1);
m1 = (Master) s.createCriteria(Master.class).add(Restrictions.eq("name", "foobar")).uniqueResult();
assertTrue(m1 == null);
m1 = (Master) s.createCriteria(Master.class).add(Example.create(m).excludeProperty("bigDecimal")).createCriteria("otherMaster").add(Example.create(m).excludeZeroes().excludeProperty("bigDecimal")).uniqueResult();
assertTrue(m1.getOtherMaster() == m1);
Master m2 = (Master) s.createCriteria(Master.class).add(Example.create(m).excludeNone().excludeProperty("bigDecimal")).uniqueResult();
assertTrue(m2 == m1);
m.setName(null);
m2 = (Master) s.createCriteria(Master.class).add(Example.create(m).excludeNone().excludeProperty("bigDecimal")).uniqueResult();
assertTrue(null == m2);
if (getDialect() instanceof HSQLDialect || getDialect() instanceof MySQLDialect) {
m1.setOtherMaster(null);
s.flush();
}
s.delete(m1);
t.commit();
s.close();
}
use of org.hibernate.dialect.MySQLDialect 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.MySQLDialect 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.MySQLDialect in project hibernate-orm by hibernate.
the class MasterDetailTest method testCachedCollectionRefresh.
@Test
public void testCachedCollectionRefresh() throws Exception {
if (isSerializableIsolationEnforced()) {
SkipLog.reportSkip("SERIALIZABLE isolation", "cached collection refreshing");
return;
}
Session s = openSession();
s.beginTransaction();
Category c = new Category();
List list = new ArrayList();
c.setSubcategories(list);
list.add(new Category());
c.setName("root");
Serializable id = s.save(c);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
c = (Category) s.load(Category.class, id);
//force load and cache
c.getSubcategories().size();
s.getTransaction().commit();
s.close();
s = openSession();
if ((getDialect() instanceof MySQLDialect)) {
s.doWork(new AbstractWork() {
@Override
public void execute(Connection connection) throws SQLException {
connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
}
});
}
s.beginTransaction();
c = (Category) s.load(Category.class, id);
//force load
c.getSubcategories().size();
Session ss = openSession();
ss.beginTransaction();
Category c2 = (Category) ss.load(Category.class, id);
ss.delete(c2.getSubcategories().get(0));
c2.getSubcategories().clear();
ss.getTransaction().commit();
ss.close();
s.refresh(c);
assertTrue(c.getSubcategories().size() == 0);
ss = openSession();
ss.beginTransaction();
c2 = (Category) ss.load(Category.class, id);
c2.getSubcategories().add(new Category());
c2.getSubcategories().add(new Category());
ss.getTransaction().commit();
ss.close();
s.refresh(c);
assertEquals(2, c.getSubcategories().size());
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
c = (Category) s.load(Category.class, id);
assertEquals(2, c.getSubcategories().size());
s.delete(c);
s.getTransaction().commit();
s.close();
}
Aggregations