0

I've trying to connect in MYSQL and I have error when try to add in configure next resource : hibernate.cfg.xml. And have next error org.hibernate.MappingNotFoundException: resource: hibernate.cfg.xml not found

HibernateUtil:

package util;

   Imports

   public class HibernateUtil {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;


    static{
        try {
            Configuration configuration = new Configuration();
            configuration.addResource("hibernate.cfg.xml");
            configuration.configure();
            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    } catch (HibernateException he) {
        System.err.println("Error creating Session: " + he);
        throw new ExceptionInInitializerError(he);
    }
}

public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Hibernate.cfg.xml:

<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://**.**.**.**/land_of_dkl_db</property>
        <property name="connection.username">*****</property>
        <property name="connection.password">****</property>
        <property name="connection.pool_size">10</property>
        <!-- Возможно потом нужно будет поставить MySQLInnoDBDialect диалект -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Логирование sql запросов в консоль -->
        <property name="show_sql">true</property>
        <!-- Есть недокументированная возможность none вместо validate, но вроде validate нас устраивает -->
        <property name="hbm2ddl.auto">validate</property>
        <!-- Спросить у преподов, что значит Enables autocommit for JDBC pooled connection -->
        <property name="hibernate.connection.autocommit">false</property>
        <property name="current_session_context_class">thread</property>

        <mapping class="DAO.logic.User" />

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

ProjectName.iml (I delete many trash, Import maven jetty and other):

<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="hibernate" name="Hibernate">
      <configuration>
        <datasource-map />
        <naming-strategy-map />
        <deploymentDescriptor name="hibernate.cfg.xml" url="file://$MODULE_DIR$/hibernate.cfg.xml" />
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
    <output url="file://$MODULE_DIR$/target/classes" />
    <output-test url="file://$MODULE_DIR$/target/test-classes" />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      <excludeFolder url="file://$MODULE_DIR$/target" />
    </content>
  </component>
</module>

Project Structure :

+project 
++src
+++main
++++java
+++++util
++++++HibernateUtil
++hibernate.cfg.xml
++project.iml

1 Answer 1

2

Try putting hibernate.cfg.xml under src/main/resources.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it is solved my problem. What the magic ? In project.iml I have the next row: <deploymentDescriptor name="hibernate.cfg.xml" url="file://$MODULE_DIR$/hibernate.cfg.xml" />
I'm not sure how IDEA is handling its internal files, but hibernate.cfg.xml needs to be in the project's classpath in order to be visible from code, and by (maven) default everything under src/main/java andsrc/main/resources is included in classpath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.