如何使用JPA / hibernate创建索引,并使用mappedsupperclass中的字段和来自具体实体的字段

时间:2023-02-01 20:07:13

I have the @MappedSupperClass (simplified example):

我有@MappedSupperClass(简化示例):

@MappedSuperclass
public abstract class MySuperClass {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false)
    private Date creationDate;

    // ...
}

and a concrete Entity (simplified example):

和一个具体实体(简化示例):

@Entity
public class MyEntity extends MySuperClass {
    @Index(name = "IDX_MYINDEX")
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private MyType type;

    @Index(name = "IDX_MYINDEX")
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private MyResult status;

    // ...
}

Now I need an index including the columns MySuperClass.creationDate, MyEntity.status and MyEntity.type.

现在我需要一个索引,包括MySuperClass.creationDate,MyEntity.status和MyEntity.type列。

If I add @Index(name = "IDX_MYINDEX") to MySuperClass.creationDate hibernate adds an index of creationDate to every Entity inherited from MySuperClass.

如果我将@Index(name =“IDX_MYINDEX”)添加到MySuperClass.creationDate,hibernate会为从MySuperClass继承的每个Entity添加一个creationDate索引。

I tried @AttributeOverride but it is not capable for indexes.

我试过@AttributeOverride,但它不能用于索引。

Any ideas? TIA!

有任何想法吗? TIA!

2 个解决方案

#1


27  

If you are using JPA 2.1 then you can use class annotation @Table with its attribute indexes

如果您使用的是JPA 2.1,则可以使用类注释@Table及其属性索引

@Table(indexes = { @Index(name = "IDX_MYIDX1", columnList = "id,name,surname") })

Please note that as documentation says

请注意,正如文件所述

These are only used if table generation is in effect. Defaults to no additional indexes.

这些仅在表生成有效时使用。默认为无其他索引。

columnlist, as shown above, accepts column names list as a comma-delimited list.

如上所示,columnlist接受列名列表作为逗号分隔列表。

If you don't use JPA 2.1 you can just use old Hibernates @Index annotation (note this is already deprecated). There's attribute columnNames where you can pass array of column names no matter above which field it is declared.

如果你不使用JPA 2.1,你可以使用旧的Hibernates @Index注释(注意这已被弃用)。有一个属性columnNames,您可以在其中传递列名称数组,无论它在哪个字段上声明。

@Index(name = "IDX_MYIDX1", columnNames = { "id", "name", "surname"})

#2


2  

Use @Index annotation and use the parameter "columnList" to set which columns should be used to make your index. That list should be made of a comma-separated list of values of the column names.

使用@Index注释并使用参数“columnList”来设置应该使用哪些列来创建索引。该列表应由逗号分隔的列名值列表组成。

Important: Don't forget to add the column name property (via Column annotation) to all properties that make this index, otherwise you'll get an error when starting up your container.

重要提示:不要忘记将列名属性(通过列注释)添加到创建此索引的所有属性,否则在启动容器时会出错。

#1


27  

If you are using JPA 2.1 then you can use class annotation @Table with its attribute indexes

如果您使用的是JPA 2.1,则可以使用类注释@Table及其属性索引

@Table(indexes = { @Index(name = "IDX_MYIDX1", columnList = "id,name,surname") })

Please note that as documentation says

请注意,正如文件所述

These are only used if table generation is in effect. Defaults to no additional indexes.

这些仅在表生成有效时使用。默认为无其他索引。

columnlist, as shown above, accepts column names list as a comma-delimited list.

如上所示,columnlist接受列名列表作为逗号分隔列表。

If you don't use JPA 2.1 you can just use old Hibernates @Index annotation (note this is already deprecated). There's attribute columnNames where you can pass array of column names no matter above which field it is declared.

如果你不使用JPA 2.1,你可以使用旧的Hibernates @Index注释(注意这已被弃用)。有一个属性columnNames,您可以在其中传递列名称数组,无论它在哪个字段上声明。

@Index(name = "IDX_MYIDX1", columnNames = { "id", "name", "surname"})

#2


2  

Use @Index annotation and use the parameter "columnList" to set which columns should be used to make your index. That list should be made of a comma-separated list of values of the column names.

使用@Index注释并使用参数“columnList”来设置应该使用哪些列来创建索引。该列表应由逗号分隔的列名值列表组成。

Important: Don't forget to add the column name property (via Column annotation) to all properties that make this index, otherwise you'll get an error when starting up your container.

重要提示:不要忘记将列名属性(通过列注释)添加到创建此索引的所有属性,否则在启动容器时会出错。