3

Hibernateを使用していて、Enversの監査/改訂機能を追加してみたいのですが、何が必要かわからないようです。(私の例はコンパイルして正常に実行され、通常のHibernate機能を取得しますが、データベースに監査テーブルが表示されません。)以前に誰かがこれを行ったことはありますか?HSQLDBダイアレクトを使用するH2データベースで動作しますか?Web上にシンプルで完全なサンプルプログラムはありますか?

編集:少し言い換えさせてください。最後に、ビルドプロセスで.jarファイルを作成し、別のコンピューターにインストールして、適切な.propertiesファイルとJDBCドライバーを使用して、適切なデータベースを作成(または作成できるようにする)したいと思います。テーブルがまだ存在しない場合。これどうやってするの?

編集:これまでのところ、Jamie Bが提案したantタスクを実行する場合は、クラスパスを微調整して、Hibernateツールのzip内に埋め込まれているenversjarファイルとhibernate- toolsjarファイルを見つける必要があります。そして、私はまだ物事が機能していない。もしそうなら、多分それならSQLファイルを作成し、それを最終的な.jarファイル内のリソースとして置くことができ、それをプログラム自体の中から使用できると思います。(セキュリティの問題を考えていると頭の中で赤い旗が消えますが...うーん....)

4

3 に答える 3

2

次の休止状態のプロパティを探しているようです:

hibernate.hbm2ddl.auto

ドキュメントから:

SessionFactory の作成時にスキーマ DDL を自動的に検証またはデータベースにエクスポートします。

これにより、設定したenvers プロパティに従って名前が付けられたスキーマ テーブルが自動的に作成されます。追加のライブラリや Ant タスクは必要ありません。

たとえば、これを開発データベース用のhibernate.cfg.xmlセットに追加します。Hibernate のオブジェクトupdateを使用して、このプロパティをプログラムで追加することもできます。configuration

于 2009-06-05T14:37:53.423 に答える
1

リファレンスドキュメント(www.jboss.org/file-access/default/members/envers/downloads/envers-1.2.0.ga-hibernate-3.3.pdf)の第6章を読みましたか?_AUDテーブルは標準のHibernateの方法では作成されないようです。それを補強するAntTaskがあります。

于 2009-05-22T20:27:00.270 に答える
1

いくつかの実験の後、私はenversで作業テーブルを生成しました

これらのパラメーター hibernate.hbm2ddl.auto=create-drop を使用しました

テーブルが存在するためエラーが表示されるので、パラメーター hibernate.hbm2ddl.auto=update で解決すると思います

ログ:

    5755 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: myappcompany.base.Company_AUD -> company_AUD
5782 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: org.hibernate.envers.DefaultRevisionEntity -> REVINFO
5915 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
6746 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
6784 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - Running hbm2ddl schema export
6785 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - exporting generated schema to database
9004 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: create table company (ID bigint not null auto_increment, CREATED datetime, CREATED_BY varchar(255), MODIFIED datetime, MODIFIED_BY varchar(255), NAME varchar(255) not null, primary key (ID))
9004 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Table 'company' already exists
10227 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete

私の設定:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <!--
    Data Source config 
     -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}"
        p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entity-manager-factory-ref="entityManagerFactory" />

    <!-- 
    JPA config   
    -->
    <tx:annotation-driven />

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:persistence-xml-location="${persistence.xml.location}"
        p:data-source-ref="dataSource">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                p:showSql="true" p:generateDdl="true">
            </bean>
        </property>
        <property name="jpaProperties">
            <value>
                hibernate.ejb.naming_strategy=org.hibernate.cfg.DefaultNamingStrategy
                hibernate.dialect=${hibernate.dialect}
                hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
            </value>
        </property>
    </bean>

    <bean
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>

数日後、完全な例を作成します

于 2009-11-18T09:56:41.600 に答える