在hibernate中使用注释的异常

时间:2016-03-20 12:48:30

标签: java hibernate

伙计们我是hibernate的新手..我正在尝试在hibernate中使用注释但是它给了我一个例外..这里是我的代码..任何建议..提前感谢

在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;
    }


}
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();

    }

}

例外

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

2 个答案:

答案 0 :(得分:0)

不加载Hibernate所需的所有依赖项,如果使用maven,引用的所有jar都会自动加载到Application类路径中。

正如你的错误清楚地说,你错过了对sl4j jar的引用。

https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=7&cad=rja&uact=8&ved=0ahUKEwiBxfuDrM_LAhWCVI4KHU2uBZYQFghAMAY&url=http%3A%2F%2Fmvnrepository.com%2Fartifact%2Forg.slf4j%2Fslf4j-api&usg=AFQjCNFZmEX-pLO1rqWxEyCRGohyjvgEFw

答案 1 :(得分:0)

异常非常明确:Hibernate需要类org.slf4j.LoggerFactory,但未找到。您需要将相应的库添加到类路径中,即除了hibernate.jar之外还需要slf4j.jar。