Location of hibernate.cfg.xml in project?
Give the path relative to your project.
Create a folder called resources
in your src
and put your config 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 are creating two configuration objects.
That should work(haven't tested) if you write,
Configuration configuration = new Configuration().configure( "C:\\Users\\Nikolay_Tkachev\\workspace\\hiberTest\\src\\logic\\hibernate.cfg.xml");
return configuration.buildSessionFactory();
But It fails after you deploy on the server,Since you are using system path than project relative path.
Where to place hibernate.cfg.xml?
The config file hibernate.cfg.xml
needs to be on the classpath
.
This can be accomplished in different ways, depending on your project.
For a web-app WAR project (you are running the program in a Servlet container):
placing it inWEB-INF/classes
will work as files inWEB-INF/classes
are visible on the classpath when app is running in container.For a Maven-style project (not running the program in a Servlet container): placing it in
/src/main/resources/
will workFor otherwise, try in the
src/
directory.
web application - where to place hibernate.cfg.xml file?
I always put it into WEB-INF/classes
directory (compiled files are stored there).
Hibernate file location in maven web application
create at src/main/resources/hibernate.cfg.xml
the project structure should be like this
you can follow this link for further clarification
How a JAR can load the hibernate.cfg.xml external file?
The problem was that StandardServiceRegistry was trying to load hibernate.cfg.xml from the resources and the resources were not embedded in JAR.
I move the configuration file to the sub-folder 'config' and loaded it using File class, like this:
File config = new File("./config/hibernate.cfg.xml");
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure(config)
.build();
try
{
sessionFactory = new MetadataSources(registry)
.buildMetadata().buildSessionFactory();
}
catch (Exception e) {
e.printStackTrace();
StandardServiceRegistryBuilder.destroy( registry );
throw e;
}
Related Topics
Java String.Indexof and Empty Strings
Possible to Use Two Java Classes with Same Name and Same Package
Class Object of Generic Class (Java)
Having a Column Name as Input Parameter of a Preparedstatement
Multiple Axes on the Same Data
Uninitialized Object VS Object Initialized to Null
How to Attach Multiple Files to an Email Using Javamail
Superclass Reference Not Able to Call Subclass Method in Java
Stax Xmlstreamreader Check for the Next Event Without Moving Ahead
Jackson: How to Add Custom Property to the JSON Without Modifying the Pojo
Why Doesn't My Compare Work Between Char and Int in Java
Spring MVC: How to Return Custom 404 Errorpages
Accessing Post Variables Using Java Servlets
Gson and Deserializing an Array of Objects with Arrays in It