使用 Hibernate @Index Annotation 在 DB 上创建索引 [英] Creating Indexes on DB with Hibernate @Index Annotation
问题描述
我的项目有注释驱动的休眠功能.
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;
我在这里添加了 @Index
注释.
and I add @Index
annotation here.
@NotNull
@Column(name = "hash")
@Index(name="hashIndex")
private String hash;
然后删除表并重新启动Tomcat服务器.实例化服务器后,创建了表,但在以下查询中看不到新索引.
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
期望用新索引构造表.我在 MySQL 中使用 InnoDB.
It is expected to construct table with new index. I am using InnoDB with MySQL.
推荐答案
有趣的是,在我的 Hibernate 配置中,我使用了 hibernate.hbm2ddl.auto=update
.
Interestingly, in my Hibernate configuration I was using hibernate.hbm2ddl.auto=update
.
这个修改了一个现有的数据库.我手动删除表 tableName
并重新启动 Tomcat,并且该表已构建但未创建索引.
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.
然而,我制作了 hibernate.hbm2ddl.auto=create
,它在每次 webapp 实例化时重新创建数据库,它删除了我所有的数据库并重建了 - 地狱是的 - 我的新索引已经已创建!
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!
这篇关于使用 Hibernate @Index Annotation 在 DB 上创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!