3

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):

  1. My Eclipse project Name: HibernateExa
  2. 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> 

  3. My POJO is Person.java. Using eclipse generates Person.hbm.xml.

  4. 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(); } 
  5. 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.

4
  • You're missing jta.jar from the project libraries or dependencies in Eclipse. (Or another equivalent jar which contains the missing class.) Commented Sep 9, 2013 at 23:34
  • Thanks madth3, I add jta now, but still has this issue. Commented Sep 10, 2013 at 14:41
  • If you added JTA correctly you would not have the same exact issue. Please add information about your Eclipse project and how do you execute it. Commented Sep 10, 2013 at 14:43
  • Hi madth3, I put more details. Wish we could find problem on it. Commented Sep 10, 2013 at 15:21

4 Answers 4

6

Please make sure you have all of this jar files in your lib folder:

lib/antlr.jar lib/cglib.jar lib/asm.jar lib/commons-collections.jar lib/commons-logging.jar lib/jta.jar lib/dom4j.jar lib/log4j.jar lib/hibernate3.jar 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Fahimeh, Thank you for your suggestion. I download the hibernate-4.3.0 and get the require jar file that contains: antlr-2.7.7 avro-1.6.3 dom4j-1.6.1 hibernate-commons-annotations-4.0.2 hibernate-core-4.2.2 jackson-core-asl-1.9.2 jackson-mapper-asl-1.9.2 javassist-3.15.0-GA jboss-logging-3.1.0 jta-1.1 lucene-core-3.6.2 paranamer-2.3 slf4j-api-1.6.1 snappy-java-1.0.4.1 But still has same problem.
1

Problem is solved! Thanks for everyone's advice.

So I summary the issue and give the solution.

ERROR: Initial SessionFactory creation failed. java.lang.NoClassDefFoundError. some jar files are misssing.

In order to set hibernate project, here is the list for jar files:

enter image description here

  1. Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: person is not mapped [from person] Because hibernate query is based on Object, from person should be from Person.
  2. The correct connection url:

    jdbc:mysql://localhost:3306/hibernatetest?useUnicode=true&characterEncoding=GBK

  3. In DB, the id is INT. However, in Person.hbm.xml I change id type to "long". Thus, the expected result comes out.

1 Comment

Eric, Does these all jars are include in our hibernate-release5.2.14.Final file or Do we have to dowload them from some where?
1

Add this dependency in your pom.xml

<dependency> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> <version>1.1</version> </dependency> 

Make a mvn clean install and deploy.

Another way:
Download this jar : Project -->right click --> build path --> add external jar

Project-->right click -->properties --> deployment assembly --> add --> add from build path entries (add this jar)

Now re build the project.

Hope it will help.

Comments

0

Place below dependency inside your pom.xml or add persistence-api jar in your classpath.

<dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.