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?

6

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.