2

The following is my HibernateUtil class:

package Hibernate.util;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

My controller:

package Controllers;
import DAOImpl.ProductDAOImpl;
import Entities.Product;
import java.sql.SQLException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ProductController {

    @RequestMapping("/products.htm")
    public String getAllProducts() throws SQLException
    {
        ProductDAOImpl mapping = new ProductDAOImpl();
        Product p = new Product();
        p.setCost(1000);
        p.setName("Саморезы");
        mapping.addProduct(p);
        return "index";
    }
}

But when I run web-app I have excpetion:

java.lang.NoClassDefFoundError: Could not initialize class Hibernate.util.HibernateUtil
    DAOImpl.ProductDAOImpl.addProduct(ProductDAOImpl.java:24)
    Controllers.ProductController.getAllProducts(ProductController.java:20)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52

Any Idea? Might HibernateUtil must be registered in any place?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • possible duplicate of [How to Solve java.lang.NoClassDefFoundError?](http://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror) (did you even google?) – Theolodis Jul 15 '14 at 09:06
  • @Theolodis Of course, I google. But it doesnot solve the problem. – St.Antario Jul 15 '14 at 09:10
  • So your path is corrected? You did add Hibernate to the project path? – Theolodis Jul 15 '14 at 09:14
  • 1:1 copy of [java.lang.NoClassDefFoundError: Could not initialize class org.com.hibernate.HibernateUtil](http://stackoverflow.com/questions/14951395/java-lang-noclassdeffounderror-could-not-initialize-class-org-com-hibernate-hib?rq=1) – Theolodis Jul 15 '14 at 09:16
  • @Theolodis I have already said that it _does not work_. I check my HibernateUtil.class and Hibernate Library. – St.Antario Jul 15 '14 at 09:21
  • 1
    This link may help you http://stackoverflow.com/questions/7325579/java-lang-noclassdeffounderror-could-not-initialize-class-xxx – Wundwin Born Jul 15 '14 at 09:24

2 Answers2

4

i think you don't have mysql-connector.jar in your Classpath

lennon310
  • 12,503
  • 11
  • 43
  • 61
yakup_y
  • 155
  • 2
  • 7
1

This class first of all must be on your classpath. And then make sure that ProductDAOImpl ge the sessionFactory(to eventually provide the session) via HibernateUtil.getSessionFactory().

You are anyway free to define this class as a bean in you application context if you like to do so.

xrcwrn
  • 5,339
  • 17
  • 68
  • 129
Puneetsri
  • 234
  • 2
  • 7