如何使用JPA / hibernate创建索引并将mappedsupperclass中的字段与来自具体实体的字段一起使用 [英] How to create an index with JPA/hibernate and use fields from mappedsupperclass together with fields from concrete entity

查看:358
本文介绍了如何使用JPA / hibernate创建索引并将mappedsupperclass中的字段与来自具体实体的字段一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 @MappedSupperClass (简单示例):

  @MappedSuperclass 
public abstract class MySuperClass {

@Id
@GeneratedValue
private long id;

@Column(nullable = false)
private创建日期;

// ...
}

实体(简单示例):

  @Entity 
public类MyEntity扩展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;

// ...
}

现在我需要包含列 MySuperClass.creationDate MyEntity.status MyEntity.type


$ b

如果我将 @Index(name =IDX_MYINDEX)添加到 MySuperClass.creationDate hibernate为每个从 MySuperClass 继承的实体添加 creationDate 索引。



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



任何想法? TIA!

解决方案

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

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

请注意,如文档所述


只有在生成表格时才会使用它们。默认为没有
的附加索引。


columnlist ,如图所示如果你不使用JPA 2.1,你可以使用旧的 Hibernate

code> s @Index 注释(注意这已被弃用)。有一些属性 columnNames ,您可以在其中传递列名称数组,无论它在哪个字段声明。

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


I have the @MappedSupperClass (simplified example):

@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.

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

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

Any ideas? TIA!

解决方案

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

@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.

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.

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

这篇关于如何使用JPA / hibernate创建索引并将mappedsupperclass中的字段与来自具体实体的字段一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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