// --- create a new cat
CategoryIF catA = builder.createCategory(null, "News Category");
Long catId = null;
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(catA);
tx.commit();
catId = new Long(catA.getId());
System.out.println("Saved category with id " + catId + " persistently");
}
catch (HibernateException he) {
if (tx != null) tx.rollback();
throw he;
}
finally {
session.close();
}
// --- update
session = handler.getSession();
try {
tx = session.beginTransaction();
Category theCat = (Category) session.load(Category.class, catId);
theCat.setTitle("Another category title");
tx.commit();
System.out.println("Updated category title for id: " + catId);
} catch (HibernateException he) {
if (tx != null) tx.rollback();
throw he;
}
finally {
session.close();
}
// --- list
session = handler.getSession();
try {
tx = session.beginTransaction();
// Query q = session.createQuery("select cat.id from Category as cat");
// List result = q.list();
Query q = session.createQuery("from Category as cat where cat.title = :title");
q.setParameter("title", "Another category title", Hibernate.STRING);
List cats = q.list();
tx.commit();
Iterator it = cats.iterator();
while (it.hasNext()) {
Category c = (Category) it.next();
System.out.println("--> " + c.getId());
}
} catch (HibernateException he2) {
if (tx != null) tx.rollback();
throw he2;
}
finally {
session.close();
}