什么是JPA配置?

Jas*_*n S 0 java ant hibernate jpa hibernate-envers

很难尝试让一个简单的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'.
Run Code Online (Sandbox Code Playgroud)

据我所知,持久性单元与JPA persistence.xml文件相关联.但我没有使用persistence.xml文件; 我正在使用hibernate.cfg.xml - 但是envers示例<jpaconfiguration>在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>
Run Code Online (Sandbox Code Playgroud)

有什么东西我可以替换它以使其与hibernate.cfg.xml文件一起使用?似乎有关于如何使所有这些东西正常工作的ZERO文档.

编辑:好的,所以主要的问题是我不明白hibernatetool选项以及什么适合我的应用程序.幸运的是,我确实找到了Hibernate ant docs.谢谢.现在我遇到了一个新问题:我正在使用注释,但我也为属性设置设置了hibernate.cfg.xml.该hibernatetool任务只让我跑任一<configuration /><annotationconfiguration />不同时,甚至<configuration />是行不通的,因为我已经有注释做的事情.如何将我的属性设置从hibernate.cfg.xml文件迁移到我的注释?

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

<annotationconfiguration configurationfile="...filename..." />
Run Code Online (Sandbox Code Playgroud)

根据hibernatetool任务文档.

and*_*dri 8

更换<jpaconfiguration />用的<configuration />标签,在休眠的工具文档详细介绍:

<configuration
    configurationfile="hibernate.cfg.xml"
    propertyfile="hibernate.properties"
    entityresolver="EntityResolver classname"
    namingstrategy="NamingStrategy classname">
Run Code Online (Sandbox Code Playgroud)


Kha*_*oth 5

只是为了给你高级别的准备.JPA是SUN提供的标准持久性API.
您可以使用任何持久性框架(如Hibernate,TopLink,JDO等)作为JPA的持久性提供程序.

所以只是为了清楚

你的代码-----> JPA -----> Persistence Provider(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>
Run Code Online (Sandbox Code Playgroud)

你的应用程序上下文看起来像(依赖于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>
Run Code Online (Sandbox Code Playgroud)

  • >您可以使用任何持久性框架(如Hibernate,TopLink,JDO等)作为JPA的持久性提供程序.不,你不能.您可以使用任何JPA*实现*.JDO是持久性的规范,就像JPA是持久性规范一样. (2认同)