【问题标题】:Hibernate Configuration does not work休眠配置不起作用
【发布时间】:2015-11-16 18:30:41
【问题描述】:

我真的要疯了。我有这样的代码:

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;

}

我已经在 Eclipse 中使用包含包和 hibernate.cfg.xml 文件的 src 文件夹设置项目。当我使用 Eclipse 运行项目时,客户端可以正常进行会话(>beforeafter

现在:我还有一个 Ant 目标来运行我的项目,并且我确定 hibernate.cfg.xml 在类路径中,但是当我运行程序时,应用程序不会打印 >afterCaused by: java.lang.IllegalStateException: Could not locate SessionFactory in JNDI。

有人吗?谢谢。

【问题讨论】:

    标签: java eclipse hibernate ant


    【解决方案1】:

    问题是文件不在正确的位置。我增强了我的 Ant 脚本,以使用以下目标将资源复制到我的类路径的根目录中。

    <target name="copy-resources">
        <copy todir="${targetdir}">
            <fileset dir="${sourcedir}">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    

    【讨论】:

      【解决方案2】:
      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);
      

      【讨论】:

      • 感谢您的回复 kamel2005,但您能详细说明一下吗?
      • 好的,你可能缺少这个属性:cf.setProperty("hibernate.connection.datasource", datasourceJNDIName); "datasourceJNDIName" 字符串是服务器端的 jndi 名称
      • 其他强制属性如用户名密码驱动程序类驱动程序名称和URL,
      猜你喜欢
      • 2016-12-29
      • 2015-10-28
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 2013-10-16
      • 2013-09-25
      相关资源
      最近更新 更多