1

例如,

<?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_1andfield_2有 2 个索引描述。我可以做以下事情吗?或者如何实现?

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

和willfield_1field_22 个索引为他们自己。


我参考了hibernate 3.6, 5.1.4.2 与 hbm.xml 的属性映射,似乎该index字段可以由only one column.


PS,

该项目有点旧,并且由很多人维护,所以我不能使用注释语法来添加index.

4

1 回答 1

1

我找到了帖子并尝试了一下。

<?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

对于具有多个属性的唯一约束,有人已经回答了

使用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>
于 2015-10-15T09:31:05.023 回答