我正在使用Hibernate与我的数据库通信。我有一个字段是一个枚举,它将在一个慢查询中使用。所以我想把它编入索引。我在字段中添加了以下注释:
@Column(name="RIGHT_TYPE", unique=false, nullable=false, length=10)
@Enumerated(EnumType.STRING)
@Index(name = "ABC_INDEX")
protected RightType rightType;但是,我没有看到在该字段上创建了任何索引。
我使用的是org.hibernate.dialect.Oracle9Dialect。(修剪后的) ddl是:
create table DOCUMENTS (documentID varchar2(255 char) not null, owner_principalId varchar2(255 char), primary key (documentID))
create table DOC_RIGHTS (document raw(255) not null, primary key (document))
create table PRINCIPAL (TYPE varchar2(31 char) not null, principalId varchar2(255 char) not null, displayName varchar2(255 char), primary key (principalId))
create table PRINCIPAL_RIGHTS (id varchar2(255 char) not null, PRINCIPAL_ID varchar2(255 char) unique, DOCUMENT_RIGHT_ID raw(255) unique, primary key (id))
create table RIGHTS (TYPE varchar2(31 char) not null, id number(19,0) not null, RIGHT_TYPE varchar2(10 char) not null, PRINCIPAL_RIGHT_ID varchar2(255 char) unique, primary key (id))
create table ROLE_MAP (PRINCIPAL_ID varchar2(255 char) not null, ROLE_ID varchar2(255 char) not null)
alter table DOCUMENTS add constraint FKDC2BB35E362547 foreign key (owner_principalId) references PRINCIPAL
create index PRINCIPAL_INDEX on PRINCIPAL_RIGHTS (PRINCIPAL_ID)
alter table PRINCIPAL_RIGHTS add constraint FKB32239ADFB30571B foreign key (PRINCIPAL_ID) references PRINCIPAL
alter table PRINCIPAL_RIGHTS add constraint FKB32239ADE1F0C813 foreign key (DOCUMENT_RIGHT_ID) references DOC_RIGHTS
create index RIGHT_TYPE_INDEX on RIGHTS (RIGHT_TYPE)
alter table RIGHTS add constraint FKF34FBA9CA09D6215 foreign key (PRINCIPAL_RIGHT_ID) references PRINCIPAL_RIGHTS
alter table ROLE_MAP add constraint FKA413CD78FB30571B foreign key (PRINCIPAL_ID) references PRINCIPAL
alter table ROLE_MAP add constraint FKA413CD7883A04939 foreign key (ROLE_ID) references PRINCIPAL
create sequence RIGHTS_SEQUENCE但如果我愿意,我可以手动创建一个。为什么会这样呢?有没有办法迫使Hibernate实现这一点?
发布于 2010-03-30 05:30:10
这似乎就像你在你的问题中得到的一样。我已经创建了一个小test app来演示它在独立应用程序中的工作。
可能发生的一件事是hibernate在更新操作期间不会尝试创建索引。它将从scratch hbm2ddl=create创建它们,但不是在更新期间。hibernate团队对此被报告为错误的标准响应是'hbm2ddl仅用于开发,因此不需要索引,您应该获得数据库管理员。
我个人的建议是,如果这是问题所在,那就开始使用liquibase来管理创建索引之类的事情。
发布于 2010-03-30 04:57:41
我用以下实体做了一个测试:
@Entity
public class EntityWithIndices {
@Id
@GeneratedValue
private Long id;
@Column(name = "GENDER_TYPE", unique = false, nullable = false, length = 10)
@Enumerated(EnumType.STRING)
@Index(name = "ABC_INDEX")
protected GenderType genderType;
// getter and setters
} 使用以下枚举:
public enum GenderType {
FEMALE("0"), MALE("1");
private final String genderCode;
public String getCode() {
return this.genderCode;
}
private GenderType(String code) {
this.genderCode = code;
}
}使用Hibernate注解3.4.0.GA,MySQL 5.1作为数据库,生成索引。不过,我不能尝试使用Oracle。我做了一个search for existing issues (项目z- Hibernate注解,任何问题类型,在“索引”上),找不到任何最近版本的Hibernate注解的问题(这并不意味着没有问题)。如果你创建了一个问题,试着提供一个简单的可运行的测试用例和SchemaExport日志,这将大大增加你修复它的机会。
https://stackoverflow.com/questions/2537378
复制相似问题