views:

350

answers:

2

Hi;

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
+3  A: 

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.

marcos
+4  A: 

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.

Pascal Thivent