找不到hibernate.cfg.xml

时间:2017-01-13 17:19:05

标签: java xml hibernate

我正在尝试学习如何使用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

非常感谢任何帮助或建议。

3 个答案:

答案 0 :(得分:0)

如果您使用eclipse&#34; Run As&gt;运行您的代码Java应用程序&#34;选项...将文件(hibernate.cfg.xml)作为src文件夹的silibing文件...

答案 1 :(得分:0)

你放了:

<hibernate-mapping package="model">
在device.hbm.xml中?

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

当我使用hibernate时,我不会给出&#34; hibernate.cfg.xml&#34;在配置和我把

  1. connection.driver_class
  2. connection.username
  3. connection.password
  4. connection.url
  5. 没有休眠。

答案 2 :(得分:0)

对不起我周末没有回应。我已修复它,似乎我下载了一个不正确版本的hibernate,它没有包含所有需要的.JAR文件。

更换后,它就像一个魅力。谢谢你的帮助! 结案!