How to define index by several columns in hibernate entity?

前端 未结 3 1260
梦谈多话
梦谈多话 2020-12-09 21:24

Morning.

I need to add indexing in hibernate entity. As I know it is possible to do using @Index annotation to specify index for separate column but I need an index

相关标签:
3条回答
  • 2020-12-09 22:21

    Please try the following:

    @Entity
    @org.hibernate.annotations.Table(appliesTo = House.TABLE_NAME,
        indexes = {
                @Index(name = "IDX_XDN_DFN",
                        columnNames = {House.XDN, House.DFN}
                )
        }
    )
    @Table(name="house")
    public class House {
        ...
    }
    

    Note that this should also allow you to create a multi-column index (based on the index name):

    @Index(name = "index1")
    public String getFoo();
    
    @Index(name = "index1")
    public String getBar();
    

    P.S.: What version of Hibernate are you using BTW? What database/dialect?

    0 讨论(0)
  • 2020-12-09 22:22

    You have to have hibernate.hbm2ddl.auto set to create in persistence.xml. When set to update hibernate won't create indexes.

    hibernate.hbm2ddl.auto = create

    0 讨论(0)
  • 2020-12-09 22:22

    You'd better go with a composite primary key.

    This article explains how to do it with JPA annotations. It uses @Embeddable and @EmbeddedId

    0 讨论(0)
提交回复
热议问题