-
06-07-2019 - |
题
我正在努力想要一个简单的 Envers 工作的例子。我被困在org.hibernate.tool.ant.EnversHibernateToolTask
<!>#8212;看起来我终于得到了我需要的所有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 <!>#8212;但是envers示例在ant任务中有<jpaconfiguration>
:
<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文件一起使用?似乎有关于如何使所有这些东西正常工作的ZERO文档。
编辑好的,所以主要问题是我不了解hibernatetool选项以及适合我的应用的内容。我确实找到了 Hibernate ant docs ,幸运的是。谢谢。现在我遇到了一个新问题:我正在使用注释,但我也为属性设置设置了hibernate.cfg.xml。 hibernatetool
任务只允许我运行<configuration />
或<annotationconfiguration />
而不是两者都运行,甚至<=>也不会工作,因为我已经有注释做事。如何将我的属性设置从hibernate.cfg.xml文件迁移到我的注释?
编辑:Duh,我没有意识到你只是这样做:
<annotationconfiguration configurationfile="...filename..." />
解决方案
将<jpaconfiguration />
替换为 <configuration />
标记,详见Hibernate Tools文档:
<configuration
configurationfile="hibernate.cfg.xml"
propertyfile="hibernate.properties"
entityresolver="EntityResolver classname"
namingstrategy="NamingStrategy classname">
其他提示
只是为了给你高级别的意见。
JPA是SUN提供的标准持久性API
您可以使用任何持久性框架(如Hibernate,TopLink,JDO等)作为JPA的持久性提供程序。
所以只是为了清楚
您的代码----- <!> gt; JPA ----- <!> gt;持久性提供程序(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>
您需要注释配置而不是jpaconfiguration。