Java – simple examples of errors in Hibernate beginner level
In order to learn hibernate, I wrote two examples of exercises However, they all have the following errors:
Basically, I first write POJOs and use eclipse to generate HBM xml. After that, I wrote the main functions of managing the database I tried twice, but I had the same problem
Can someone give me some advice to solve this problem? Before that, the project was built using JDBC, but it was too complicated So I need to learn hibernate thank you.
Supplement (details in this hibernate sample 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 to generate person hbm. xml. > Project contains the 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();
}
>This is the error message:
Initial sessionfactory creation failed java. Lang. NoClassDefFoundError: javax / persistence / entitylisteners thread "main" The exception in lang.exceptionininitializererror is found in com sample. SessionFactoryUtil. (sessionfactoryutil. Java: 17) on COM sample. TestPerson. Main (testperson. Java: 14) causes: Java Lang. NoClassDefFoundError: javax / persistence / entitylisteners at org hibernate. cfg. annotations. reflection. JPAMetadataProvider. Getdefaults (jpametadataprovider. Java: 97) is available at org hibernate. annotations. common. reflection. java. JavaReflectionManager. Getdefaults (javareflectionmanager. Java: 226) is available at org hibernate. cfg. Configuration. Secondpasscompile (configuration. Java: 1331) is available at org hibernate. cfg. Configuration. The buildsessionfactory (configuration. Java: 1756) is located at org hibernate. cfg. Configuration. Buildsessionfactory (configuration. Java: 1840) is available on COM sample. SessionFactoryUtil. (sessionfactoryutil. Java: 13)... There is another cause: Java lang.ClassNotFoundException:javax. persistence. Entitylisteners in Java net. urlclassloader $1. Run (unknown source) in 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 (unknow source)... There are 7 more
Basically, this example can help me practice how to use hibernate Nevertheless, I encountered the problem of creating projects at the beginning
I hope I can solve this problem as soon as possible Please give me some suggestions. Thank you
Solution
Make sure that your lib folder contains all these jar files:
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
