简体   繁体   English

Hibernate 5:找不到配置文件

[英]Hibernate 5 : can't find config file

I'm discovering Hibernate today, and I'm struggling while trying to make a minimal program find my "hibernate.cfg.xml" configuration file. 我今天发现了Hibernate,并且在尝试使一个最小的程序找到我的“ hibernate.cfg.xml”配置文件时遇到了麻烦。

Here is what I already tried : 这是我已经尝试过的:

My main : 我的主要:

package testHibernate;

import org.hibernate.cfg.Configuration;

public class Main
{
    public static void main(String[] args)
    {
        Configuration configuration = new Configuration().configure();
    }
}

at the same level of this main file : 在此主文件的同一级别:

hibernate.cfg.xml hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
       <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
       <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/pianissimo</property>
       <property name="hibernate.connection.username">admin</property>
       <property name="hibernate.connection.password">nx5fDdVdEGVDS6Tq</property>
    </session-factory>
</hibernate-configuration>

Any help will be greatly appreciated :) Sincerely, Alann 任何帮助将不胜感激:)真诚的,Alann

You shouldn't put hibernate.cfg.xml on the same level as your Main class. 您不应将hibernate.cfg.xmlMain类放在同一级别。 You should put it on the same level as your testHibernate package, in the root of the source folder. 您应该将其放在源文件夹根目录中与testHibernate包相同的级别。 Or you need to specify a path to hibernate.cfg.xml like this 或者您需要像这样指定hibernate.cfg.xml的路径

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM