public class HibernateExecutor {
public static void main(String[] args) {
boolean success = true;
Transaction transaction = null;
Configuration config = new Configuration();
config.configure();
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
(中略)
try {
list = shainDao.findShainByHQL();
dispShain(list, "HQL");
list = shainDao.findShainByCriteria();
dispShain(list, "Criteria");
transaction = session.beginTransaction();
busho = new Busho();
busho.setBushoId(6);
busho.setBushoName("商品部");
busho.setExtensionNum(new Integer(7777));
bushoDao.saveBusho(busho);
list = bushoDao.findBusho();
dispBusho(list, "挿入結果");
busho.setExtensionNum(new Integer(8888));
list = bushoDao.findBusho();
dispBusho(list, "更新結果");
bushoDao.deleteBusho(busho);
list = bushoDao.findBusho();
dispBusho(list, "削除結果");
} catch(Exception e) {
success = false;
e.printStackTrace();
} finally {
if(success) {
transaction.commit();
} else {
transaction.rollback();
}
session.clear();
factory.close();
}
}
public static void dispShain(List list, String str) {
System.out.println("-----検索結果(" + str + ")-----");
Shain shain = null;
for(int i = 0; i < list.size(); i++) {
shain = (Shain)list.get(i);
System.out.println("社員ID = " + shain.getShainId());
System.out.println("社員名 = " + shain.getShainName());
System.out.println("部署名 = "
+ shain.getBusho().getBushoName());
}
System.out.println("\n");
}
(中略)
}
|
|