5

I have my hibernate.cfg.xml located at src/main/resources/ but I keep getting this error when I run my maven project, I have also tried putting the path to the file in the configuration like this but still got the error

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

What am I doing wrong?

And when I go to the properties of the project and go to Source I see /src/main/resources in the build path?

It also runs when I make it in eclipse, but when I export to jar it stops working but I have it in class path as you can seeenter image description here

Thanks

Edit

Here is my directory in my eclipse project

enter image description here

And then here is it when I open the .jar file

enter image description here

Are you saying the .xml should be in the root directory?

3
  • which version of hibernate you are using Commented Aug 7, 2015 at 11:53
  • @dom hibernate 4.1.9 Commented Aug 7, 2015 at 15:12
  • remove string argument from configure and do clean build project and run by defining main class Commented Aug 8, 2015 at 4:47

6 Answers 6

6
+25

There is no /src/main/resources in your built project, that is just where maven keeps the files before building. While building the project (I assume you are building a .jar-artifact) maven will do the following:

  1. All .java-files in src/main/java will be compiled to .class-files and will be moved to the subdirectory of the jar corresponding to their package
  2. All files from src/main/resources will be copied to the root of the jar-file

So this file-structure in your project:

src
|
+-main
   |
   +-java
   |   |
   |   +- mypackage
   |          |
   |          +- MyClass.java
   |
   +-resources
         |
         +- hibernate.cfg.xml

will end up in your .jar-file like this:

project.jar
|
+- mypackage
|     |
|     +- MyClass.class
|
+- hibernate.cfg.xml

The essential point is: All files from /src/main/resources will end up in the classpath of your built project. But since your configuration-file is named hibernate.cfg.xml and is located on the classpath you should be OK just doing

Configuration conf = new Configuration(); 
conf.configure();

Note that configure() is called wthout an argument, so that means "Look for a file called hibernate.cfg.xml on your current classpath". So try removing the argument from your configure-call.

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

4 Comments

I guess it would be a bit more clear, if you would add an example of a file structure before and after building.
So you are saying what I am doing should be fine, I thought so too, but its not working could you explain a little more about what you mean iwht the .war and stuff
@Jan thanks for the suggestion, Tried to add it to the answer
@spenf10 I tried to rework my answer to make it clearer. Note what I say about the arguments to the configure()-call
4

When using maven with hibernate, one needs only active the shade plugin and hibernate.cfg.xml will go into the right place, such that you can run the file using java -jar jarfile.jar. The stanza you need is something like:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.example.jpa.Main</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

To generate the appropriate uberjar, type mvn package and Bob's your uncle.

6 Comments

Are you saying to put the above xml in my pom.xml? And then do mvn package in the terminal?
Yes, that is precisely what I'm saying.
okay I did that and got BUILD SUCCESS and then what do I just export as .jar and run it?
Don't export as jar and run it, do so from the command line and run it as a jar
Okay I am trying to run this on my server, so you are saying add the project folder to my server -> cd to that folder -> call mvn package -> and then run java -jar jarfile.jar
|
3

First clear one thing that if you are using maven with hibernate so there is no need to define the name of hibernate.cfg.xml in calling configure()

close your project then re-open it use below code to generate session factory you need hibernate 4 or above

public class HibernateUtils {

    private static SessionFactory sessionFactory = null;
    private static Logger logger = Logger.getLogger(HibernateUtils.class.getName());

    public static void createSessionFactory() {

        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());

        try {
            sessionFactory = configuration.buildSessionFactory(builder.build());
        } catch (Exception exe) {
            exe.printStackTrace();
        }

        logger.debug("Session factory created");
    }

    public static SessionFactory getSessionFactory() {

        return sessionFactory;
    }

    public static void shutdown() {

        if (sessionFactory != null)
            getSessionFactory().close();
    }
}

use the following main class for testing purpose

class Main {
    public static void main(String[] args) {
        HibernateUtils.createSessionFactory();
        SessionFactory sessionFactory = HibernateUtils.getSessionFactory();

        // do your  task 

        HibernateUtils.shutdown();
    }
}

Comments

2

Instead of:

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

Use:

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

Comments

2

Keep your config file under normal src directory itself, not to put under resources folder

Hibernate config [1]

And refer the file like this Configuration configuration = new Configuration();
configuration.configure("/hibernate.cfg.xml");

//Here i considered configuration class (HibernateSessionFactory.java)is there within the same package

I hope this helps !

Comments

0

If you are sure your file in resources folder then check this Right click on project->Properties->Deployment Assembly and check if your resource file map with Deploy Path or not

enter image description here

6 Comments

sorry I don't see Deployment Assembly?
Which IDE you are using?
Which version? please let me know
I am using version 4.5
Its a Web dynamic project? If yes which server you are using for deployment?
|

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.