guys i am newbie in hibernate .. i am trying to use annotation in hibernate but it gives me an exception .. here is my code .. any suggestions .. thanks in advance
in hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration>
<session-factory>
<!-- Related to the connection START -->
<property name="connection.driver_class">com.mysql.jdbc.Driver </property>
<property name="connection.url">jdbc:mysql://localhost:3306/mydb </property>
<property name="connection.user">root </property>
<property name="connection.password">root</property>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START -->
<property name="show_sql">true</property>
<property name="dialet">org.hibernate.dialet.MYSQLDialet</property>
<property name="hbm2ddl.auto">create</property>
<!-- Related to hibernate properties END-->
<!-- Related to mapping START-->
<mapping resource="user.hbm.xml" />
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
DataProvider.java
import javax.persistence.*;
@Entity
@Table(name="dataprovider")
public class DataProvider {
@Id @GeneratedValue
@Column(name="id")
private int user_id;
@Column(name="name")
private String user_name;
@Column(name="description")
private String user_desc;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_desc() {
return user_desc;
}
public void setUser_desc(String user_desc) {
this.user_desc = user_desc;
}
}
in InsertData.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class InsertData {
private static SessionFactory factory;
public static void main(String[] args) {
factory = new AnnotationConfiguration().configure("hibernate.cfg.xml").addAnnotatedClass(DataProvider.class)
.buildSessionFactory();
new InsertData().insertInfo();
}
public void insertInfo() {
Session session = factory.openSession();
DataProvider provider = new DataProvider();
provider.setUser_id(121);
provider.setUser_name("name");
provider.setUser_desc("desc");
Transaction tr = session.beginTransaction();
session.save(provider);
System.out.println("Object Saved");
tr.commit();
session.close();
factory.close();
}
}
the exception
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.hibernate.cfg.annotations.Version.<clinit>(Version.java:12)
at org.hibernate.cfg.AnnotationConfiguration.<clinit>(AnnotationConfiguration.java:78)
at InsertData.main(InsertData.java:11)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
All dependencies required by Hibernate are not loaded, if you use maven all jars referenced are automatically loaded in the Application classpath.
As your error is clearly saying , you are missing a reference to the sl4j jar.
The exception is quite clear: the class org.slf4j.LoggerFactory
is required by Hibernate, but was not found. You need to add the corresponding Library to your classpath, ie you need an slf4j.jar in addition to hibernate.jar.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.