使用 Hibernate @Index 注解在数据库上创建索引
我的项目具有注释驱动的休眠功能。
现在我想在列上创建索引。我当前的列定义是
@NotNull
@Column(name = "hash")
private String hash;
,我在此处添加 @Index
注释。
@NotNull
@Column(name = "hash")
@Index(name="hashIndex")
private String hash;
然后删除表并重新启动 Tomcat 服务器。服务器实例化后,表被创建,但我在以下查询中看不到新索引。
SHOW INDEX FROM tableName
预计将使用新索引构建表。我正在使用 InnoDB 和 MySQL。
I have annotation-driven hibernate capabilies on my project.
Now I want to create an index over a column. My current column definition is
@NotNull
@Column(name = "hash")
private String hash;
and I add @Index
annotation here.
@NotNull
@Column(name = "hash")
@Index(name="hashIndex")
private String hash;
and then DROP TABLE and restart Tomcat server. After the server is instantiated, the table is created but I can't see new index on following query.
SHOW INDEX FROM tableName
It is expected to construct table with new index. I am using InnoDB with MySQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(4)
在 Hibernate 中有意禁用架构更新时的索引创建,因为它似乎与架构导出中使用的命名不一致。
这是您可以在 org.hibernate.cfg.Configuration
类中找到的注释代码。
//broken, 'cos we don't generate these with names in SchemaExport
subIter = table.getIndexIterator();
while ( subIter.hasNext() ) {
Index index = (Index) subIter.next();
if ( !index.isForeignKey() || !dialect.hasImplicitIndexForForeignKey() ) {
if ( tableInfo==null || tableInfo.getIndexMetadata( index.getFilterName() ) == null ) {
script.add( index.sqlCreateString(dialect, mapping) );
}
}
}
//broken, 'cos we don't generate these with names in SchemaExport
subIter = table.getUniqueKeyIterator();
while ( subIter.hasNext() ) {
UniqueKey uk = (UniqueKey) subIter.next();
if ( tableInfo==null || tableInfo.getIndexMetadata( uk.getFilterName() ) == null ) {
script.add( uk.sqlCreateString(dialect, mapping) );
}
}
通常我会删除该注释,重新编译 Hibernate.jar 并在架构更新时创建索引,不会出现任何问题,至少对于 Oracle DB 来说是这样。
在 Hibernate 的最新版本中,官方版本中也删除了第一部分(表索引)的注释,但仍然注释了第二部分(实现唯一键的索引)。
请参阅http://opensource.atlassian.com/projects/hibernate/browse/HHH-1012<的讨论< /a>
更好的数据库设计意味着模式由与数据本身不同的用户拥有。因此,我设置了 hibernate.hbm2ddl.auto=none ,这样 Hibernate 启动时就不会出现故障。我改用 SchemaPrinter。其输出可以通过我最喜欢的 SQL 工具运行,以便在需要时重新创建模式。
import java.io.IOException;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class SchemaPrinter {
public static void main(String[] args) throws IOException {
Configuration cfg = new AnnotationConfiguration()
.addAnnotatedClass(MyClass1.class)
.addAnnotatedClass(MyClass2.class)
.setProperty(Environment.USER, "user")
.setProperty(Environment.PASS, "password")
.setProperty(Environment.URL, "jdbc:sybase:jndi:file://sql.ini?mydb")
.setProperty(Environment.DIALECT, "org.hibernate.dialect.SybaseASE15Dialect")
.setProperty(Environment.DRIVER, "com.sybase.jdbc4.jdbc.SybDriver")
.setProperty(Environment.HBM2DDL_AUTO, "none")
SchemaExport exp = new SchemaExport(cfg);
exp.setOutputFile("schema.ddl");
exp.create(true, false);
}
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有趣的是,在我的 Hibernate 配置中,我使用了 hibernate.hbm2ddl.auto=update 。
此操作修改现有数据库。我手动删除表
tableName
并重新启动 Tomcat,表已构建,但未创建索引。但是,我制作了
hibernate.hbm2ddl.auto=create
,它在每次 webapp 实例化时重新创建数据库,它删除了我所有的数据库并重新构建回来 - 是的 - 我的新索引已经创建了!Interestingly, in my Hibernate configuration I was using
hibernate.hbm2ddl.auto=update
.This one modifies an existing database. I was manually DROPping the table
tableName
and restarting Tomcat and the table had been constructed but index was not being created.However, I made
hibernate.hbm2ddl.auto=create
which re-creates database upon each instantiation of webapp, it dropped all my database and rebuilt back and -hell yeah- my new index has been created!