如何在多列上创建索引 [英] How to create indexes on multiple columns

查看:195
本文介绍了如何在多列上创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户属于特定组织的情况下,我们具有以下实体关系。我的查询看起来像是select * from User where org =:orgorselect * from User where org =:org and type =:type

We have the following entity relationships where a User belongs to a particular Organization. My queries either look like "select * from User where org=:org" or "select * from User where org=:org and type=:type"

单独的用户类索引。第一个查询将会很好,因为外键元素上的索引。第二个查询是否对org和type列指定了多列索引。如果是这样,我应该如何注释来创建一个这样的索引。

I have separate indexes on the User class. The first query will be fine, because of the Index on the foreign key element. Does the second query mandate a multi columnindex on org and type columns. If so how should I annotate to create one such index.

@Entity 
class User {
...

@ManyToOne 
@ForeignKey
@Index
Organization org;

@Index
Type type;    
...
}


推荐答案

这是可以使用特定于Hibernate的 @Table 注释来实现的。从文件中:

This is doable using the Hibernate specific @Table annotation. From the documentation:


2.4.1实体



...

2.4.1 Entity

...

@Table(applicableTo =tableName,indexes = {@Index(name =index1,columnNames = {column1,column2})))在表 tableName 的列上创建定义的索引。这可以应用在主表或任何辅助表上。 @Tables 注释允许您在不同的表上应用索引。预计此注释位于 @ javax.persistence.Table @ javax.persistence.SecondaryTable(s)发生的位置。 / b>

@Table(appliesTo="tableName", indexes = { @Index( name="index1", columnNames={"column1", "column2"} ) } ) creates the defined indexes on the columns of table tableName. This can be applied on the primary table or any secondary table. The @Tables annotation allows your to apply indexes on different tables. This annotation is expected where @javax.persistence.Table or @javax.persistence.SecondaryTable(s) occurs.



参考




  • Hibernate Annotations Reference Guide


    • 2.4。 Hibernate Annotation扩展

    • Reference

      • Hibernate Annotations Reference Guide
        • 2.4. Hibernate Annotation Extensions
        • 这篇关于如何在多列上创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