Search in sources :

Example 51 with Serializable

use of java.io.Serializable in project hibernate-orm by hibernate.

the class MultiTableTest method testMultiTableCollections.

@Test
public void testMultiTableCollections() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    assertTrue(s.createQuery("from Top").list().size() == 0);
    Multi multi = new Multi();
    multi.setExtraProp("extra");
    multi.setName("name");
    Top simp = new Top();
    simp.setDate(new Date());
    simp.setName("simp");
    s.save(multi);
    s.save(simp);
    Lower ls = new Lower();
    ls.setOther(ls);
    ls.setAnother(ls);
    ls.setYetanother(ls);
    ls.setName("Less Simple");
    Set set = new HashSet();
    ls.setSet(set);
    set.add(multi);
    set.add(simp);
    Serializable id = s.save(ls);
    t.commit();
    s.close();
    assertTrue(ls.getOther() == ls && ls.getAnother() == ls && ls.getYetanother() == ls);
    s = openSession();
    t = s.beginTransaction();
    ls = (Lower) s.load(Lower.class, id);
    assertTrue(ls.getOther() == ls && ls.getAnother() == ls && ls.getYetanother() == ls);
    assertTrue(ls.getSet().size() == 2);
    Iterator iter = ls.getSet().iterator();
    int foundMulti = 0;
    int foundSimple = 0;
    while (iter.hasNext()) {
        Object o = iter.next();
        if (o instanceof Top)
            foundSimple++;
        if (o instanceof Multi)
            foundMulti++;
    }
    assertTrue(foundSimple == 2 && foundMulti == 1);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    try {
        // MySQL does not like deleting rows that refer to itself without first
        // null'ing out the FK.  Ugh...
        ls = s.load(Lower.class, id);
        ls.setOther(null);
        ls.setAnother(null);
        ls.setYetanother(null);
        for (Object o : ls.getSet()) {
            s.delete(o);
        }
        ls.getSet().clear();
        s.flush();
        s.delete(ls);
        t.commit();
    } catch (Exception e) {
        t.rollback();
        throw e;
    } finally {
        s.close();
    }
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) HashSet(java.util.HashSet) Date(java.util.Date) SQLException(java.sql.SQLException) Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 52 with Serializable

use of java.io.Serializable in project hibernate-orm by hibernate.

the class MasterDetailTest method testCascading.

@Test
public void testCascading() throws Exception {
    Session s = openSession();
    s.beginTransaction();
    Detail d1 = new Detail();
    Detail d2 = new Detail();
    d2.setI(22);
    Master m = new Master();
    Master m0 = new Master();
    Serializable m0id = s.save(m0);
    m0.addDetail(d1);
    m0.addDetail(d2);
    d1.setMaster(m0);
    d2.setMaster(m0);
    m.getMoreDetails().add(d1);
    m.getMoreDetails().add(d2);
    Serializable mid = s.save(m);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    m = (Master) s.load(Master.class, mid);
    assertTrue("cascade save", m.getMoreDetails().size() == 2);
    assertTrue("cascade save", ((Detail) m.getMoreDetails().iterator().next()).getMaster().getDetails().size() == 2);
    s.delete(m);
    s.delete(s.load(Master.class, m0id));
    s.getTransaction().commit();
    s.close();
}
Also used : Serializable(java.io.Serializable) Session(org.hibernate.Session) Test(org.junit.Test)

Example 53 with Serializable

use of java.io.Serializable in project hibernate-orm by hibernate.

the class MasterDetailTest method testIncomingOutgoing.

@Test
public void testIncomingOutgoing() throws Exception {
    Session s = openSession();
    s.beginTransaction();
    Master master1 = new Master();
    Master master2 = new Master();
    Master master3 = new Master();
    s.save(master1);
    s.save(master2);
    s.save(master3);
    master1.addIncoming(master2);
    master2.addOutgoing(master1);
    master1.addIncoming(master3);
    master3.addOutgoing(master1);
    Serializable m1id = s.getIdentifier(master1);
    assertTrue(s.createFilter(master1.getIncoming(), "where this.id > 0 and this.name is not null").list().size() == 2);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    master1 = (Master) s.load(Master.class, m1id);
    Iterator iter = master1.getIncoming().iterator();
    int i = 0;
    while (iter.hasNext()) {
        Master m = (Master) iter.next();
        assertTrue("outgoing", m.getOutgoing().size() == 1);
        assertTrue("outgoing", m.getOutgoing().contains(master1));
        s.delete(m);
        i++;
    }
    assertTrue("incoming-outgoing", i == 2);
    s.delete(master1);
    s.getTransaction().commit();
    s.close();
}
Also used : Serializable(java.io.Serializable) Iterator(java.util.Iterator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 54 with Serializable

use of java.io.Serializable 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)

