首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Hibernate @Index注释在数据库上创建索引

使用Hibernate @Index注释在数据库上创建索引
EN

Stack Overflow用户
提问于 2010-08-21 01:23:22
回答 4查看 31K关注 0票数 19

我的项目中有注释驱动的hibernate功能。

现在我想在列上创建一个索引。我当前的列定义是

代码语言:javascript
复制
@NotNull
@Column(name = "hash")
private String hash;

我在这里添加了@Index注释。

代码语言:javascript
复制
@NotNull
@Column(name = "hash")
@Index(name="hashIndex")
private String hash;

然后删除TABLE并重新启动Tomcat服务器。在服务器实例化之后,表被创建了,但是我在下面的查询中看不到新的索引。

代码语言:javascript
复制
SHOW INDEX FROM tableName

它期望使用新的索引来构建表。我在MySQL中使用InnoDB。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-08-21 02:31:56

有趣的是,在Hibernate配置中,我使用的是hibernate.hbm2ddl.auto=update

它修改了一个已有的数据库。我手动对表tableName执行DROPping操作,然后重新启动Tomcat,表已经构建好了,但是没有创建索引。

然而,我做了hibernate.hbm2ddl.auto=create,它在每次webapp的实例化时重新创建数据库,它丢弃了我所有的数据库并重建了回来,-hell是的-我的新索引已经创建了!

票数 18
EN

Stack Overflow用户

发布于 2011-03-02 01:57:03

在Hibernate中故意禁止在模式更新时创建索引,因为它似乎与模式导出中使用的命名不一致。

这是您可以在org.hibernate.cfg.Configuration类中找到的注释代码。

代码语言:javascript
复制
//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上的讨论

票数 9
EN

Stack Overflow用户

发布于 2013-12-12 18:58:15

更好的数据库设计意味着模式由不同于数据本身的用户拥有。因此,我设置了hibernate.hbm2ddl.auto=none,这样在Hibernate启动时就不会出现故障。我使用的是SchemaPrinter。它的输出可以通过我最喜欢的SQL工具在需要时重新创建模式。

代码语言:javascript
复制
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);
    }

}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3533301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档