Hibernate - problem z utworzeniem konfiguracji w servlecie

0

Cześć

Zaczynam poznawanie frameworka Hibernate i mam mały problem.
Na stronie mam formularz ,który przesyła POST'em dane do servletu.
Servlet odczytuje dane - na tym etapie jest wszystko ok.
Dane te chciałbym zapisać do bazy z użyciem hibernate.
Jednak wyskakuje mi taki oto exception

Nazwa servletu : Test

Servlet.service() for servlet [com.tomek.ReadData] in context with path [/Test] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration

Plik hibernate.cfg.xml - w katalogu src

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:mysql://localhost/extdb</property>
    <property name="connection.username">mojUser</property>
    <property name="connection.password">mojehaselko</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
    <property name="show_sql">true</property>
 
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
 
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <property name="current_session_context_class">thread</property>
 
    <!-- Mapping files will go here.... -->

    
    <mapping class="com.tomek.UserBean" /> // moj obiekt do zapisu
  </session-factory>
</hibernate-configuration>

I kod servletu Test

WebServlet("/ReadData")
public class ReadData extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ReadData() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String userFirstName = request.getParameter("name");
		String userLastName = request.getParameter("lastname");
		
		UserBean user = new UserBean();
		user.setUserName(userFirstName);
		user.setUserLastName(userLastName);
		
		System.out.println(user.toString());
		
		// Mamy dane
		// Wkladamy dane do bazy danych
		
		// Odpalam Hibernate
		@SuppressWarnings("deprecation")
		SessionFactory sesionFactory = new Configuration().configure().buildSessionFactory();
		Session session = sesionFactory.openSession();
		session.beginTransaction();
		session.save(user);
		session.getTransaction().commit();
		session.close();
		
		
	}

}

Dodam ,że gdy tworze sobie testowa klasie javy w pakiecie com.tomek z metoda main to zapis do bazy danych dziala

Mam dziwne przeczucie ,że podczas wdrożenia aplikacji do kontenera servletow ( uzywam Tomcat'a ) nie sa wrzucane libsy z hibernate.

0

Wyjątek by na to wskazywał ;]
A czy wrzucasz te liby do tomcata? Prawy klik w eclipse, properties, deployment descriptor - co tam masz?

0

Już sobie poradziłem . Prosty bład: nie dodałem libsów hibernate do WEB-INF/libs
Pojawił mi się kolejny problem D

Tworzę sobie servlet i daje na niego w eclipse run as -> run on server -> Tomcat7 .
I mam bład :

HTTP Status 404 - /HibernateTest2/Test

type Status report

message /HibernateTest2/Test

description The requested resource (/HibernateTest2/Test) is not available.

Do stronki index.jsp dostaje się normalnie.
Zastanawiałem się nad mapowaniem servletu w pliku web.xml ale bez tego eclipse tez mi odpalał servlety
Moj web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HibernateTest2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
0

Ok problem polega prawdopodobnie na tym ze mam wyłączone automatyczne buildowanie w eclipse.
Po uruchomieniu narzedzia clean servlet zostal ladnie wdrożony.
Pozdrawiam

1 użytkowników online, w tym zalogowanych: 0, gości: 1