Java – how to define an index in a hibernate entity through several columns?

morning.

I need to add an index to the hibernate entity As far as I know, the @ index annotation can be used to specify the index of individual columns, but I need the index of several fields of an entity

I googled and found the JBoss annotation @ table, which allows this (according to the specification) But (I don't know why) this function doesn't work Maybe the JBoss version is lower than necessary, or I may not understand how to use this annotation, but... The complex index is not created

Why can't I create an index?

JBoss version 4.2 3.GA

Examples of entities:

package somepackage;
import org.hibernate.annotations.Index;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

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

public class House {
    public final static String TABLE_NAME = "house";
    public final static String XDN = "xdn";
    public final static String DFN = "dfn";

    @Id
    @GeneratedValue
    private long Id;

    @Column(name = XDN)
    private long xdn;

    @Column(name = DFN)
    private long dfn;

    @Column
    private String address;

    public long getId() {
        return Id;
    }

    public void setId(long id) {
        this.Id = id;
    }

    public long getXdn() {
        return xdn;
    }

    public void setXdn(long xdn) {
        this.xdn = xdn;
    }

    public long getDfn() {
        return dfn;
    }

    public void setDfn(long dfn) {
        this.dfn = dfn;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

When JBoss / Hibernate tries to create the table "house", it throws the following exception:

Reason: org.hibernate.AnnotationException: @org.hibernate.annotations.Table references an unkNown table: house

Solution

Try the following:

@Entity
@org.hibernate.annotations.Table(appliesTo = House.TABLE_NAME,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 btw do you use? What database / dialect?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>