简体   繁体   中英

Could not find hibernate.cfg.xml

I'm trying to learn how to use Hibernate. I have created a small project following a tutorial but when I try to run it, it throws this exception: http://pastebin.com/pu2FnmmT

Seems to me hibernate.cfg.xml can not be found. I know this has probably been asked before, and all solutions say I have to place hibernate.cfg.xml in the /src folder. Which is what I did, but for some reason this error keeps popping up.

Beneath I've pasted the code involved:

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>

My project structure is as follows: https://puu.sh/tl50v/31cb4b0da9.png

Any help or advice would be greatly appreciated.

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

Did you put:

<hibernate-mapping package="model">

in device.hbm.xml ?

And check if your jar are correctly in your project.

When i use hibernate i don't give "hibernate.cfg.xml" in configure and i put

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

without hibernate.

Sorry i wasn't able to respond during the weekend. I've fixed it, seems i downloaded a incorrect version of hibernate which didn't contain all the needed .JAR files.

After replacing those it worked like a charm. Thanks for your help guys! Case closed!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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