如何使用hibernate映射文件在可空列上创建条件唯一约束?

时间:2017-10-28 06:59:27

标签: oracle hibernate conditional nullable unique-constraint

我在oracle数据库中有这些表:

create table APP_BASEINFORMATIONHEADER
(
  id               NUMBER(10) not null,
  topic            VARCHAR2(50 CHAR) not null,
  subsystem_type   NUMBER(10)
);

create table APP_BASEINFORMATION
(
  id                    NUMBER(10) not null,
  code                  NUMBER(10),
  parentid              NUMBER(10)
);

alter table APP_BASEINFORMATION
  add constraint FK_BASE_INFO_PARENT foreign key (PARENTID)
  references APP_BASEINFORMATIONHEADER (ID);

CREATE UNIQUE INDEX UNQ_BASE_INFO_PARENT_CODE
  ON APP_BASEINFORMATION 
 (CASE WHEN CODE IS NOT NULL  THEN PARENTID || '-' || CODE END);

如上所示,当UNQ_BASE_INFO_PARENT_CODE列不为空时,我在表APP_BASEINFORMATION上创建CODE唯一约束。我如何在.hbm.xml

上设置条件

这些是APP_BASEINFORNATION

的hi​​bernate映射文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class   name="org.my.framework.model.core.baseInfo.BaseInformation" table="App_BaseInformation">
        <id name="id" type="int" >
            <column name="ID"  />
            <generator class="sequence" >
                <param name="sequence">SEQ_BaseInformation</param>   
            </generator>
        </id> 

        <property column="Code"         name="code"             not-null="false"    type="integer" unique-key="??" />
        <property column="Topic"        name="topic"            not-null="true"     type="string"  length="100"/>

        <many-to-one entity-name="org.my.framework.model.core.baseInfo.BaseInformationHeader" name="parent" unique-key="??" >
            <column name="ParentId" not-null="false"  />
        </many-to-one>

    </class>
</hibernate-mapping>

我的意思是我应该将(CASE WHEN CODE IS NOT NULL THEN PARENTID || '-' || CODE END)放在hbm文件中以及正确的语法是什么?

0 个答案:

没有答案