JPA 配置到底是什么?

发布于 2024-07-25 05:26:08 字数 2108 浏览 9 评论 0原文

我疯狂地试图让一个简单的 Envers 示例工作。 我被困在 org.hibernate.tool.ant.EnversHibernateToolTask​​ 上 - 看起来我终于得到了我需要的所有 jar 文件,但现在我收到了错误消息

[hibernatetool] Persistence unit not found: 'ConsolePU'.

BUILD FAILED
C:\deka\proj\java\test-database\build.xml:61: Persistence unit not found: 'ConsolePU'.

据我所知,持久性单元与 JPA persistence.xml 文件关联。 但我没有使用 persistence.xml 文件; 我正在使用 hibernate.cfg.xml — 但 envers 示例在 ant 任务中有一个

<hibernatetool destdir=".">
        <classpath>
             <fileset dir="src/">
                  <include name="**/*.hbm.xml"/>
            </fileset>

            <path location="${buildDir}" />
        </classpath>
    <jpaconfiguration persistenceunit="ConsolePU" />
    <hbm2ddl
        drop="false"
        create="true"
        export="false"
        outputfilename="versioning-ddl.sql"
        delimiter=";"
        format="true"/>
    </hibernatetool>

是否有一些我可以替换它的东西,以使其能够与 hibernate 一起工作。 cfg.xml 文件? 关于如何让所有这些东西正常工作的文档似乎为零。

编辑:好的,主要问题是我不了解 hibernatetool 选项以及什么适合我的应用程序。 我确实找到了 Hibernate ant 文档,幸运的是。 谢谢。 现在我有一个新问题:我正在使用注释,但我还为属性设置设置了 hibernate.cfg.xml 。 hibernatetool 任务只允许我运行 ,不能同时运行,甚至 不会工作,因为我已经有注释在做事情了。 如何将属性设置从 hibernate.cfg.xml 文件迁移到注释?

编辑:呃,我没有意识到你只是这样做:

<annotationconfiguration configurationfile="...filename..." />

根据hibernatetool 任务文档。

I'm going nuts trying to get a simple Envers example to work. I'm stuck on the org.hibernate.tool.ant.EnversHibernateToolTask — it looks like I finally got all the jar files I needed, but now I get the error message

[hibernatetool] Persistence unit not found: 'ConsolePU'.

BUILD FAILED
C:\deka\proj\java\test-database\build.xml:61: Persistence unit not found: 'ConsolePU'.

As far as I can tell, persistence units are associated with JPA persistence.xml files. But I'm not using a persistence.xml file; I'm using hibernate.cfg.xml — but the envers example has a <jpaconfiguration> in the ant task:

<hibernatetool destdir=".">
        <classpath>
             <fileset dir="src/">
                  <include name="**/*.hbm.xml"/>
            </fileset>

            <path location="${buildDir}" />
        </classpath>
    <jpaconfiguration persistenceunit="ConsolePU" />
    <hbm2ddl
        drop="false"
        create="true"
        export="false"
        outputfilename="versioning-ddl.sql"
        delimiter=";"
        format="true"/>
    </hibernatetool>

is there something that I can replace it with to get it to work with the hibernate.cfg.xml file? There seems to be ZERO documentation on how to get all this stuff to work properly.

edit: OK, so the main problem was I didn't understand the hibernatetool options and what was appropriate for my app. I did find the Hibernate ant docs, fortunately. Thanks. Now I have a new problem: I'm using annotations, but I also have set up a hibernate.cfg.xml for the properties settings. The hibernatetool task only lets me run either <configuration /> or <annotationconfiguration /> not both, and even <configuration /> won't work since I already have annotations doing things. How can I migrate my property settings from the hibernate.cfg.xml file to my annotations?

edit: Duh, I didn't realize you just do:

<annotationconfiguration configurationfile="...filename..." />

per the hibernatetool task docs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丶视觉 2024-08-01 05:26:08

替换为 标签,详见 Hibernate Tools 文档:

<configuration
    configurationfile="hibernate.cfg.xml"
    propertyfile="hibernate.properties"
    entityresolver="EntityResolver classname"
    namingstrategy="NamingStrategy classname">

Replace the <jpaconfiguration /> with the <configuration /> tag, as detailed in Hibernate Tools docs:

<configuration
    configurationfile="hibernate.cfg.xml"
    propertyfile="hibernate.properties"
    entityresolver="EntityResolver classname"
    namingstrategy="NamingStrategy classname">
万劫不复 2024-08-01 05:26:08

只是为了给您高水平的视角。
JPA是SUN提供的标准持久化API。
您可以使用任何持久性框架(如 Hibernate、TopLink、JDO 等)作为 JPA 的持久性提供

程序 JPA -----> 持久化提供者(Hibernate)。

使用 JPA 是一个很好的做法,因为它是标准库。

因此,您的持久性提供程序信息应该只有 JPA 知道,而不是您的代码特定的 XML。

这就是你的 persistence.xml 的样子

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="QuarkFrameworkPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>

,你的应用程序上下文的样子(依赖于 JPA ,没有提到 Hibernate)

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="com.mysql.jdbc.Driver" p:url="${db.url}" />




<!-- ADD PERSISTENCE SUPPORT HERE (jpa,etc) -->

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="QuarkFrameworkPU" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
        </bean>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource" />
</bean>

Just to give you high level prespective.
JPA is standard persistence API provided by SUN.
You can use any persistence framework like Hibernate,TopLink,JDO etc as persistence provider with JPA.

So just to make things clear

Your code -----> JPA ----->Persistence Provider(Hibernate).

Its will be good practice to use JPA as it is standard library.

So what is your persistence provider information should only be known to JPA and not your code specific XML's.

This is how your persistence.xml will look like

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="QuarkFrameworkPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>

And your Application context will look like (Dependent on JPA , no mention of Hibernate)

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="com.mysql.jdbc.Driver" p:url="${db.url}" />




<!-- ADD PERSISTENCE SUPPORT HERE (jpa,etc) -->

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="QuarkFrameworkPU" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
        </bean>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource" />
</bean>
爱,才寂寞 2024-08-01 05:26:08

您需要从头开始:Sun 的 JPA 教程

也可能有帮助。

您需要创建 persistence.xml 并将其放入项目的 META-INF 目录中。

Hibernate 是JPA 的一种具体实现,但还有其他实现(例如JDO)。

You need to start at the beginning: Sun's JPA tutorial.

This might help as well.

You need to create the persistence.xml and put it in the META-INF directory for your project.

Hibernate is one concrete implementation for JPA, but there are others (e.g., JDO).

时常饿 2024-08-01 05:26:08

您需要注释配置而不是 jpa 配置。

You need annotationconfiguration not jpaconfiguration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文