Java 如何通过休眠实体中的几列定义索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2182950/
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
How to define index by several columns in hibernate entity?
提问by Zaur_M
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.
我需要在休眠实体中添加索引。据我所知,可以使用 @Index 注释为单独的列指定索引,但我需要为实体的多个字段设置索引。
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.
我用谷歌搜索并找到了 jboss 注释@Table,它允许这样做(按规范)。但是(我不知道为什么)这个功能不起作用。可能是 jboss 版本低于必要,或者我可能不了解如何使用此注释,但是...没有创建复杂的索引。
Why index may not be created?
为什么不能创建索引?
jboss version 4.2.3.GA
jboss 版本 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:
当 jboss/hibernate 尝试创建表“house”时,它会抛出以下异常:
Reason: org.hibernate.AnnotationException: @org.hibernate.annotations.Table references an unknown table: house
回答by Bozho
You'd better go with a composite primary key.
你最好使用复合主键。
This articleexplains how to do it with JPA annotations. It uses @Embeddable
and @EmbeddedId
这篇文章解释了如何使用 JPA 注释来做到这一点。它使用@Embeddable
和@EmbeddedId
回答by Pascal Thivent
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?
PS: BTW 你用的是什么版本的 Hibernate?什么数据库/方言?
回答by fraktalek
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 设置为在 persistence.xml 中创建。当设置为更新休眠时不会创建索引。
hibernate.hbm2ddl.auto = create
hibernate.hbm2ddl.auto = create