use of java.io.Serializable in project hibernate-orm by hibernate.
the class MasterDetailTest method testMultiLevelCascade.
@Test
public void testMultiLevelCascade() throws Exception {
Session s = openSession();
Transaction txn = s.beginTransaction();
Detail detail = new Detail();
SubDetail subdetail = new SubDetail();
Master m = new Master();
Master m0 = new Master();
Serializable m0id = s.save(m0);
m0.addDetail(detail);
detail.setMaster(m0);
m.getMoreDetails().add(detail);
detail.setSubDetails(new HashSet());
detail.getSubDetails().add(subdetail);
Serializable mid = s.save(m);
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
m = (Master) s.load(Master.class, mid);
assertTrue(((Detail) m.getMoreDetails().iterator().next()).getSubDetails().size() != 0);
s.delete(m);
assertTrue(s.createQuery("from SubDetail").list().size() == 0);
assertTrue(s.createQuery("from Detail d").list().size() == 0);
s.delete(s.load(Master.class, m0id));
txn.commit();
s.close();
}
use of java.io.Serializable in project hibernate-orm by hibernate.
the class MasterDetailTest method testCollectionRefresh.
@Test
public void testCollectionRefresh() throws Exception {
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);
s.refresh(c);
s.flush();
assertTrue(c.getSubcategories().size() == 1);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
c = (Category) s.load(Category.class, id);
assertTrue(c.getSubcategories().size() == 1);
s.delete(c);
s.getTransaction().commit();
s.close();
}
use of java.io.Serializable in project hibernate-orm by hibernate.
the class MultiTableTest method testSubclassCollection.
@Test
public void testSubclassCollection() throws Exception {
//if ( getDialect() instanceof HSQLDialect ) return; //TODO: figure out why!?
Session s = openSession();
s.beginTransaction();
SubMulti sm = new SubMulti();
SubMulti sm1 = new SubMulti();
SubMulti sm2 = new SubMulti();
ArrayList list = new ArrayList();
ArrayList anotherList = new ArrayList();
sm.setChildren(list);
sm.setMoreChildren(anotherList);
sm.setExtraProp("foo");
list.add(sm1);
list.add(sm2);
anotherList.add(sm1);
anotherList.add(sm2);
sm1.setParent(sm);
sm2.setParent(sm);
Serializable id = s.save(sm);
s.save(sm1);
s.save(sm2);
s.getTransaction().commit();
s.close();
sessionFactory().getCache().evictEntityRegion(SubMulti.class);
final Session s2 = openSession();
s2.beginTransaction();
s2.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
final String sql = "select * from leafsubsubclass sm, nonleafsubclass m, rootclass s " + "where sm.sid=m.sid and sm.sid=s.id1_ and sm.sid=1";
Statement st = ((SessionImplementor) s2).getJdbcCoordinator().getStatementPreparer().createStatement();
((SessionImplementor) session).getJdbcCoordinator().getResultSetReturn().extract(st, sql).next();
}
});
assertTrue(s2.createQuery("select s from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").list().size() == 2);
s2.createQuery("select c from SubMulti sm join sm.children c").list();
assertTrue(s2.createQuery("select elements(sm.children) from SubMulti as sm").list().size() == 2);
assertTrue(s2.createQuery("select distinct sm from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").list().size() == 1);
sm = (SubMulti) s2.load(SubMulti.class, id);
assertTrue(sm.getChildren().size() == 2);
assertEquals(s2.createFilter(sm.getMoreChildren(), "select count(*) where this.amount>-1 and this.name is null").list().get(0), new Long(2));
assertEquals("FOO", sm.getDerived());
assertSame(s2.createQuery("select distinct s from SubMulti s where s.moreChildren[1].amount < 1.0").iterate().next(), sm);
assertTrue(sm.getMoreChildren().size() == 2);
s2.delete(sm);
Iterator iter = sm.getChildren().iterator();
while (iter.hasNext()) {
s2.delete(iter.next());
}
s2.flush();
s2.getTransaction().commit();
s2.close();
}
use of java.io.Serializable in project hibernate-orm by hibernate.
the class MultiTableTest method testCollectionPointer.
@Test
public void testCollectionPointer() throws Exception {
Session sess = openSession();
sess.beginTransaction();
Lower ls = new Lower();
List list = new ArrayList();
ls.setBag(list);
Top s = new Top();
Serializable id = sess.save(ls);
sess.save(s);
sess.flush();
list.add(s);
sess.getTransaction().commit();
sess.close();
sess = openSession();
sess.beginTransaction();
ls = (Lower) sess.load(Lower.class, id);
assertTrue(ls.getBag().size() == 1);
doDelete(sess, "from java.lang.Object");
sess.getTransaction().commit();
sess.close();
}
use of java.io.Serializable in project hibernate-orm by hibernate.
the class IJ2Test method testUnionSubclass.
@SuppressWarnings({ "UnusedAssignment" })
@Test
public void testUnionSubclass() throws Exception {
Session s = sessionFactory().openSession();
s.beginTransaction();
I i = new I();
i.setName("i");
i.setType('a');
J j = new J();
j.setName("j");
j.setType('x');
j.setAmount(1.0f);
Serializable iid = s.save(i);
Serializable jid = s.save(j);
s.getTransaction().commit();
s.close();
sessionFactory().getCache().evictEntityRegion(I.class);
s = sessionFactory().openSession();
s.beginTransaction();
j = (J) s.get(I.class, jid);
j = (J) s.get(J.class, jid);
i = (I) s.get(I.class, iid);
assertTrue(i.getClass() == I.class);
j.setAmount(0.5f);
s.lock(i, LockMode.UPGRADE);
s.getTransaction().commit();
s.close();
sessionFactory().getCache().evictEntityRegion(I.class);
s = sessionFactory().openSession();
s.beginTransaction();
j = (J) s.get(J.class, jid);
j = (J) s.get(I.class, jid);
i = (I) s.get(I.class, iid);
assertTrue(i.getClass() == I.class);
j.setAmount(0.5f);
s.lock(i, LockMode.UPGRADE);
s.getTransaction().commit();
s.close();
sessionFactory().getCache().evictEntityRegion(I.class);
s = sessionFactory().openSession();
s.beginTransaction();
assertTrue(s.createQuery("from I").list().size() == 2);
assertTrue(s.createQuery("from J").list().size() == 1);
assertTrue(s.createQuery("from J j where j.amount > 0 and j.name is not null").list().size() == 1);
assertTrue(s.createQuery("from I i where i.class = org.hibernate.test.legacy.I").list().size() == 1);
assertTrue(s.createQuery("from I i where i.class = J").list().size() == 1);
s.getTransaction().commit();
s.close();
sessionFactory().getCache().evictEntityRegion(I.class);
s = sessionFactory().openSession();
s.beginTransaction();
j = (J) s.get(J.class, jid);
i = (I) s.get(I.class, iid);
K k = new K();
Serializable kid = s.save(k);
i.setParent(k);
j.setParent(k);
s.getTransaction().commit();
s.close();
sessionFactory().getCache().evictEntityRegion(I.class);
s = sessionFactory().openSession();
s.beginTransaction();
j = (J) s.get(J.class, jid);
i = (I) s.get(I.class, iid);
k = (K) s.get(K.class, kid);
System.out.println(k + "=" + i.getParent());
assertTrue(i.getParent() == k);
assertTrue(j.getParent() == k);
assertTrue(k.getIs().size() == 2);
s.getTransaction().commit();
s.close();
sessionFactory().getCache().evictEntityRegion(I.class);
s = sessionFactory().openSession();
s.beginTransaction();
assertTrue(s.createQuery("from K k inner join k.is i where i.name = 'j'").list().size() == 1);
assertTrue(s.createQuery("from K k inner join k.is i where i.name = 'i'").list().size() == 1);
assertTrue(s.createQuery("from K k left join fetch k.is").list().size() == 2);
s.getTransaction().commit();
s.close();
s = sessionFactory().openSession();
s.beginTransaction();
j = (J) s.get(J.class, jid);
i = (I) s.get(I.class, iid);
k = (K) s.get(K.class, kid);
s.delete(k);
s.delete(j);
s.delete(i);
s.getTransaction().commit();
s.close();
}
Aggregations