In order to learn hibernate, I write two examples for practising. However, both of them have same error as following:
Failed to create sessionFactory object.java.lang.NoClassDefFoundError: javax/transaction/SystemException Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.NoClassDefFoundError: javax/transaction/SystemException at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.jboss.logging.Logger.getMessageLogger(Logger.java:2248) at org.jboss.logging.Logger.getMessageLogger(Logger.java:2214) at org.hibernate.cfg.Configuration.(Configuration.java:184) at com.example.ManageEmployee.main(ManageEmployee.java:17)
Basically, I write POJO first, and using eclipse generates hbm.xml. After than, I write main function to manage the database. I tried twice but got same problem.
Could someone give me advice to solve this problem? Before that, using JDBC builds a project, but that is too complex. So I need to learn hibernate. Thank you.
Supplement(detail in this hibernate example project):
- My Eclipse project Name: HibernateExa
hibernate.cfg.xml:
<session-factory> <!-- hibernate dialect --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">hibernateTest</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest;</property> <property name="hibernate.connection.username">hibernater</property> <property name="hibernate.default_schema">hibernatetest</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Automatic schema creation(begin) --> <property name="hibernate.hbm2ddl.auto">create</property> <!-- Simple memory-only cache --> <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- mapping files with external dependencies --> <mapping resource="com/sample/Person.hbm.xml"/> </session-factory>My POJO is Person.java. Using eclipse generates Person.hbm.xml.
Project contains main function: TestPerson.java
public static void main(String [] args){ Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); createPerson(session); queryPerson(session); } public static void createPerson(Session session){ Person person = new Person(); person.setName("Jack"); person.setSurname("Yu"); person.setAddress("White House"); session.save(person); } private static void queryPerson(Session session){ Query query = session.createQuery("from person"); List<Person> list = new ArrayList<Person>(); list = query.list(); java.util.Iterator<Person> iter = list.iterator(); while(iter.hasNext()){ Person person = iter.next(); System.out.println("Person: \"" + person.getName() + "\", "+ person.getSurname() + "\", " + person.getAddress()); } session.getTransaction().commit(); }Here is error info:
Initial SessionFactory creation failed. java.lang.NoClassDefFoundError: javax/persistence/EntityListeners Exception in thread "main" java.lang.ExceptionInInitializerError at com.sample.SessionFactoryUtil.(SessionFactoryUtil.java:17) at com.sample.TestPerson.main(TestPerson.java:14) Caused by: java.lang.NoClassDefFoundError: javax/persistence/EntityListeners at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:97) at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1331) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1756) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840) at com.sample.SessionFactoryUtil.(SessionFactoryUtil.java:13) ... 1 more Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 7 more
Basically, this example could help me to practise how to using hibernate. Nevertheless, I got problem at the very beginning that create the project.
Hopefully, I could solve this problem soon. Please some advice, Thank you.
