Hibernate在数据库上保存数据时的问题

ale*_*lex 2 java hibernate slf4j

我正在尝试通过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();
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

问题 :

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
Run Code Online (Sandbox Code Playgroud)

Pas*_*ent 5

Hibernate现在使用Simple Logging Facade for Java(SLF4J),正如其名称所示,它是各种日志框架实现的外观(例如java.util.logging,log4j,logback).由于使用其中一个或另一个取决于用户,因此用户可以在类路径上为日志框架和SLF4J" 绑定 "(将SLF4J与实现" 绑定 "的jar)放入jar .如果缺少slf4j绑定,SLF4J将输出以下警告消息

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Run Code Online (Sandbox Code Playgroud)

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

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

org.slf4j.impl.StaticLoggerBinder 无法将类加载到内存中时会报告此错误 .如果在类路径上找不到合适的SLF4J绑定,则会发生这种情况.在类路径上放置一个(并且只有一个) slf4j-nop.jar,slf4j-simple.jar, slf4j-log4j12.jar,slf4j-jdk14.jarlogback-classic.jar应该可以解决问题.

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

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