【问题标题】:configure Hibernate to use a renamed persistence.xml配置 Hibernate 以使用重命名的 persistence.xml
【发布时间】:2011-11-10 09:34:12
【问题描述】:

我们必须重命名persistence.xml 以欺骗WebSphere 7 不使用其内置的OpenJPA。

当你使用 Spring 时,这很容易做到,你只需指示它的实体管理器工厂为 persistence.xml 使用另一个位置:

<property name="persistenceXmlLocation" value="META-INF/persistence-xxx.xml"/>

但是现在我们想在没有 Spring 的情况下使用普通的 Hibernate/JPA,并且找不到任何方法来指定备用 persistence.xml 位置。

JPA2 规范对此没有任何说明...

有什么线索吗?是否可以指示 Hibernate 使用重命名的 persistence.xml?

======

似乎不可能让 Hibernate 读取重命名的 persistence.xml 文件。 在我的情况下没有必要。

【问题讨论】:

    标签: java hibernate jpa-2.0


    【解决方案1】:

    据我所知,无法更改persistence.xml 文件的位置。

    还请查看此文档:Alternate JPA Providers in WebSphere Application Server,您似乎应该能够通过在persistence.xml 文件中指定 Hibernate 作为 JPA 提供程序,并将所需的 jars 嵌入到您的应用程序中。

    确保您的 persistence.xml 将 Hibernate 指定为 JPA 提供者:

    <persistence>
        <persistence-unit name="myapp">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
    

    您还应该能够通过创建一个共享库并使用它来配置 WebSphere 以使用替代的持久性提供程序来实现这一点。 在这里您可以找到操作方法:Configuring the Java Persistence API (JPA) default persistence provider

    编辑 鉴于此答案中 cmets 中的信息,似乎可以通过在persistence.xml 中添加这些属性来解决问题,如本文Websphere EntityManagerFactory creation problem 中所示:

    <property name="hibernate.transaction.manager_lookup_class"
    value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
    
    <property name="hibernate.transaction.factory_class"
    value="org.hibernate.transaction.CMTTransactionFactory" />
    

    WebSphere Application Server 文档中的备用 JPA 提供程序也提供了相同的信息。

    【讨论】:

    • 谢谢!每次我们安装应用程序、编写指令等时,都会涉及到额外的安装过程。但如果我们没有找到其他方法并且这是唯一的选择 - 我们将不得不这样做......
    • 另外,请查看此文档:Alternate JPA Providers in WebSphere Application Server。看起来您应该能够通过在 persistence.xml 中指定 provider 并在应用程序中嵌入所需的 jar 来使用休眠。请参阅我对答案的编辑。
    • Xavi,我们确实指定了提供者...但它只是导致 The server cannot create an EntityManagerFactory factory for the xxx persistent unit from the org.hibernate.ejb.HibernatePersistence provider in jar:file:/C:/IBM/WebSphere/wp_profile/installedApps/XXX/xxx-ear.ear/xxx-ejb.jar!/ module. 当然这可能是一个单独的问题...
    • 谢谢!!!我只是没想到解决方案会如此奇怪...我实际上更愿意重命名持久性 xml,而不是让它感知 WebSphere
    【解决方案2】:

    persistence.xml 应该存在于 META-INF 目录中,通常与包含实体类的 jar 文件一起打包。我所做的是我在 eclipse 下的一个单独项目中拥有实体类,其中包含一个包含 persistence.xml 的 META-INF 目录,将其打包为一个 jar 文件,并将其包含在应用程序项目依赖项中(即 WEB-INF/ lib),或者直接部署到应用服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 2016-02-23
      • 2023-03-03
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多