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

发布于 2024-09-15 19:45:08 字数 428 浏览 8 评论 0 原文

我的项目具有注释驱动的休眠功能。

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

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

德意的啸 2024-09-22 19:45:08

有趣的是,在我的 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!

夜吻♂芭芘 2024-09-22 19:45:08

在 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>

Index creation on schema update was intentionally disabled in Hibernate because it seemed inconsistent with the naming used in the schema export.

This is the commented code that you can find in class 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) );
    }
}

Usually I remove that comment, recompile Hibernate.jar and have indexes created on schema update without any problem, at least with Oracle DB.

In recent versions of Hibernate the comment on the first part (table indexes) has been removed in the official version as well, while it's still commented the second one (indexes that implement unique keys).
See the discussion at http://opensource.atlassian.com/projects/hibernate/browse/HHH-1012

许仙没带伞 2024-09-22 19:45:08

更好的数据库设计意味着模式由与数据本身不同的用户拥有。因此,我设置了 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);
    }

}

Better DB design means the schema is owned by a different user than the data itself. Hence I set hibernate.hbm2ddl.auto=none so there are no failures upon Hibernate start. I use a SchemaPrinter instead. The output of which can be run via my favorite SQL tool to recreate the schema when required.

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);
    }

}
花之痕靓丽 2024-09-22 19:45:08

在 Hibernate 3.5.6 中使用 update>索引已创建。所以现在正确的答案是升级。
但我把这个答案留给像我这样遇到过这个问题的人。

In Hibernate 3.5.6 using <property name="hibernate.hbm2ddl.auto">update</property> the indexes are created. So a proper answer now would be to upgrade.
But I'm leaving this answer for those like me that have come across this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文