配置hibernate.reveng.xml以使用hibernate3-maven-plugin和Postgre检测序列PK生成器

时间:2010-06-06 17:20:41

标签: hibernate maven-2 maven maven-plugin

有没有办法配置hibernate3-maven-plugin,以便检测到primary-key的序列生成器?我正在使用自下而上的hibernate配置方法(这意味着让hibernate-tools在现有数据库模式上通过jdbc-connection使用reverse-engineering生成hibernate配置。我已经阅读了this,但也已经this了(这两个可以不相关,但也可以提示)。我的hibernate.reveng.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering 
 SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
    <table name="ORDERS">
        <primary-key>
            <!-- setting up a specific id generator for a table -->
            <generator class="sequence">
                <param name="sequence">ORDERS_ORDER_ID_seq</param>
            </generator>
            <key-column name="ORDER_ID"/>
        </primary-key>
    </table>
</hibernate-reverse-engineering>

我希望它能生成这样的Orders.hbm.xml文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2010-06-06 18:55:42 by Hibernate Tools 3.2.2.GA -->
<hibernate-mapping>
    <class name="some.package.Orders" table="orders" schema="public">
        <id name="orderId" type="long">
            <column name="order_id" />
            <generator class="sequence">
                <param name="sequence">ORDERS_ORDER_ID_seq</param>
            </generator>
        </id>
    ...
    </class>
</hibernate-mapping>

...但是接受了这个:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2010-06-06 18:55:42 by Hibernate Tools 3.2.2.GA -->
<hibernate-mapping>
    <class name="some.package.Orders" table="orders" schema="public">
        <id name="orderId" type="long">
            <column name="order_id" />
            <generator class="assigned" />
        </id>
    ...
    </class>
</hibernate-mapping>

我知道hibernate.reveng.xml正在阅读hibernate3-maven-plugin,因为每当文件中出现语法错误时我都会遇到maven错误,因此pom.xml似乎是正确的hibernate.reveng.xml语法正确。

任何线索?

2 个答案:

答案 0 :(得分:4)

最终代码对我有用。我只需要以小写形式传递order表名(我在我的DDL中使用大写字母,所以我实际上不明白,但这有效)。此外,还需要提供schema属性。 <key-column name="pkey"/>是可选的(如果您遵循休眠命名约定)。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering 
 SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
    <table name="orders" schema="public">
        <primary-key>
            <!-- setting up a specific id generator for a table -->
            <generator class="sequence">
                <param name="sequence">ORDERS_ORDER_ID_seq</param>
            </generator>
        </primary-key>
    </table>
</hibernate-reverse-engineering>

答案 1 :(得分:3)

您可以尝试使用 <param name="table">...</param> (这是我在documentationthis thread中看到的内容)。所以像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering 
 SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
    <table name="ORDERS">
        <primary-key>
            <!-- setting up a specific id generator for a table -->
            <generator class="sequence">
                <param name="table">ORDERS_ORDER_ID_seq</param>
            </generator>
            <key-column name="ORDER_ID"/>
        </primary-key>
    </table>
</hibernate-reverse-engineering>