use of java.io.Serializable in project hibernate-orm by hibernate.
the class FooBarTest method testSerializableType.
@Test
public void testSerializableType() throws Exception {
Session s = openSession();
s.beginTransaction();
Vetoer v = new Vetoer();
v.setStrings(new String[] { "foo", "bar", "baz" });
s.save(v);
Serializable id = s.save(v);
v.getStrings()[1] = "osama";
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
v = (Vetoer) s.load(Vetoer.class, id);
assertTrue("serializable type", v.getStrings()[1].equals("osama"));
s.delete(v);
s.delete(v);
s.flush();
s.getTransaction().commit();
s.close();
}
use of java.io.Serializable in project hibernate-orm by hibernate.
the class ParentChildTest method testQueryOneToOne.
@Test
public void testQueryOneToOne() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Serializable id = s.save(new Parent());
assertTrue(s.createQuery("from Parent p left join fetch p.child").list().size() == 1);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Parent p = (Parent) s.createQuery("from Parent p left join fetch p.child").uniqueResult();
assertTrue(p.getChild() == null);
s.createQuery("from Parent p join p.child c where c.x > 0").list();
s.createQuery("from Child c join c.parent p where p.x > 0").list();
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.delete(s.get(Parent.class, id));
t.commit();
s.close();
}
use of java.io.Serializable in project hibernate-orm by hibernate.
the class MasterDetailTest method testInterface.
@Test
public void testInterface() throws Exception {
Session s = openSession();
s.beginTransaction();
Serializable id = s.save(new BasicNameable());
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
Nameable n = (Nameable) s.load(Nameable.class, id);
s.delete(n);
s.getTransaction().commit();
s.close();
}
use of java.io.Serializable 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 java.io.Serializable 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();
}
Aggregations