首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到hibernate.cfg.xml

找不到hibernate.cfg.xml
EN

Stack Overflow用户
提问于 2017-01-13 17:19:05
回答 3查看 741关注 0票数 2

我在努力学习如何使用Hibernate。我在教程之后创建了一个小项目,但是当我尝试运行它时,它会抛出以下异常:http://pastebin.com/pu2FnmmT

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

在下面我粘贴了所涉及的代码:

HibernateUtil.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-01-16 10:23:26

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

在替换了这些之后,它就像一种魅力。谢谢你们的帮助!案子结案了!

票数 0
EN

Stack Overflow用户

发布于 2017-01-13 17:44:39

如果您使用eclipse "Run >“选项运行代码.将该文件(hibernate.cfg.xml)作为src文件夹的silibing文件.

票数 0
EN

Stack Overflow用户

发布于 2017-01-13 19:03:21

你有没有说:

代码语言:javascript
复制
<hibernate-mapping package="model">

在device.hbm.xml?

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

当我使用hibernate时,我不会在配置中给出"hibernate.cfg.xml“,而是将

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

没有冬眠。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41640068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档