When I use a simple class test (HibernateTest.java) .it works well and I can save my object in my mysql Database, but when I use it in a WAR I couldn't save my bean into Database.
this is my HibernateTest.java :
package com.live.presentation;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.live.hibernate.util.HibernateUtil;
import com.liver.beans.User;
import com.live.business.UserBusiness;
import com.live.business.UserBusinessImp;
import com.live.dao.UserDao;
import com.live.dao.UserDaoMysql;
public class HibernateTest {
private Session session;
private void openSession(){
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
session.beginTransaction();
}
private void closeSession(){
session.getTransaction().commit();
session.close();
}
public HibernateTest() {
UserBusiness userBusiness = new UserBusinessImp();
userBusiness.add(new User("Lee", "lee@gmail.com", "pass00Z"));
}
public static void main(String[] args) {
new HibernateTest();
}
}
Business layer has UserBusiness interface and UserBusinessImp class.
interface UserBusiness :
package com.liver.business;
import com.live.beans.User;
public interface UserBusiness {
public void add(User user);
}
class UserBusinessImp :
package com.live.business;
import com.live.beans.User;
import com.live.dao.UserDao;
import com.live.dao.UserDaoMysql;
public class UserBusinessImp implements UserBusiness{
public void add(User user) {
UserDao userDao = new UserDaoMysql();
userDao.insert(user);
}
}
DAOlayer has UserDao interface and UserDaoMysql class.
interface UserDao :
package com.liver.dao;
import com.live.beans.User;
public interface UserDao {
public void insert(User user);
}
class UserDaoMysql:
package com.live.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.live.hibernate.util.HibernateUtil;
import com.live.beans.User;
public class UserDaoMysql implements UserDao {
private Session session;
private void openSession(){
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
session.beginTransaction();
}
private void closeSession(){
session.getTransaction().commit();
session.close();
}
public void insert(User user) {
openSession();
User p = new User(user.getName(), user.getEmail(), user.getPassword());
session.save(p);
System.out.println("sauvegarde reussi");
closeSession();
}
}
Now when I would like to save my data while click on sign up button it doesn't work.
public void save(){
System.out.println("name :"+name);
UserBusiness userBusiness = new UserBusinessImp();
userBusiness.add(new User(name, email,password));
}
}
so when I click on sign up for save data, it throws an exception with below root cause:
Caused by: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
at com.live.dao.UserDaoMysql.openSession(UserDaoMysql.java:26)
at com.live.dao.UserDaoMysql.insert(UserDaoMysql.java:50)
at com.live.business.UserBusinessImp.add(UserBusinessImp.java:11)
at com.live.beans.User.save(User.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 25 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
... 37 more
I don't understand my problem , and why it's work when I use class hibernateTest and doesn't work in a WAR. Anybody can help me please
Updated
<project xmlns="http://ift.tt/IH78KX" xmlns:xsi="http://ift.tt/ra1lAU" xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/VE5zRx">
<modelVersion>4.0.0</modelVersion>
<groupId>p01</groupId>
<artifactId>p01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<name>HibernateTest</name>
<url>http://ift.tt/19pvvEY;
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.1.0.CR2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
</dependencies>
</project>
Aucun commentaire:
Enregistrer un commentaire