I kept having problem running my first Hibernate Entity Manager standalone application (build with Java SE 1.6) adapted from the original tutorial HelloWorld.java which I had success in running with the Hibernate Core 3.2. It always complained about the missing class org.hibernate.MappingException even after I included all of the jar files in distributed libraries. I tried to create the build.xml to copy the persistence.xml to the bin/META-INF but the problem persisted.
Here is the exception stack trace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/MappingException
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:119)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at hello.HelloWorld.main(HelloWorld.java:19)
Hibernate version:
Hibernate Entity Manager 3.3.1GA
Mapping documents:
My persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation='http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/ ... ce_1_0.xsd version="1.0"'>
<persistence-unit name="manager" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.ejb.cfgfile"
value="/hibernate.cfg.xml"></property>
<property name="hibernate.archive.autodetection" value="class" />
</properties>
</persistence-unit></persistence>
My hibernate.cfg.xml:
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Use the C3P0 connection pool provider -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">300</property>
<property name="c3p0.max_statements">50</property>
<property name="c3p0.idle_test_period">3000</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Print formated SQL -->
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!--<mapping resource="hello/Message.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():
// The exception occurred on this very first line.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Message message = new Message("Hello World");
em.persist(message);
tx.commit();
em.close();
EntityManager em2 = emf.createEntityManager();
EntityTransaction tx2 = em2.getTransaction();
tx2.begin();
List messages = em2.createQuery("select m from Message m order by m.text asc")
.getResultList();
System.out.println(messages.size() + " mesage(s) found: ");
for (Object m : messages)
{
Message loadedMsg = (Message) m;
System.out.println(loadedMsg.getText());
}
tx2.commit();
em2.close();
Full stack trace of any exception that occurs:
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/MappingException
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:119)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at hello.HelloWorld.main(HelloWorld.java:19)
Name and version of the database you are using:
MySQL connector 3.1.13 and MySQL Server 4.1