Java hbm.xml一列的多个索引

问题描述:

例如,

<?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_1 field_2 有2个索引说明.我可以做以下事情吗?或如何实现?

If I want the field_1 and field_2 has 2 indexes description. Can I do the following thing? Or How to achieve it ?

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

field_1 field_2 各自具有2个索引.

The field_1 and field_2 will has 2 index for their self.

我引用了 hibernate 3.6,5.1.4.2使用hbm.xml进行属性映射,似乎 index 字段只能由一列分配.

I refer the hibernate 3.6, 5.1.4.2 Property mapping with hbm.xml, it seems like the index field can be assigned by only one column.

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

The project is some kind old, and is maintained by many people, so I cannot use annotation syntax to add index.

我找到了

I found the post and give it a try.

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


通过编写本机sql语法创建索引,可以通过<数据库对象> 解决此问题.

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

For unique constraint with multiple properties, 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>