Simple Hibernate
save - get - load –delete – entity class objects
save
====
public int saveIteam(Item item) {
Integer itemCode = 0;
try {
log.info("Before saveItem - Dao start");
session = getSessionFactory().openSession();
transaction = session.beginTransaction();
log.info("Before addItem - Dao");
itemCode = (Integer) session.save(item);
//item.setItemName("itemName");
transaction.commit();
} catch (HibernateException hibernateException) {
transaction.rollback();
log.info(hibernateException.getMessage());
} finally {
//session.close();
}
return itemCode;
}
get
===
public Item getItem(int itemCode) {
try {
session = getSessionFactory().openSession();
transaction = session.beginTransaction();
item = (Item) session.get(Item.class,itemCode);
transaction.commit();
} catch (HibernateException hibernateException) {
transaction.rollback();
log.info(hibernateException.getMessage());
} finally {
session.close();
}
return item;
}
load
====
public Item loadItem(int itemCode) {
Item item = null;
try {
session = getSessionFactory().openSession();
transaction = session.beginTransaction();
item = (Item) session.load(Item.class,itemCode);
transaction.commit();
} catch (HibernateException hibernateException) {
transaction.rollback();
log.info(hibernateException.getMessage());
} finally {
session.close();
}
return item;
}
delete
======
public void deleteItem(int itemCode) {
try {
session = getSessionFactory().openSession();
transaction = session.beginTransaction();
Item item = new Item();
//item.setItemCode(itemCode);
item = (Item) session.load(Item.class,itemCode);
session.delete(item);
transaction.commit();
} catch (HibernateException hibernateException) {
transaction.rollback();
log.info(hibernateException.getMessage());
} finally {
session.close();
}
}
No comments:
Post a Comment