gpt4 book ai didi

java - Hibernate 将数据保存到数据库时出现的问题

转载 作者:行者123 更新时间:2023-12-01 04:30:59 25 4
gpt4 key购买 nike

我正在尝试借助 Java 中的 Hibernate 将数据保存到数据库。但是当我运行代码时,我遇到了很多问题。谁能帮我解决这个问题?谢谢...

我的代码:

package org.ultimania.model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {

public static void main(String[] args) {
Session session = null;
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Transaction transaction = session.getTransaction();

BusinessCard card = new BusinessCard();
card.setId(1);
card.setName("Özgür");
card.setDescription("Acıklama");

try{
transaction.begin();
session.save(card);
transaction.commit();
} catch(Exception e){
e.printStackTrace();
}
finally{
session.close();
}
}
}

问题:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Exception in thread "main" java.lang.NoClassDefFoundError:
org/slf4j/impl/StaticLoggerBinder
at org.slf4j.LoggerFactory.getSingleton(LoggerFactory.java:223)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:120)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:269)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:255)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:152)
at org.ultimania.model.Test.main(Test.java:14)
Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 8 more

最佳答案

Hibernate 现在使用 Simple Logging Facade for Java (SLF4J),顾名思义,它是各种日志框架实现(例如 java.util.logging、log4j、logback)的外观。由于使用一个或另一个取决于用户,由用户将日志框架和 SLF4J 的 jar“binding”(将 SLF4J 与实现“绑定(bind)”的 jar)放在类路径上。如果缺少 slf4j 绑定(bind),SLF4J 会输出以下警告消息

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

这里的好处是错误提供了一个自解释链接。那么,您看到http://www.slf4j.org/codes.html#StaticLoggerBinder了吗?了解更多详情?我将它粘贴在下面以供记录:

Failed to load class org.slf4j.impl.StaticLoggerBinder

This error is reported when the org.slf4j.impl.StaticLoggerBinder class could not be loaded into memory. This happens when no appropriate SLF4J binding could be found on the class path. Placing one (and only one) of slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.

You can download SLF4J bindings from the project download page.

消息很清楚:您需要添加一个 SLF4J binding .目前,我的建议是使用 slf4j-simple.jar(它将所有事件输出到 System.err)。从上面给出的链接获取 jar 并将其添加到应用程序的类路径中。如果您想进行更高级的日志记录,请稍后更改此设置。

关于java - Hibernate 将数据保存到数据库时出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2458774/

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