【问题标题】:Got Exception in thread "main" org.hibernate.HibernateException: hibernate.cgf.xml not found while running hibernate application在线程“主”org.hibernate.HibernateException 中出现异常:运行休眠应用程序时未找到 hibernate.cgf.xml
【发布时间】:2017-06-28 18:21:53
【问题描述】:

我的 Hibernate 应用程序需要检索存储在 MySQL 数据库中的数据。但我成功将数据保存到 DB 中,但检索失败。

错误日志

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.HibernateException: hibernate.cgf.xml not found
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2035)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2016)
    at mypack.DataInsertion.getInfo(DataInsertion.java:39)
    at mypack.DataInsertion.main(DataInsertion.java:12)

hibernate.cfg.xml(配置文件)

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<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/hibdb</property>
   <property name="connection.user">root</property>
   <property name="connection.password">admin</property>
<!-- Related to the connection END -->     

<!-- Related to the hibernate properties START -->
   <property name="show_sql">true</property>
   <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Related to the hibernate properties END -->     

<!-- List of XML mapping files -->
   <mapping resource="user.hbm.xml"/>

</session-factory>
</hibernate-configuration>

user.hbm.xml(映射文件)

<?xml version="1.0"?>               <!-- Mapping File to POJO Class -->
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="mypack.DataProvider" table="user_info">
<id name="user_id" column="id">
<generator class="assigned"/>
</id>

<property name="user_name" column = "name" />
<property name="user_address" column = "address" />

</class>

</hibernate-mapping>

DataProvider.java(POJO 类)

package mypack;                      //POJO Class

public class DataProvider {

private int user_id;
private String user_name;
private String user_address;

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_address() {
    return user_address;
}
public void setUser_address(String user_address) {
    this.user_address = user_address;
}

}

DataInsertion.java(实现逻辑)

打包我的包;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class DataInsertion {

    public static void main(String[] args) {
        //new DataInsertion().insertInfo();
        new DataInsertion().getInfo();

    }

    public void insertInfo()
    {
        Configuration con = new Configuration();        //interation with hib
        con.configure("hibernate.cfg.xml");            //registering to xml

        SessionFactory SF = con.buildSessionFactory(); //creating session
        Session session = SF.openSession();           //opening new session
        DataProvider provider = new DataProvider();
        provider.setUser_id(1);
        provider.setUser_name("Goutham");
        provider.setUser_address("Hunsur");

        Transaction TR = session.beginTransaction();
        session.save(provider);
        System.out.println("Object saved successfully");
        TR.commit();                                 //saving transaction
        session.close();
        SF.close();
    }

   public void getInfo()
   {
       Configuration con = new Configuration();
       con.configure("hibernate.cgf.xml");

       SessionFactory SF = con.buildSessionFactory();
       Session session = SF.openSession();

       Object obj = session.load(DataProvider.class,new Integer(1));     //We are binding data into obj from DataProvider class
       DataProvider dp = (DataProvider) obj;                        //Typecasting into DataProvider
       System.out.println("Name:"+dp.getUser_name());
       System.out.println("Address:"+dp.getUser_address());
       session.close();
       SF.close();
   }

}

请给我建议,

谢谢。

【问题讨论】:

    标签: java xml hibernate exception jakarta-ee


    【解决方案1】:

    你的文件名有误 hibernate.cgf.xml 应该是 hibernate.cfg.xml

    【讨论】:

    • 对不起,这是愚蠢的打字错误,我没认出来。
    【解决方案2】:

    您在 getInfo 方法中输入了错误的名称 hibernate.cgf.xml。这就是您面临问题的原因

    【讨论】:

    • 对不起,这是愚蠢的打字错误,我没认出来。
    猜你喜欢
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2016-04-04
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多