Java hbm.xml一列的多个索引

时间:2015-10-15 09:13:11

标签: java hibernate hbmxml

例如,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.sql.index">
    <class name="User">
        <id name="id" type="long">
            <generator class="native" />
        </id>
        <property name="firstName" type="string" index="IDX_FIRST_NAME" />
        <property name="lastName" type="string" />
        <property name="address" type="string" />
        <property name="field_1" type="long" />
        <property name="field_2" type="long" />
    </class>
</hibernate-mapping>

如果我希望field_1field_2有2个索引描述。我可以做以下事情吗?或者如何实现它?

        <property name="field_1" type="long" index="idx_1,idx_2"/>
        <property name="field_2" type="long" index="idx_1,idx_3"/>

field_1field_2将为自己设置2个索引。

我引用了hibernate 3.6, 5.1.4.2 Property mapping with hbm.xml,好像index字段可以由only one column分配。

PS,

该项目有些陈旧,并且由许多人维护,因此我无法使用注释语法来添加index

1 个答案:

答案 0 :(得分:1)

我找到post并尝试一下。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.sql.index">
    <class name="User">
        <id name="id" type="long">
            <generator class="native" />
        </id>
        <property name="firstName" type="string" index="IDX_FIRST_NAME" />
        <property name="lastName" type="string" />
        <property name="address" type="string" />
        <property name="field_1" type="long" index="idx_2"/>
        <property name="field_2" type="long" index="idx_3"/>
    </class>
    <database-object>
        <create>
            CREATE INDEX idx_1 ON User (field_1, field_2)
        </create>
        <drop></drop>
    </database-object>
</hibernate-mapping>

<database-object>可以通过编写本机sql语法来创建索引来解决这个问题。

更新,2018/11

对于具有多个属性的唯一约束someone has answered it

  

使用properties代码

<properties name="uk1" unique="true">
        <property name="username" .../>
        <many-to-one name="client" .../>
</properties>

<properties name="uk2" unique="true">
        <property name="email" .../>
        <many-to-one name="client" update="false" insert="false" .../>
</properties>