Example 55 with Serializable

use of java.io.Serializable in project hibernate-orm by hibernate.

the class MasterDetailTest method testMasterDetail.

@Test
public void testMasterDetail() throws Exception {
    if (getDialect() instanceof HSQLDialect)
        return;
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Master master = new Master();
    assertTrue("save returned native id", s.save(master) != null);
    Serializable mid = s.getIdentifier(master);
    Detail d1 = new Detail();
    d1.setMaster(master);
    Serializable did = s.save(d1);
    Detail d2 = new Detail();
    d2.setI(12);
    d2.setMaster(master);
    assertTrue("generated id returned", s.save(d2) != null);
    master.addDetail(d1);
    master.addDetail(d2);
    if (!(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof org.hibernate.dialect.TimesTenDialect)) {
        assertTrue("query", s.createQuery("from Detail d, Master m where m = d.master and size(m.outgoing) = 0 and size(m.incoming) = 0").list().size() == 2);
    }
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    master = new Master();
    s.load(master, mid);
    assertTrue(master.getDetails().size() == 2);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    master = (Master) s.load(Master.class, mid);
    Iterator iter = master.getDetails().iterator();
    int i = 0;
    while (iter.hasNext()) {
        Detail d = (Detail) iter.next();
        assertTrue("master-detail", d.getMaster() == master);
        i++;
    }
    assertTrue("master-detail", i == 2);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    assertTrue(s.createQuery("select elements(master.details) from Master master").list().size() == 2);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    List list = s.createQuery("from Master m left join fetch m.details").list();
    Master m = (Master) list.get(0);
    assertTrue(Hibernate.isInitialized(m.getDetails()));
    assertTrue(m.getDetails().size() == 2);
    list = s.createQuery("from Detail d inner join fetch d.master").list();
    Detail dt = (Detail) list.get(0);
    Serializable dtid = s.getIdentifier(dt);
    assertTrue(dt.getMaster() == m);
    //assertTrue(m.getAllDetails().size()==2);
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    list = s.createQuery("select m from Master m1, Master m left join fetch m.details where m.name=m1.name").list();
    assertTrue(Hibernate.isInitialized(((Master) list.get(0)).getDetails()));
    dt = (Detail) s.load(Detail.class, dtid);
    assertTrue(((Master) list.get(0)).getDetails().contains(dt));
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    list = s.createQuery("select m, m1.name from Master m1, Master m left join fetch m.details where m.name=m1.name").list();
    assertTrue(Hibernate.isInitialized(((Master) ((Object[]) list.get(0))[0]).getDetails()));
    dt = (Detail) s.load(Detail.class, dtid);
    assertTrue(((Master) ((Object[]) list.get(0))[0]).getDetails().contains(dt));
    //list = s.find("select m from Master m, Master m2 left join fetch m.details");
    // depracted syntax
    //		list = s.find("select m.id from Master m inner join fetch m.details");
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    Detail dd = (Detail) s.load(Detail.class, did);
    master = dd.getMaster();
    assertTrue("detail-master", master.getDetails().contains(dd));
    assertTrue(s.createFilter(master.getDetails(), "order by this.i desc").list().size() == 2);
    assertTrue(s.createFilter(master.getDetails(), "select this where this.id > -1").list().size() == 2);
    Query q = s.createFilter(master.getDetails(), "where this.id > :id");
    q.setInteger("id", -1);
    assertTrue(q.list().size() == 2);
    q = s.createFilter(master.getDetails(), "where this.id > :id1 and this.id < :id2");
    q.setInteger("id1", -1);
    q.setInteger("id2", 99999999);
    assertTrue(q.list().size() == 2);
    q.setInteger("id2", -1);
    assertTrue(q.list().size() == 0);
    q = s.createFilter(master.getDetails(), "where this.id in (:ids)");
    list = new ArrayList();
    list.add(did);
    list.add(new Long(-1));
    q.setParameterList("ids", list);
    assertTrue(q.list().size() == 1);
    assertTrue(q.iterate().hasNext());
    assertTrue(s.createFilter(master.getDetails(), "where this.id > -1").list().size() == 2);
    assertTrue(s.createFilter(master.getDetails(), "select this.master where this.id > -1").list().size() == 2);
    assertTrue(s.createFilter(master.getDetails(), "select m from Master m where this.id > -1 and this.master=m").list().size() == 2);
    assertTrue(s.createFilter(master.getIncoming(), "where this.id > -1 and this.name is not null").list().size() == 0);
    assertTrue(s.createFilter(master.getDetails(), "select max(this.i)").iterate().next() instanceof Integer);
    assertTrue(s.createFilter(master.getDetails(), "select max(this.i) group by this.id").iterate().next() instanceof Integer);
    assertTrue(s.createFilter(master.getDetails(), "select count(*)").iterate().next() instanceof Long);
    assertTrue(s.createFilter(master.getDetails(), "select this.master").list().size() == 2);
    assertTrue(s.createFilter(master.getMoreDetails(), "").list().size() == 0);
    assertTrue(s.createFilter(master.getIncoming(), "").list().size() == 0);
    Query f = s.createFilter(master.getDetails(), "select max(this.i) where this.i < :top and this.i>=:bottom");
    f.setInteger("top", 100);
    f.setInteger("bottom", 0);
    assertEquals(f.iterate().next(), new Integer(12));
    f.setInteger("top", 2);
    assertEquals(f.iterate().next(), new Integer(0));
    f = s.createFilter(master.getDetails(), "select max(this.i) where this.i not in (:list)");
    Collection coll = new ArrayList();
    coll.add(new Integer(-666));
    coll.add(new Integer(22));
    coll.add(new Integer(0));
    f.setParameterList("list", coll);
    assertEquals(f.iterate().next(), new Integer(12));
    f = s.createFilter(master.getDetails(), "select max(this.i) where this.i not in (:list) and this.master.name = :listy2");
    f.setParameterList("list", coll);
    f.setParameter("listy2", master.getName());
    assertEquals(f.iterate().next(), new Integer(12));
    iter = master.getDetails().iterator();
    i = 0;
    while (iter.hasNext()) {
        Detail d = (Detail) iter.next();
        assertTrue("master-detail", d.getMaster() == master);
        s.delete(d);
        i++;
    }
    assertTrue("master-detail", i == 2);
    s.delete(master);
    t.commit();
    s.close();
}
Also used : HSQLDialect(org.hibernate.dialect.HSQLDialect) MckoiDialect(org.hibernate.dialect.MckoiDialect) Serializable(java.io.Serializable) Query(org.hibernate.Query) ArrayList(java.util.ArrayList) SAPDBDialect(org.hibernate.dialect.SAPDBDialect) MySQLDialect(org.hibernate.dialect.MySQLDialect) Transaction(org.hibernate.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Serializable (java.io.Serializable)1280 Test (org.junit.Test)334 HashMap (java.util.HashMap)265 ArrayList (java.util.ArrayList)215 Map (java.util.Map)127 List (java.util.List)94 Metacard (ddf.catalog.data.Metacard)89 IOException (java.io.IOException)89 HashSet (java.util.HashSet)70 Session (org.hibernate.Session)63 Task (org.apache.hadoop.hive.ql.exec.Task)57 ByteArrayInputStream (java.io.ByteArrayInputStream)50 Set (java.util.Set)50 ObjectInputStream (java.io.ObjectInputStream)43 Date (java.util.Date)41 LinkedHashMap (java.util.LinkedHashMap)40 EntityPersister (org.hibernate.persister.entity.EntityPersister)34 File (java.io.File)33 Iterator (java.util.Iterator)33 URI (java.net.URI)32