【问题标题】:Hibernate XML mapping - create new field after another AND add it into a multi-column constraintHibernate XML 映射 - 一个接一个地创建新字段并将其添加到多列约束中
【发布时间】:2015-01-14 05:51:32
【问题描述】:

我遇到了休眠 xml 映射的问题。我想在现有的数据库架构中添加一个新字段“newField”。

我想在另一个之后插入此字段(示例中为大小),并且成为多列唯一键约束的一部分。

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有没有办法做到这一点?

感谢您的帮助

乔里斯

【问题讨论】:

    标签: java xml hibernate mapping key


    【解决方案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"/>
    

    【讨论】:

      【解决方案2】:

      首先你可以使用唯一索引

      CREATE UNIQUE INDEX `index_nom` ON `As` (`name`,`version`,`new_field`);
      

      然后

      <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" />
      

      【讨论】:

        猜你喜欢
        • 2012-01-10
        • 2014-12-12
        • 2020-12-20
        • 2018-11-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多