Search in sources :

Example 11 with MySQLDialect

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();
}
Also used : SAPDBDialect(org.hibernate.dialect.SAPDBDialect) MckoiDialect(org.hibernate.dialect.MckoiDialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 12 with MySQLDialect

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();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 13 with MySQLDialect

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();
}
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 14 with MySQLDialect

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();
}
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 15 with MySQLDialect

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();
}
Also used : MySQLDialect(org.hibernate.dialect.MySQLDialect) Serializable(java.io.Serializable) SQLException(java.sql.SQLException) AbstractWork(org.hibernate.jdbc.AbstractWork) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) List(java.util.List) 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