早上.
我需要在hibernate实体中添加索引.据我所知,可以使用@Index注释来指定单独列的索引,但我需要一个实体的几个字段的索引.
我用谷歌搜索并找到了jboss注释@Table,它允许这样做(按照规范).但是(我不知道为什么)这个功能不起作用.可能是jboss版本低于必要,或者我可能不明白如何使用这个注释,但是…复杂索引没有创建.
为什么不能创建索引?
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(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; } }
当jboss / hibernate尝试创建表“house”时,它抛出以下异常:
Reason: org.hibernate.AnnotationException: @org.hibernate.annotations.Table references an unknown table: house
解决方法
请尝试以下方法:
@Entity @org.hibernate.annotations.Table(appliesTo = House.TABLE_NAME,House.DFN} ) } ) @Table(name="house") public class House { ... }
请注意,这还应该允许您创建多列索引(基于索引名称):
@Index(name = "index1") public String getFoo(); @Index(name = "index1") public String getBar();
P.S.:您使用什么版本的Hibernate BTW?什么数据库/方言?