繁体   English   中英

在Glassfish 3 Web服务中使用Hibernate

[英]Using Hibernate inside a Glassfish 3 web service

我有一个使用Hibernate进行持久化的Java应用程序。 现在,我试图将此应用程序的一部分作为部署到Glassfish 3.0.1的Web服务公开。

实现该服务的代码位于JAR文件中,该文件与WAR文件一起部署到了Glassfish中。 WAR中的服务代码可以访问JAR文件中使用Hibernate的类。

但是,当我尝试调用该服务时,抛出一个异常,表明Hibernate无法找到hibernate.cfg.xml文件。 为了我的一生,我无法弄清楚.cfg文件应该在JAR,WAR或Glassfish中的位置,以便可以在类路径中找到它。

我尝试将其放在WAR文件的META-INF目录中(当我检查WAR时实际上是“ meta-inf”),并放在JAR的同一目录中。 我也尝试在几个不同的Glassfish目录中创建一个META-INF目录,但无济于事。

在Eclipse中开发此服务时,我不得不花很多时间在哪里放置.cfg文件,以便使其进入类路径。 似乎也将其部署到GF时就是问题所在。

谁能告诉我.cfg文件应该放在哪里,以便在GF中部署此服务时可以对其进行访问?

谢谢,

克里斯

如果您正在使用no-arg Configure#configure()方法,则hibernate.cfg.xml文件应位于类路径的根目录下。

这是ohcConfiguration.java的相关代码段:

/**
 * Use the mappings and properties specified in an application
 * resource named <tt>hibernate.cfg.xml</tt>.
 */
public Configuration configure() throws HibernateException {
    configure( "/hibernate.cfg.xml" );
    return this;
}

/**
 * Use the mappings and properties specified in the given application
 * resource. The format of the resource is defined in
 * <tt>hibernate-configuration-3.0.dtd</tt>.
 * <p/>
 * The resource is found via <tt>getConfigurationInputStream(resource)</tt>.
 */
public Configuration configure(String resource) throws HibernateException {
    log.info( "configuring from resource: " + resource );
    InputStream stream = getConfigurationInputStream( resource );
    return doConfigure( stream, resource );
}

这实际上记录在《 Hibernate Core参考指南》中:

3.7。 XML配置文件

另一种配置方法是在名为hibernate.cfg.xml的文件中指定完整配置。 该文件可以代替hibernate.properties文件,或者如果两者都存在,则可以覆盖属性。

默认情况下,XML配置文件应位于CLASSPATH的根目录中。 (...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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