【问题标题】:Could not find hibernate.cfg.xml找不到 hibernate.cfg.xml
【发布时间】:2017-01-13 17:19:05
【问题描述】:

我正在尝试学习如何使用 Hibernate。我按照教程创建了一个小项目,但是当我尝试运行它时,它抛出了这个异常: http://pastebin.com/pu2FnmmT

在我看来hibernate.cfg.xml 找不到。我知道这可能以前被问过,所有解决方案都说我必须将hibernate.cfg.xml 放在 /src 文件夹中。我就是这样做的,但由于某种原因,此错误不断弹出。

下面我粘贴了相关代码:

HibernateUtil.java

package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import model.Device;

public class HibernateUtil {

    public static void main(String[] args) {

        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");

        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
        builder.applySettings(configuration.getProperties());
        ServiceRegistry serviceRegistery = builder.build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistery);

        Session session = sessionFactory.openSession();
        Device device = new Device();
        device.setId(4);
        device.setName("Test Smart TV");

        Transaction tx = session.beginTransaction();
        session.save(device);
        tx.commit();
        System.out.println("Object saved successfully.....!!");
        session.close();
        sessionFactory.close();
    }
}

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>
       <!-- Database connection settings -->
       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
       <property name="hibernate.connection.username"></property>
       <property name="hibernate.connection.password"></property>
       <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/poc</property>

       <!-- SQL dialect -->
       <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

       <!-- Echo all executed SQL to sysout -->
       <property name="show_sql">true</property>

       <!-- Drop and re-create the database schema on startup -->
       <property name="hibernate.hbm2ddl.auto">create</property>
       <!-- Mapping file -->
       <mapping resource="device.hbm.xml" />

    </session-factory>

</hibernate-configuration>

我的项目结构如下: https://puu.sh/tl50v/31cb4b0da9.png

任何帮助或建议将不胜感激。

【问题讨论】:

    标签: java xml hibernate


    【解决方案1】:

    如果您使用 eclipse“运行方式 > Java 应用程序”选项运行代码...将文件 (hibernate.cfg.xml) 作为 src 文件夹的 silibing 文件...

    【讨论】:

    • 您好,感谢您的回答。但它已经存在,如下所示:puu.sh/tl50v/31cb4b0da9.png。还是你的意思是别的?
    • 根据你的图片,hibernate.cfg.xml在src下!!它应该在您项目中的 src 文件夹的同一级别!
    【解决方案2】:

    你放了吗:

    <hibernate-mapping package="model">
    

    在 device.hbm.xml 中?

    并检查您的 jar 是否在您的项目中正确。

    当我使用休眠时,我没有在配置中给出“hibernate.cfg.xml”,而是放了

    1. connection.driver_class
    2. connection.username
    3. connection.password
    4. connection.url

    没有休眠。

    【讨论】:

      【解决方案3】:

      抱歉,我无法在周末回复。我已经修复了,似乎我下载了不正确的休眠版本,它没有包含所有需要的 .JAR 文件。

      在替换那些之后,它就像一个魅力。谢谢你们的帮助! 结案!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-26
        • 1970-01-01
        • 1970-01-01
        • 2017-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多