2014-11-17 32 views
1

我有一個hibernate xml映射問題。我想在現有的數據庫模式中添加一個新字段「newField」。Hibernate XML映射 - 創建新字段並將其添加到多列約束中

我想插入這個字段在另一個(在這個例子中的大小),併成爲一個多列唯一鍵約束的一部分。

XML映射如下:

<hibernate-mapping default-lazy="false" default-cascade="all" package="xxx"> 
<class name="A" table="As" discriminator-value="a"> 
    <id name="dbId" column="db_id" type="long"> 
     <generator class="IdentityGenerator"/> 
    </id> 
    <discriminator column="tag" type="string"/>   
    <property name="created" type="timestamp" insert="false" update="false" generated="insert"/> 

    <properties name="key" unique="true">    
     <property name="name" column="name" type="string" not-null="true"/>    
     <property name="version" column="version" length="80" type="string"/> 
    </properties> 

<property name="size" column="size" type="long"/> 
<property name="newField" column="new_field" type="long"/> 

</class> 

如果我加入我的新領域的財產到多列「屬性」,它會被創建兩次。

你知道hibernate是否有辦法做到這一點?

感謝您的幫助

里斯

回答

1

謝謝您的回答@Sebri。

我找到了解決辦法,我只是通過獨特的,關鍵屬性代替指數

<hibernate-mapping default-lazy="false" default-cascade="all" package="xxx"> 

<id name="dbId" column="db_id" type="long"> 
    <generator class="IdentityGenerator"/> 
</id> 
<discriminator column="tag" type="string"/>   
<property name="created" type="timestamp" insert="false" update="false" generated="insert"/>   
<property name="name" column="name" type="string" not-null="true" unique-key="key"/>    
<property name="version" column="version" length="80" type="string" unique-key="key"/> 
<property name="size" column="size" type="long"/> 
<property name="newField" column="new_field" type="long" unique-key="key"/> 

0

首先,你可以使用唯一索引

然後

<property name="name" column="name" type="string" not-null="true" index="UNIQUE_IDX_KEY"/>    
<property name="version" column="version" length="80" type="string" index="UNIQUE_IDX_KEY"/> 


<property name="newField" column="new_field" index="UNIQUE_IDX_KEY" /> 
相關問題