Location of hibernate.cfg.xml in the project?

I created a project with the following structure:

enter image description here

HibernateUtil:

public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml Configuration configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml"); return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { // Close caches and connection pools getSessionFactory().close(); } } 

in line

 Configuration configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml"); 

I have a mistake

Initial creation of SessionFactory failed.org.hibernate.HibernateException: C: \ Users \ Nikolay_Tkachev \ working \ hiberTest \ SRC \ logic \ hibernate.cfg.xml not found Exception in thread "main" java.lang.ExceptionInInitializerError at logic.HibernateUtilbuildbutbubu (HibernateUtil.java:19) in logic.HibernateUtil. (HibernateUtil.java:9) at logic.Main.main (Main.java:12) Called: org.hibernate.HibernateException: C: \ Users \ Nikolay_Tkachev \ working \ hiberTest \ SRC \ logic \ hibernate.cfg.xml not found org.hibernate.internal.util.ConfigHelper.getResourceAsStream (ConfigHelper.java:173) at org.hibernate.cfg.Configuration.getConfigurationInputStream (Configuration.java:1947) at org.hibernate.cfg.Configuration.avafigure ( 1928) in the logic .HibernateUtil.buildSessionFactory (HibernateUtil.java:14) ... 2 more

What is the cause of the error and how to fix it?

+15
java orm hibernate
Sep 11 '13 at 8:48 on
source share
9 answers

Give a path regarding your project.

Create a resources folder in src and place your configuration file there.

  configuration.configure("/resources/hibernate.cfg.xml"); 

And if you check your code

 Configuration configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml"); return new Configuration().configure().buildSessionFactory(); 

In two lines, you create two configuration objects.

This should work (not verified) if you write

 Configuration configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml"); return configuration.buildSessionFactory(); 

But it failed after deploying to the server, since you are using the system path than the relative path of the project.

+24
Sep 11 '13 at 8:54 on
source share

Somehow the placement in the "src" folder did not work for me.

Instead, put cfg.xml as shown below:

 [Project Folder]\src\main\resources\hibernate.cfg.xml 

worked. Using this code

 new Configuration().configure().buildSessionFactory().openSession(); 

in the file under

  [Project Folder]/src/main/java/com/abc/xyz/filename.java 

Also this piece of code in hibernate.cfg.xml

 <mapping resource="hibernate/Address.hbm.xml" /> <mapping resource="hibernate/Person.hbm.xml" /> 

The above hbm.xml files are located in the section:

EDIT:

 [Project Folder]/src/main/resources/hibernate/Address.hbm.xml [Project Folder]/src/main/resources/hibernate/Person.hbm.xml 

Work on the add-in.

+11
Dec 29 '13 at 4:16
source share

You can put the file "hibernate.cfg.xml" in the src folder (src \ hibernate.cfg.xml), and then run the configuration as the code below:

 Configuration configuration = new Configuration(); sessionFactory =configuration.configure().buildSessionFactory(); 
+4
Sep 11 '13 at 8:55
source share

try under the code, it will solve your problem.

 Configuration configuration = new Configuration().configure("/logic/hibernate.cfg.xml"); 
+2
Sep 13 '13 at 9:08 on
source share

This is an example of reality when setting up the folder structure: Folder structure and initialize the HibernateUtil class

enter image description here

from:

 return new Configuration().configure("/config/hibernate.cfg.xml").buildSessionFactory(); 

display: enter image description here


with setting up mapping of entity files:

  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <mapping class="com.vy.entities.Users"/> <mapping class="com.vy.entities.Post"/> <mapping resource="config/Users.hbm.xml"/> <mapping resource="config/Post.hbm.xml"/> </session-factory> </hibernate-configuration> 

(Note: The simplest way, if you follow the default, means that all xml configuration files inside the src folder, when build sessionFactory, only:

 return new Configuration().configure().buildSessionFactory(); 

)

+2
Jan 09 '16 at 10:20
source share

Another reason this exception occurs is to call the configure method on the Configuration or AnnotatedConfiguration object twice, for example this -

 AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(MyClass.class); //Use this if config files are in src folder config.configure(); //Use this if config files are in a subfolder of src, such as "resources" config.configure("/resources/hibernate.cfg.xml"); 

Btw, this project structure is inside an eclipse.

+1
Apr 24 '14 at 9:53 on
source share

My problem was that I had a patch pattern in the resorces folder. After removing it

 config.configure(); 

worked for me. With the structure src / java / ... HibernateUtil.java and cfg file under src / resources.

+1
Jun 23 '16 at 9:09
source share

Using configure () method twice is responsible for this problem for me. Instead of this:

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

Now I use it like this, the problem no longer exists.

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

PS: My hibernate.cfg.xml file is also on the page "src / main / resources / hibernate.cfg.xml". The code works for me. when sleeping-5

 public class HibernateUtil { private static SessionFactory sessionFactory ; static { try{ Configuration configuration = new Configuration(); configuration.configure("/main/resources/hibernate.cfg.xml"); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); sessionFactory = configuration.buildSessionFactory(builder.build()); } catch(Exception e){ e.printStackTrace(); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } 
0
Jan 11 '17 at
source share

For a Maven project, create a folder called resources in src / main folder and add a resource folder as the source folder in your class path.

This can be done by selecting "Customize build path" and then "Add folder" to the "Sources" tab.

Then check the resource folder and click "Apply."

Then just use:

 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
0
Apr 24 '17 at 8:31 on
source share



All Articles