gpt4 book ai didi

java - hibernate 代码不适用于实时服务器,但适用于本地主机

转载 作者:行者123 更新时间:2023-12-02 11:00:20 25 4
gpt4 key购买 nike

我在Hibernate中遇到问题。我的代码在本地服务器上有效,但不在实时服务器上。

    public void saveEmployee(Emp employee) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
session.save(employee);
transaction.commit();
session.close();
}

用于数据保存。对于HibernateUtil:
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
* Hibernate Utility class with a convenient method to get Session Factory
* object.
*
* @author Jitendra
*/
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;
}

}

使用此代码。

和堆栈跟踪错误是
java.lang.NoClassDefFoundError: Could not initialize class javainfinite.DAOImpl.HibernateUtil
at javainfinite.DAOImpl.EmployeeDaoImpl.showAllEmployees(EmployeeDaoImpl.java:37)
at Controller.EmployeeController.doPost(EmployeeController.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:285)
at org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:2376)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

enter image description here

最佳答案

几天前我遇到了同样的错误。 “NoClassDefFoundError”异常告诉您,编译器/生成器未发布您的类。因此,它在您的实时系统上不可用。

就我而言,我想将导入的Java类发布到tomcat服务器。我的解决方案是为tomcat调整部署描述符,并添加导入的类以将其发布到库文件夹。

关于java - hibernate 代码不适用于实时服务器,但适用于本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43758997/

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