我们有以下实体关系,其中用户属于特定组织。我的查询看起来像“select * from User where org=:org”或“select * from User where org=:org and type=:type”
我在 User 类上有单独的索引。第一个查询会很好,因为外键元素上有索引。第二个查询是否要求在 org 和 type 列上使用多列索引。如果是这样,我应该如何注释以创建一个这样的索引。
@Entity
class User {
...
@ManyToOne
@ForeignKey
@Index
Organization org;
@Index
Type type;
...
}
最佳答案
这可以使用 Hibernate 特定的 @Table
注释来实现。来自文档:
2.4.1 Entity
...
@Table(appliesTo="tableName", indexes = { @Index( name="index1", columnNames={"column1", "column2"} ) } )
creates the defined indexes on the columns of tabletableName
. 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 注释引用指南
关于mysql - 如何在多列上创建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3725545/