Java 如何使用 JPA/hibernate 创建索引并将 MappedSuperClass 中的字段与具体实体中的字段一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24943436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:52:46  来源:igfitidea点击:

How to create an index with JPA/hibernate and use fields from MappedSuperClass together with fields from concrete entity

javahibernatejpa

提问by t777

I have the @MappedSuperClass(simplified example):

我有@MappedSuperClass(简化的例子):

@MappedSuperclass
public abstract class MySuperClass {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false)
    private Date creationDate;

    // ...
}

and a concrete Entity(simplified example):

和一个具体的Entity(简化的例子):

@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.statusand MyEntity.type.

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

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

如果我添加@Index(name = "IDX_MYINDEX")MySuperClass.creationDatehibernate creationDate,则为从MySuperClass.

I tried @AttributeOverridebut it is not capable for indexes.

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

Any ideas?

有任何想法吗?

采纳答案by Maciej Dobrowolski

If you are using JPA 2.1then you can use class annotation @Tablewith 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 @Indexannotation (note this is already deprecated). There's attribute columnNameswhere 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"})

回答by dontForgetTheJoker

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.

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

回答by babin souare

@Table(name = "table_name",indexes = [Index(name = "name_of_index", columnList = "liste of fields separated by ',' ")])

回答by Badamchi

Make sure list column names that you've given in @Column's name attribute not fields' names.

确保列出您在@Column 的名称属性中给出的列名称而不是字段名称。

Also enabling ddl-auto to update will create all the indexes.

同时启用 ddl-auto 更新将创建所有索引。

spring.jpa.hibernate.ddl-auto=update

spring.jpa.hibernate.ddl-auto=update

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "ANSWER_OPTION", indexes = {@Index(name="SEC_INDEX",columnList = "ID,SEQUENCE,QUESTION_ID,OPTION_TITLE")})
public class AnswerOptionEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "SEQUENCE")
private Long sequence;

@Column(name = "OPTION_TITLE")
private String optionTitle;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "QUESTION_ID", nullable = false)
@JsonIgnore
private QuestionEntity questionEntity;

@OneToMany(mappedBy = "answerOptionEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnore
private List<ResultEntity> resultEntityList = new ArrayList<>();

}

}