Hibernate将数据保存到数据库时出现问题
我正在尝试通过 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
I'm trying do save data to database by help of Hibernate , in Java. But when ı run codes , ı had lot of problems. Can anyone help me about that?
Thanks...
My code:
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();
}
}
}
Problems :
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Hibernate 现在使用 Simple Logging Facade for Java (SLF4J),正如其名称所示,它是各种日志框架实现(例如 java.util.logging、log4j、logback)的外观。由于使用其中之一取决于用户,因此由用户为日志框架和 SLF4J“绑定"(将 SLF4J 与实现“绑定”的 jar)位于类路径上。如果 slf4j 绑定丢失,SLF4J 会输出以下警告消息
好处是该错误提供了一个自我解释的链接。那么,您是否看到http://www.slf4j.org/codes.html# StaticLoggerBinder了解更多详情?我将其粘贴在下面以供记录:
该消息非常明确:您需要添加 SLF4J 绑定。目前,我的建议是使用 slf4j-simple.jar (它将所有事件输出到
System.err
)。从上面给出的链接获取 jar 并将其添加到应用程序的类路径中。如果您想进行更高级的日志记录,请稍后更改此设置。Hibernate now uses Simple Logging Facade for Java (SLF4J) which, as its name indicates, is a facade to various logging framework implementations (e.g. java.util.logging, log4j, logback). And since using one or the other depends on the user, it's up to the user to put jars for the logging framework and for the SLF4J "binding" (the jar that "binds" SLF4J with the implementation) on the class path. In case the slf4j binding is missing, SLF4J outputs the following warning message
The nice thing here is that the error provides a self explaining link. So, did you See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details? I'm pasting it below for the record:
The message is pretty clear: you need to add a SLF4J binding. For now, my suggestion would be to use slf4j-simple.jar (which outputs all events to
System.err
). Get the jar from the link given above and add it to the class path of your application. Change this later if you want to do more advanced logging.您缺少库,请前往此处下载 slf4j jar,然后将 jar 放入您的 claspspath 中。
如果我没记错的话,slf4j-api 与 hibernate 一起分发,只需将它和 slf4j-simple 添加到应用程序的类路径中即可。
You have a missing library, go here and download the slf4j jars, then put the jars in your claspspath.
If i'm not mistaken, slf4j-api is distributed along with hibernate, just add it and slf4j-simple to your application's classpath.