gpt4 book ai didi

java.lang.NoClassDefFoundError : Could not initialize class Hibernate. util.HibernateUtil

转载 作者:行者123 更新时间:2023-12-01 15:02:10 25 4
gpt4 key购买 nike

以下是我的 HibernateUtil 类:

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;
}
}

我的 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";
}
}

但是当我运行 web-app 时我有异常:

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

有什么想法吗?可能 HibernateUtil 必须在任何地方注册吗?

最佳答案

我认为你的 Classpath 中没有 mysql-connector.jar

关于java.lang.NoClassDefFoundError : Could not initialize class Hibernate. util.HibernateUtil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24754029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com