1

I have a problem with my hibernate.cfg.xml in IntelliJ IDE.

Here is my hibernate config file:

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

<hibernate-configuration>
    <session-factory>
       <!--  Database connection settings -->
       <property name="connection.driver:class">org.postgresql.Driver</property>
       <property name="connection.url">jdbc:postgresql://localhost/HIndex</property>
       <property name="hibernate.connection.username">index_user</property>
       <property name="hibernate.connection.password">password</property>

       <!--  JDBC connection pool (use the built-in) -->
       <property name="connection.pool_size">1</property>

       <!--  SQL Dialect -->
       <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</property>

       <!-- Assume test is the database name -->
       <property name="show_sql">true</property>

       <!--  Drop and re-create the database schema on startup -->
       <property name="hbm2ddl.auto">create</property>

       <!--  Names the annotated entity class -->
       <mapping class="HIndexSaar.HIndex.Person"/>
       <mapping class="HIndexSaar.HIndex.University"/>
       <mapping class="HIndexSaar.HIndex.Publication"/>
    </session-factory>
</hibernate-configuration>

And this is my application class:

package HIndexSaar.HIndex;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateManager {

    private static SessionFactory factory;

    public HibernateManager(){
        //
        //* Setup the configuration.
        //
        Configuration config = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Person.class)               .addAnnotatedClass(University.class).addAnnotatedClass(Publication.class);
        factory = config.buildSessionFactory();
    }

I get an error by running the following code:

package HIndexSaar.HIndex;

public class AppHibernate {

    public static void main(String[] args){
        HibernateManager mng = new HibernateManager();
        [...]
    }
}

So something seems to be broken in building the SessionFactory in the HibernateManager. I get the error

Could not locate cfg.xml resource

and the following stacktrace:

Feb 27, 2016 10:26:03 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.7.Final}
Feb 27, 2016 10:26:03 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 27, 2016 10:26:03 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "main"        org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
at org.hibernate.cfg.Configuration.configure(Configuration.java:245)
at HIndexSaar.HIndex.HibernateManager.<init>(HibernateManager.java:18)
at HIndexSaar.HIndex.AppHibernate.main(AppHibernate.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

I already have placed the hibernate.cfg.xml file under /src/main/java/resources but obviously something goes still wrong. Does somebody have an idea what could be the mistake I made? Surprisingly, the same code runs in Eclipse without error, so what could be the problem of IntelliJ? I use the same SDK/JDK, namely jdk 1.7.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
tschens
  • 39
  • 2
  • 7
  • Place your hibernate.cfg.xml file under the src folder then try to execute your program – Mohsin AR Feb 27 '16 at 22:04
  • Just tried that, but did not solve the problem :( – tschens Feb 27 '16 at 22:16
  • If your are using intellij then try to create new module with hibernate framework support then paste this same code and execute it. – Mohsin AR Feb 27 '16 at 22:34
  • This might be path difference in eclipse and intellij – Mohsin AR Feb 27 '16 at 22:34
  • Ok, I will try this, hope it will work. The Hibernate framework support is already enabled for the current module :( – tschens Feb 27 '16 at 22:38
  • Thanks a lot for this tip, I created a new module, with hibernate framework support, and this bug is fixed, now I am getting another error conserning javax.persistence.Table.indexes()... have to use google for further research – tschens Feb 27 '16 at 23:09
  • This is an other question try to post it. And please vote up my answer. – Mohsin AR Feb 27 '16 at 23:15

1 Answers1

2

If your are using intellij then try to create new module with hibernate framework support then paste this same code and execute it. This might be path difference in clipse and intellij

Mohsin AR
  • 2,998
  • 2
  • 24
  • 36