Creating Indexes on DB with Hibernate @Index Annotation

后端 未结 4 1988
无人共我
无人共我 2020-12-30 01:48

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
@C         


        
4条回答
  •  抹茶落季
    2020-12-30 02:24

    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

提交回复
热议问题