简体   繁体   English

为什么即使我的xml文件在maven项目的src / main / resources中,也找不到hibernate.cfg.xml?

[英]Why I get hibernate.cfg.xml not found even if my xml file is in src/main/resources in maven project?

I am building an maven-based app where I want to use Hibernate for work with DB. 我正在构建一个基于Maven的应用程序,我想在其中使用Hibernate与DB一起工作。 I read a lot of articles where writes that, when I have Maven project, I must put my xml file into src/main/resources. 我读了很多文章,其中写到:当我有Maven项目时,必须将xml文件放入src / main / resources。 And I have done that but I am again getting error. 我已经做到了,但是我又一次出错了。 This is my xml file: 这是我的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">""</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/database</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <property name="show_sql">true</property> 
        <property name="connection.pool_size">1</property>

        <mapping class="pojo.Student"></mapping>

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

I added this file into JavaResources/src/main/resources. 我将此文件添加到JavaResources / src / main / resources中。

This is my FirstDemo class: 这是我的FirstDemo类:

public class DemoFirst {
 public static void main(String[] args) {

        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Student student = new Student();
        student.setFirstName("Bob");
        student.setAge(26);

        session.save(student);
        session.getTransaction().commit();

        session.close();

    }

And this is error: 这是错误的:

 Exception in thread "main" java.lang.ExceptionInInitializerError
    at pack.DemoFirst.main(DemoFirst.java:12)
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1953)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1934)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1914)
    at pack.HibernateUtil.<clinit>(HibernateUtil.java:17)
    ... 1 more

I don't know much about Maven, but "normally" your hibernate.cfg.xml should be named precisely like that and goes directly in your classes-folder (the "Output directory", as it is named in Eclipse) 我对Maven不太了解,但是“通常”您的hibernate.cfg.xml应该这样精确地命名,并直接在您的类文件夹(“输出目录”,在Eclipse中被命名)中

I hope that helps?! 希望对您有帮助吗?

Configuration cfg = new Configuration();
cfg.configure("main/resources/hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();

Student student = new Student();
student.setFirstName("Bob");
student.setAge(26);

session.save(student);
session.getTransaction().commit();

session.close();

could you please try this. 你能试试这个吗? it may help you. 它可能会帮助您。

请将您的hibernate.cfg.xml移至src / main / webapp / WEB-INF,它应该可以工作。

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

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