JPA:定义索引列 [英] JPA: defining an index column

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

问题描述


可能重复:

使用JPA指定索引(非唯一键)

有没有办法在enitity列上定义索引,以提高搜索性能?
我看到hibernate给出了@Index和@IndexColumn,但我正在寻找JPA方式来做到这一点。

Is there a way to define index on enitity column, to improve searches performance? I saw that hibernate gives @Index and @IndexColumn, but I am looking for JPA way to do it.

谢谢

以下是我的实体示例,我需要索引名称列

Here is an example of my entity, I need to index a name column

@Entity
@Table(name = "MY_TABLE")
public class MyEntity {
    private long id;
    private String name;
    private String sourceInfo;

  ...


推荐答案

不,jpa没有提供任何定义或创建索引的功能。对于某些(我不知道)的原因,它只能在JPA中创建唯一索引。

No, jpa doesn't provide any feature to define or create indexes. For some (unknown to me) reason, it's only possible to create unique indexes in JPA.

如果你没有使用hibernate或者你真的不想使用那些注释,你需要建立你的注释和处理器来相应地输出或更新数据库,但我不认为它是非常微不足道的(它绝对是非标准的)

If you are not using hibernate or if you really don't want to use those annotations, you'll need to build your annotations and processor to output or update the database accordingly, but I don't think it's very trivial (and it's definitely, non standard)

以下是如何使用Hibernate定义索引的示例

Here's an example of how to define an index with Hibernate

@org.hibernate.annotations.Table(
   appliesTo = "table_name",
   indexes = {
      @Index(name="single_column_index", columnNames = "col"),
      @Index(name="multi_column_index", columnNames = {"col1", "col2"}),
   }
)

这篇关于JPA:定义索引列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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