java.lang.NoClassDefFoundError: org/dom4j/Attribute
at honey.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:38)
at honey.TestClient.createHoney(TestClient.java:43)
at honey.TestClient.main(TestClient.java:28)
Exception in thread "main"
public class TestClient {
public static void main(String[] args) {
Integer primaryKey = createHoney();
System.out.println("primary key is " + primaryKey);
updateHoney(primaryKey);
listHoney();
}
private static Integer createHoney() {
Session session = null;
Transaction tx = null;
Logger log = Logger.getLogger("TestClient");
log.info("creating honey");
Integer id = null;
System.out.println("Ok");
try {
// get the session from the factory
session = HibernateSessionFactory.currentSession();
// always start a transaction before doing something
// from the database
tx = session.beginTransaction();
// create a new object
Honey honey = new Honey();
honey.setName("Sebastian's favourite honey");
honey.setTaste("sweet");
// save it to the database, Hibernate returns your object
// with the primary key field updated!
id = (Integer) session.save(honey);
// commit your transaction or nothing is wrote to the db
tx.commit();
// clean up (close the session)
session.close();
} catch (HibernateException e) {
// when an error occured, try to rollback your
// transaction
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e1) {
log.warn("rollback not successful");
}
/*
* close your session after an exception!! Your session
* is in an undefined and unstable situation so throw it away!
*
*/
if (session != null)
try {
session.close();
} catch (HibernateException e2) {
log.warn("session close not successful");
}
}
return id;
}
private static void updateHoney(Integer primaryKey) {
Session session = null;
Transaction tx = null;
Logger log = Logger.getLogger("TestClient");
log.info("updating honey");
try {
// get the session from the factory
session = HibernateSessionFactory.currentSession();
// always start a transaction before doing something
// (even reading) from the database
tx = session.beginTransaction();
// load object from the database
Honey honey = (Honey) session.get(Honey.class, primaryKey);
// honey is null when no object was found
if (honey != null) {
honey.setName("Sascha's favourite honey");
honey.setTaste("very sweet");
}
session.flush();
// commit your transaction or nothing is wrote to the db
tx.commit();
// clean up (close the session)
session.close();
} catch (HibernateException e) {
// when an error occured, try to rollback your
// transaction
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e1) {
log.warn("rollback not successful");
}
/*
* close your session after an exception!! Your session
* is in an undefined and unstable situation so throw it away!
*
*/
if (session != null)
try {
session.close();
} catch (HibernateException e2) {
log.warn("session close not successful");
}
}
}
private static void listHoney() {
Session session = null;
Transaction tx = null;
Logger log = Logger.getLogger("TestClient");
log.info("listing honey");
try {
// get the session from the factory
session = HibernateSessionFactory.currentSession();
// always start a transaction before doing something
// (even reading) from the database
tx = session.beginTransaction();
List honeys = session.find("select from Honey");
for (Iterator iter = honeys.iterator(); iter.hasNext();) {
Honey honey = (Honey) iter.next();
System.out.println("Id " + honey.getId() + " Name "
+ honey.getName());
}
// commit your transaction or nothing is wrote to the db
tx.commit();
// clean up (close the session)
session.close();
} catch (HibernateException e) {
// when an error occured, try to rollback your
// transaction
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e1) {
log.warn("rollback not successful");
}
/*
* close your session after an exception!! Your session
* is in an undefined and unstable situation so throw it away!
*
*/
if (session != null)
try {
session.close();
} catch (HibernateException e2) {
log.warn("session close not successful");
}
}
}
}
|