简体   繁体   中英

Hibernate Configuration does not work

I am really going crazy here. I have code like this:

private SessionFactory getSessionFactory() {

  Configuration conf = new Configuration();

  System.out.println("before");
  conf.configure("hibernate.cfg.xml");
  System.out.println("after");

  StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(conf.getProperties());       
  SessionFactory sessionFactory = conf.buildSessionFactory(ssrb.build());
  return sessionFactory;

}

I have set up the project in Eclipse with a src folder which contains packages and the hibernate.cfg.xml file. When I run the project using Eclipse the client gets the session fine (>before< and >after< are printed).

Now: I also have an Ant target to run my project and I am sure the hibernate.cfg.xml is in the classpath, but when I run the program the application does not print >after<. No exception there, but ultimately I get Caused by: java.lang.IllegalStateException: Could not locate SessionFactory in JNDI .

Anybody? Thanks.

The problem was that the file was not in the right place. I enhanced my Ant script to copy the resource in the root of my classpath using the following target.

<target name="copy-resources">
    <copy todir="${targetdir}">
        <fileset dir="${sourcedir}">
            <exclude name="**/*.java"/>
        </fileset>
    </copy>
</target>
Configuration cf = new Configuration();

    //  cf.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    cf.setProperty("hibernate.connection.datasource", datasourceJNDIName);


    cf.configure("hibernate.cfg.xml");

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(cf.getProperties()).build();

    SessionFactory sessionFactory  = cf.buildSessionFactory(serviceRegistry);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM