Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define index by several columns in hibernate entity?

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 for several fields of entity.

I've googled and found jboss annotation @Table, that allows to do this (by specification). But (I don't know why) this functionality doesn't work. May be jboss version is lower than necessary, or maybe I don't understant how to use this annotation, but... complex index is not created.

Why index may not be created?

jboss version 4.2.3.GA

Entity example:

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 table "house" it throws following exception:

Reason: org.hibernate.AnnotationException: @org.hibernate.annotations.Table references an unknown table: house
like image 541
Zaur_M Avatar asked Feb 02 '10 09:02

Zaur_M


People also ask

Can an index be created on multiple columns?

The two types of indexes are single-column indexes and multicolumn indexes. A single-column index is an index based on the values in one column of a table. A multicolumn index is an index based on the values in multiple columns of a table.

How does index work on multiple columns?

Multicolumn indexes (also known as composite indexes) are similar to standard indexes. They both store a sorted “table” of pointers to the main table. Multicolumn indexes however can store additional sorted pointers to other columns.

Can a table have multiple indexes for multiple columns?

It is possible for an index to have two or more columns. Multi column indexes are also known as compound or concatenated indexes.

How many columns can be in an index?

Adding an index to a list or library column increases performance when you use filters. You can add indexes on up to 20 columns on a list or library.


2 Answers

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?

like image 161
Pascal Thivent Avatar answered Oct 20 '22 17:10

Pascal Thivent


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

like image 27
fraktalek Avatar answered Oct 20 '22 17:10

fraktalek