简体   繁体   English

Java hbm.xml一列的多个索引

[英]Java hbm.xml multiple index of one column

For example, 例如,

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

If I want the field_1 and field_2 has 2 indexes description. 如果我想要field_1field_2有2个索引说明。 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"/>

The field_1 and field_2 will has 2 index for their self. field_1field_2的自身索引为2。


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 . 用hbm.xml引用了hibernate 3.6,5.1.4.2属性映射 ,似乎index字段只能由only one column分配。


PS, PS,

The project is some kind old, and is maintained by many people, so I cannot use annotation syntax to add index . 该项目有些陈旧,并且由很多人维护,所以我不能使用注释语法来添加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>

This problem can be solved by <database-object> , by writing native sql syntax to create index. 此问题可以通过<database-object>解决,方法是编写本机sql语法以创建索引。


UPDATE, 2018/11 更新,2018/11

For unique constraint with multiple properties, someone has answered it . 对于具有多个属性的唯一约束, 有人已经回答了

Use properties tag 使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM