如何在hibernate实体中通过多列定义索引? [英] How to define index by several columns in hibernate entity?

查看:442
本文介绍了如何在hibernate实体中通过多列定义索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上午。

我需要在hibernate实体中添加索引。据我所知,可以使用@Index注释来为单独的列指定索引,但我需要一个实体的几个字段的索引。



我已经使用Google搜索并找到jboss注释@Table,允许这样做(按规范)。但是(我不知道为什么)这个功能不起作用。可能是jboss版本低于必要的,或者我不明白如何使用此注释,但是...复杂索引不会创建。



为什么index可能不会被创建?

JBoss 4.2.3.GA版本


实体示例:

  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(applicableTo = House.TABLE_NAME,
indexes = {
@Index(name =IDX_XDN_DFN,
columnNames = {House.XDN,House.DFN}

}


public class House {
public final static String TABLE_NAME =房子;
public final static String XDN =xdn;
public final static String DFN =dfn;

@Id
@GeneratedValue
私人长ID;

@Column(name = XDN)
私人长xdn;

@Column(name = DFN)
private long dfn;

@Column
私有字符串地址;

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;


当jboss / hibernate尝试创建表house它抛出以下异常:

 原因:org.hibernate.AnnotationException:@ org.hibernate.annotations.Table引用未知表: house 


解决方案

请尝试以下操作:

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

}

@Table(name =房屋)
公共类房屋{
...
}



<请注意,这也应该允许您创建一个多列索引(基于索引名称):

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

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

P.S .:您使用的是BTW的哪个版本的Hibernate?什么数据库/方言?

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

解决方案

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?

这篇关于如何在hibernate实体中通过多列定义索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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