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