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

发布于 2024-08-25 23:56:40 字数 2084 浏览 5 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喵星人汪星人 2024-09-01 23:56:40

Hibernate 现在使用 Simple Logging Facade for Java (SLF4J),正如其名称所示,它是各种日志框架实现(例如 java.util.logging、log4j、logback)的外观。由于使用其中之一取决于用户,因此由用户为日志框架和 SLF4J“绑定"(将 SLF4J 与实现“绑定”的 jar)位于类路径上。如果 slf4j 绑定丢失,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了解更多详情?我将其粘贴在下面以供记录:

无法加载类org.slf4j.impl.StaticLoggerBinder


org.slf4j.impl.StaticLoggerBinder
类无法加载到内存中。
当没有合适的 SLF4J 时会发生这种情况
可以在类上找到绑定
小路。放置一个(且仅一个)
slf4j-nop.jarslf4j-simple.jar
slf4j-log4j12.jarslf4j-jdk14.jar
logback-classic.jar 在类路径上
应该可以解决问题。

您可以从以下位置下载 SLF4J 绑定
项目下载页面

该消息非常明确:您需要添加 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

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

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:

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.

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.

奢欲 2024-09-01 23:56:40

您缺少库,请前往此处下载 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文