how to write junit test for save method without transaction?
it seems test codes runs well, but it didn't save to database.
========================================
public void testSaveOrUpdate() {
try {
PigeonholeType pigeonholeType = new PigeonholeType();
pigeonholeType.setSymbol("none");
PigeonholeRule pigeonholeRule = new PigeonholeRule();
pigeonholeRule.setXtypeSymbol("SB006");
pigeonholeRule.setSize(new Integer(456));
pigeonholeRule.setPigeonholeType(pigeonholeType);
pigeonholeRuleDao.saveOrUpdate(pigeonholeRule);
} catch (Exception e) {
e.printStackTrace();
}
PigeonholeRule obj = pigeonholeRuleDao.loadByPrimaryKey("SB006");
System.out.println(obj.getSize());
Xtype xtype = xtypeDao.loadByPrimaryKey("SB006");
Iterator it = xtype.getSetOfPigeonholeRule().iterator();
while (it.hasNext()) {
PigeonholeRule pigeonholeRule = (PigeonholeRule)it.next();
System.out.println(pigeonholeRule.getPigeonholeType().getSymbol());
System.out.println(pigeonholeRule.getPigeonholeType().getTitle());
}
}
}
=========================================
public Serializable saveOrUpdate(PigeonholeRule obj)
throws DataAccessException {
Session session = null;
Transaction tx = null;
try {
session = sessionFactory.openSession();
// tx = session.beginTransaction();
session.saveOrUpdateCopy(obj);
session.flush();
// if not use transaction tx.commit();
return obj;
} catch (HibernateException he) {
try {
tx.rollback();
} catch(HibernateException hex) {
throw SessionFactoryUtils.convertHibernateAccessException(hex);
}
throw SessionFactoryUtils.convertHibernateAccessException(he);
} finally {
SessionFactoryUtils
.closeSessionIfNecessary(session, sessionFactory);
}
}
_________________ I am the creatxr of world.
|