Ejemplo n.º 1
0
 @Test
 public void testSaveLoad() {
   Long id = null;
   Session session = SessionUtil.getSession();
   Transaction tx = session.beginTransaction();
   SimpleObject obj = new SimpleObject();
   obj.setKey("sl");
   obj.setValue(10L);
   session.save(obj);
   assertNotNull(obj.getId());
   id = obj.getId();
   tx.commit();
   session.close();
   session = SessionUtil.getSession();
   tx = session.beginTransaction();
   SimpleObject o2 = (SimpleObject) session.load(SimpleObject.class, id);
   assertEquals(o2.getKey(), "sl");
   assertNotNull(o2.getValue());
   assertEquals(o2.getValue().longValue(), 10L);
   SimpleObject o3 = (SimpleObject) session.load(SimpleObject.class, id);
   // Since o2 and o3 were loaded in the same session, they are not only
   // equivalent - as shown by equals() - but equal, as shown by ==
   // since obj was loaded from a different session, it is equivalent but not ==
   assertEquals(o2, o3);
   assertEquals(obj, o2);
   assertTrue(o2 == o3);
   assertFalse(obj == o2);
   tx.commit();
   session.close();
 }
Ejemplo n.º 2
0
 @Test
 public void testSaveOrUpdateEntity() {
   Long id;
   Session session = SessionUtil.getSession();
   Transaction tx = session.beginTransaction();
   SimpleObject obj = new SimpleObject();
   obj.setKey("osas");
   obj.setValue(10L);
   session.save(obj);
   id = obj.getId();
   tx.commit();
   session.close();
   session = SessionUtil.getSession();
   tx = session.beginTransaction();
   obj.setValue(2L);
   session.saveOrUpdate(obj);
   tx.commit();
   session.close();
   // saveOrUpdate will update a row in the database
   // if one matches. This is the correct behavior.
   assertEquals(id, obj.getId());
 }
Ejemplo n.º 3
0
 @Test
 public void testSavingEntitiesTwice() {
   Long id;
   Session session = SessionUtil.getSession();
   Transaction tx = session.beginTransaction();
   SimpleObject obj = new SimpleObject();
   obj.setKey("osas");
   obj.setValue(10L);
   session.save(obj);
   id = obj.getId();
   tx.commit();
   session.close();
   session = SessionUtil.getSession();
   tx = session.beginTransaction();
   obj.setValue(2L);
   session.save(obj);
   tx.commit();
   session.close();
   // Note that save() creates a new row in the database.
   // This is wrong behavior. Do not do this.
   assertNotEquals(id, obj.getId());
 }