From 1d9b7a06a52b56b84844513d97c535eb00d0ed8a Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Wed, 6 Feb 2013 00:09:08 +0800 Subject: [PATCH] HHH-7969 initial @Table#indexes support --- .../org/hibernate/cfg/AnnotationBinder.java | 5 +- .../cfg/IndexOrUniqueKeySecondPass.java | 60 +++++++++++++-- .../cfg/annotations/EntityBinder.java | 11 ++- .../cfg/annotations/TableBinder.java | 15 ++++ .../JPAOverriddenAnnotationReader.java | 1 + .../dialect/unique/DB2UniqueDelegate.java | 11 ++- .../dialect/unique/DefaultUniqueDelegate.java | 7 +- .../org/hibernate/mapping/Constraint.java | 14 ++-- .../java/org/hibernate/mapping/Index.java | 41 ++++++++-- .../java/org/hibernate/mapping/Table.java | 15 ++-- .../java/org/hibernate/mapping/UniqueKey.java | 15 ++++ .../test/annotations/index/jpa/Car.java | 45 +++++++++++ .../test/annotations/index/jpa/IndexTest.java | 74 +++++++++++++++++++ .../src/test/resources/hibernate.properties | 1 + 14 files changed, 275 insertions(+), 40 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/index/jpa/Car.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/index/jpa/IndexTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index d13c3d17bf..bbea1b6e98 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -556,8 +556,9 @@ public final class AnnotationBinder { String table = ""; //might be no @Table annotation on the annotated class String catalog = ""; List uniqueConstraints = new ArrayList(); + javax.persistence.Table tabAnn = null; if ( clazzToProcess.isAnnotationPresent( javax.persistence.Table.class ) ) { - javax.persistence.Table tabAnn = clazzToProcess.getAnnotation( javax.persistence.Table.class ); + tabAnn = clazzToProcess.getAnnotation( javax.persistence.Table.class ); table = tabAnn.name(); schema = tabAnn.schema(); catalog = tabAnn.catalog(); @@ -711,7 +712,7 @@ public final class AnnotationBinder { //add process complementary Table definition (index & all) entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Table.class ) ); entityBinder.processComplementaryTableDefinitions( clazzToProcess.getAnnotation( org.hibernate.annotations.Tables.class ) ); - + entityBinder.processComplementaryTableDefinitions( tabAnn ); } // parse everything discriminator column relevant in case of single table inheritance diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java b/hibernate-core/src/main/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java index d45c6f11d0..4a74fdb18b 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/IndexOrUniqueKeySecondPass.java @@ -22,10 +22,16 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.cfg; +import java.util.ArrayList; +import java.util.List; import java.util.Map; +import java.util.StringTokenizer; import org.hibernate.AnnotationException; import org.hibernate.MappingException; +import org.hibernate.internal.util.StringHelper; +import org.hibernate.internal.util.collections.ArrayHelper; +import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.mapping.Column; import org.hibernate.mapping.Table; @@ -36,6 +42,7 @@ public class IndexOrUniqueKeySecondPass implements SecondPass { private Table table; private final String indexName; private final String[] columns; + private final String[] ordering; private final Mappings mappings; private final Ejb3Column column; private final boolean unique; @@ -47,10 +54,47 @@ public class IndexOrUniqueKeySecondPass implements SecondPass { this.table = table; this.indexName = indexName; this.columns = columns; + this.ordering = null; this.mappings = mappings; this.column = null; this.unique = false; } + //used for the new JPA 2.1 @Index + public IndexOrUniqueKeySecondPass(Table table, String indexName, String columnList, Mappings mappings, boolean unique) { + this.table = table; + StringTokenizer tokenizer = new StringTokenizer( columnList, "," ); + List tmp = new ArrayList(); + while ( tokenizer.hasMoreElements() ) { + tmp.add( tokenizer.nextToken().trim() ); + } + this.indexName = StringHelper.isNotEmpty( indexName ) ? indexName : "IDX_" + table.uniqueColumnString( tmp.iterator() ); + this.columns = new String[tmp.size()]; + this.ordering = new String[tmp.size()]; + initializeColumns(columns, ordering, tmp); + this.mappings = mappings; + this.column = null; + this.unique = unique; + } + + private void initializeColumns(String[] columns, String[] ordering, List list) { + for ( int i = 0, size = list.size(); i < size; i++ ) { + final String description = list.get( i ); + final String tmp = description.toLowerCase(); + if ( tmp.endsWith( " desc" ) ) { + columns[i] = description.substring( 0, description.length() - 5 ); + ordering[i] = "desc"; + } + else if ( tmp.endsWith( " asc" ) ) { + columns[i] = description.substring( 0, description.length() - 4 ); + ordering[i] = "asc"; + } + else { + columns[i] = description; + ordering[i] = null; + } + } + } + /** * Build an index @@ -68,21 +112,23 @@ public class IndexOrUniqueKeySecondPass implements SecondPass { this.columns = null; this.mappings = mappings; this.unique = unique; + this.ordering = null; } - + @Override public void doSecondPass(Map persistentClasses) throws MappingException { if ( columns != null ) { - for (String columnName : columns) { - addConstraintToColumn( columnName ); + for ( int i = 0; i < columns.length; i++ ) { + final String order = ordering != null ? ordering[i] : null; + addConstraintToColumn( columns[i], order ); } } if ( column != null ) { this.table = column.getTable(); - addConstraintToColumn( mappings.getLogicalColumnName( column.getMappingColumn().getQuotedName(), table ) ); + addConstraintToColumn( mappings.getLogicalColumnName( column.getMappingColumn().getQuotedName(), table ), null ); } } - private void addConstraintToColumn(String columnName) { + private void addConstraintToColumn(final String columnName, final String ordering) { Column column = table.getColumn( new Column( mappings.getPhysicalColumnName( columnName, table ) @@ -94,8 +140,8 @@ public class IndexOrUniqueKeySecondPass implements SecondPass { ); } if ( unique ) - table.getOrCreateUniqueKey( indexName ).addColumn( column ); + table.getOrCreateUniqueKey( indexName ).addColumn( column, ordering ); else - table.getOrCreateIndex( indexName ).addColumn( column ); + table.getOrCreateIndex( indexName ).addColumn( column, ordering ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java index eca4a64e24..a29cbda603 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java @@ -786,7 +786,11 @@ public class EntityBinder { null ); - //no check constraints available on joins + if ( secondaryTable != null ) { + TableBinder.addIndexes( table, secondaryTable.indexes(), mappings ); + } + + //no check constraints available on joins join.setTable( table ); //somehow keep joins() for later. @@ -905,7 +909,10 @@ public class EntityBinder { public void setIgnoreIdAnnotations(boolean ignoreIdAnnotations) { this.ignoreIdAnnotations = ignoreIdAnnotations; } - + public void processComplementaryTableDefinitions(javax.persistence.Table table) { + if ( table == null ) return; + TableBinder.addIndexes( persistentClass.getTable(), table.indexes(), mappings ); + } public void processComplementaryTableDefinitions(org.hibernate.annotations.Table table) { //comment and index are processed here if ( table == null ) return; diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java index 64dfb89c30..8a80098ab3 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java @@ -514,6 +514,21 @@ public class TableBinder { } } + public static void addIndexes(Table hibTable, javax.persistence.Index[] indexes, Mappings mappings) { + for ( javax.persistence.Index index : indexes ) { + //no need to handle inSecondPass here since it is only called from EntityBinder + mappings.addSecondPass( + new IndexOrUniqueKeySecondPass( + hibTable, + index.name(), + index.columnList(), + mappings, + index.unique() + ) + ); + } + } + /** * @deprecated Use {@link #buildUniqueConstraintHolders} instead */ diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/JPAOverriddenAnnotationReader.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/JPAOverriddenAnnotationReader.java index ff32adf499..2e34aa859f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/JPAOverriddenAnnotationReader.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/JPAOverriddenAnnotationReader.java @@ -2262,6 +2262,7 @@ public class JPAOverriddenAnnotationReader implements AnnotationReader { annotation.setValue( "schema", table.schema() ); annotation.setValue( "catalog", table.catalog() ); annotation.setValue( "uniqueConstraints", table.uniqueConstraints() ); + annotation.setValue( "indexes", table.indexes() ); } } if ( StringHelper.isEmpty( (String) annotation.valueOf( "schema" ) ) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/unique/DB2UniqueDelegate.java b/hibernate-core/src/main/java/org/hibernate/dialect/unique/DB2UniqueDelegate.java index 852b7c7854..15c2ae9646 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/unique/DB2UniqueDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/unique/DB2UniqueDelegate.java @@ -45,7 +45,7 @@ public class DB2UniqueDelegate extends DefaultUniqueDelegate { if ( hasNullable( uniqueKey ) ) { return org.hibernate.mapping.Index.buildSqlCreateIndexString( dialect, uniqueKey.getName(), uniqueKey.getTable(), - uniqueKey.columnIterator(), true, defaultCatalog, + uniqueKey.columnIterator(), uniqueKey.getColumnOrderMap(), true, defaultCatalog, defaultSchema ); } else { return super.applyUniquesOnAlter( @@ -88,9 +88,9 @@ public class DB2UniqueDelegate extends DefaultUniqueDelegate { } private boolean hasNullable( org.hibernate.mapping.UniqueKey uniqueKey ) { - Iterator iter = uniqueKey.getColumnIterator(); + Iterator iter = uniqueKey.columnIterator(); while ( iter.hasNext() ) { - if ( ( ( org.hibernate.mapping.Column ) iter.next() ).isNullable() ) { + if ( iter.next().isNullable() ) { return true; } } @@ -98,9 +98,8 @@ public class DB2UniqueDelegate extends DefaultUniqueDelegate { } private boolean hasNullable( UniqueKey uniqueKey ) { - Iterator iter = uniqueKey.getColumns().iterator(); - while ( iter.hasNext() ) { - if ( ( ( Column ) iter.next() ).isNullable() ) { + for ( Column column : uniqueKey.getColumns() ) { + if ( column.isNullable() ) { return true; } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java b/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java index 09e1d7e75d..cf67311f01 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/unique/DefaultUniqueDelegate.java @@ -115,11 +115,14 @@ public class DefaultUniqueDelegate implements UniqueDelegate { public String uniqueConstraintSql( org.hibernate.mapping.UniqueKey uniqueKey ) { StringBuilder sb = new StringBuilder(); sb.append( " unique (" ); - Iterator columnIterator = uniqueKey.getColumnIterator(); + Iterator columnIterator = uniqueKey.columnIterator(); while ( columnIterator.hasNext() ) { org.hibernate.mapping.Column column - = (org.hibernate.mapping.Column) columnIterator.next(); + = columnIterator.next(); sb.append( column.getQuotedName( dialect ) ); + if ( uniqueKey.getColumnOrderMap().containsKey( column ) ) { + sb.append( " " ).append( uniqueKey.getColumnOrderMap().get( column ) ); + } if ( columnIterator.hasNext() ) { sb.append( ", " ); } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Constraint.java b/hibernate-core/src/main/java/org/hibernate/mapping/Constraint.java index 9fff9398e7..be846eaf46 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Constraint.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Constraint.java @@ -38,7 +38,7 @@ import org.hibernate.engine.spi.Mapping; public abstract class Constraint implements RelationalModel, Serializable { private String name; - private final List columns = new ArrayList(); + private final List columns = new ArrayList(); private Table table; public String getName() { @@ -49,10 +49,6 @@ public abstract class Constraint implements RelationalModel, Serializable { this.name = name; } - public Iterator getColumnIterator() { - return columns.iterator(); - } - public void addColumn(Column column) { if ( !columns.contains( column ) ) columns.add( column ); } @@ -77,10 +73,14 @@ public abstract class Constraint implements RelationalModel, Serializable { } public Column getColumn(int i) { - return (Column) columns.get( i ); + return columns.get( i ); + } + //todo duplicated method, remove one + public Iterator getColumnIterator() { + return columns.iterator(); } - public Iterator columnIterator() { + public Iterator columnIterator() { return columns.iterator(); } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Index.java b/hibernate-core/src/main/java/org/hibernate/mapping/Index.java index da70576a05..85df62150e 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Index.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Index.java @@ -24,6 +24,8 @@ package org.hibernate.mapping; import java.io.Serializable; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -40,7 +42,8 @@ import org.hibernate.internal.util.StringHelper; public class Index implements RelationalModel, Serializable { private Table table; - private List columns = new ArrayList(); + private List columns = new ArrayList(); + private java.util.Map columnOrderMap = new HashMap( ); private String name; public String sqlCreateString(Dialect dialect, Mapping mapping, String defaultCatalog, String defaultSchema) @@ -50,6 +53,7 @@ public class Index implements RelationalModel, Serializable { getName(), getTable(), getColumnIterator(), + columnOrderMap, false, defaultCatalog, defaultSchema @@ -74,7 +78,8 @@ public class Index implements RelationalModel, Serializable { Dialect dialect, String name, Table table, - Iterator columns, + Iterator columns, + java.util.Map columnOrderMap, boolean unique, String defaultCatalog, String defaultSchema @@ -90,15 +95,30 @@ public class Index implements RelationalModel, Serializable { .append( " on " ) .append( table.getQualifiedName( dialect, defaultCatalog, defaultSchema ) ) .append( " (" ); - Iterator iter = columns; - while ( iter.hasNext() ) { - buf.append( ( (Column) iter.next() ).getQuotedName( dialect ) ); - if ( iter.hasNext() ) buf.append( ", " ); + while ( columns.hasNext() ) { + Column column = columns.next(); + buf.append( column.getQuotedName( dialect ) ); + if ( columnOrderMap.containsKey( column ) ) { + buf.append( " " ).append( columnOrderMap.get( column ) ); + } + if ( columns.hasNext() ) buf.append( ", " ); } buf.append( ")" ); return buf.toString(); } + public static String buildSqlCreateIndexString( + Dialect dialect, + String name, + Table table, + Iterator columns, + boolean unique, + String defaultCatalog, + String defaultSchema + ) { + return buildSqlCreateIndexString( dialect, name, table, columns, Collections.EMPTY_MAP, unique, defaultCatalog, defaultSchema ); + } + // Used only in Table for sqlCreateString (but commented out at the moment) public String sqlConstraintString(Dialect dialect) { @@ -131,7 +151,7 @@ public class Index implements RelationalModel, Serializable { return columns.size(); } - public Iterator getColumnIterator() { + public Iterator getColumnIterator() { return columns.iterator(); } @@ -139,6 +159,13 @@ public class Index implements RelationalModel, Serializable { if ( !columns.contains( column ) ) columns.add( column ); } + public void addColumn(Column column, String order) { + addColumn( column ); + if ( StringHelper.isNotEmpty( order ) ) { + columnOrderMap.put( column, order ); + } + } + public void addColumns(Iterator extraColumns) { while ( extraColumns.hasNext() ) addColumn( (Column) extraColumns.next() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java index c30ca994c6..2f9aeccfe1 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Table.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Table.java @@ -54,7 +54,7 @@ public class Table implements RelationalModel, Serializable { private Map columns = new LinkedHashMap(); private KeyValue idValue; private PrimaryKey primaryKey; - private Map indexes = new LinkedHashMap(); + private Map indexes = new LinkedHashMap(); private Map foreignKeys = new LinkedHashMap(); private Map uniqueKeys = new LinkedHashMap(); private int uniqueInteger; @@ -231,7 +231,7 @@ public class Table implements RelationalModel, Serializable { return columns.values().iterator(); } - public Iterator getIndexIterator() { + public Iterator getIndexIterator() { return indexes.values().iterator(); } @@ -239,11 +239,11 @@ public class Table implements RelationalModel, Serializable { return foreignKeys.values().iterator(); } - public Iterator getUniqueKeyIterator() { + public Iterator getUniqueKeyIterator() { return getUniqueKeys().values().iterator(); } - Map getUniqueKeys() { + Map getUniqueKeys() { cleanseUniqueKeyMapIfNeeded(); return uniqueKeys; } @@ -593,7 +593,7 @@ public class Table implements RelationalModel, Serializable { public Index getOrCreateIndex(String indexName) { - Index index = (Index) indexes.get( indexName ); + Index index = indexes.get( indexName ); if ( index == null ) { index = new Index(); @@ -606,11 +606,11 @@ public class Table implements RelationalModel, Serializable { } public Index getIndex(String indexName) { - return (Index) indexes.get( indexName ); + return indexes.get( indexName ); } public Index addIndex(Index index) { - Index current = (Index) indexes.get( index.getName() ); + Index current = indexes.get( index.getName() ); if ( current != null ) { throw new MappingException( "Index " + index.getName() + " already exists!" ); } @@ -705,6 +705,7 @@ public class Table implements RelationalModel, Serializable { } + public String getSchema() { return schema; } diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/UniqueKey.java b/hibernate-core/src/main/java/org/hibernate/mapping/UniqueKey.java index e5613817b9..8dbceb47a6 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/UniqueKey.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/UniqueKey.java @@ -22,8 +22,12 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.mapping; +import java.util.*; +import java.util.Map; + import org.hibernate.dialect.Dialect; import org.hibernate.engine.spi.Mapping; +import org.hibernate.internal.util.StringHelper; /** * A relational unique key constraint @@ -31,6 +35,7 @@ import org.hibernate.engine.spi.Mapping; * @author Brett Meyer */ public class UniqueKey extends Constraint { + private java.util.Map columnOrderMap = new HashMap( ); @Override public String sqlConstraintString( @@ -57,4 +62,14 @@ public class UniqueKey extends Constraint { this, defaultCatalog, defaultSchema ); } + public void addColumn(Column column, String order) { + addColumn( column ); + if ( StringHelper.isNotEmpty( order ) ) { + columnOrderMap.put( column, order ); + } + } + + public Map getColumnOrderMap() { + return columnOrderMap; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/index/jpa/Car.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/index/jpa/Car.java new file mode 100644 index 0000000000..a0bea61957 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/index/jpa/Car.java @@ -0,0 +1,45 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.annotations.index.jpa; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Index; +import javax.persistence.Table; + + +/** + * @author Strong Liu + */ +@Entity +@Table( indexes = {@Index( unique = true, columnList = "brand, producer") +, @Index( name = "Car_idx", columnList = "since DESC")}) +public class Car { + @Id + long id; + String brand; + String producer; + long since; + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/index/jpa/IndexTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/index/jpa/IndexTest.java new file mode 100644 index 0000000000..021ddcf26c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/index/jpa/IndexTest.java @@ -0,0 +1,74 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.annotations.index.jpa; + +import java.util.Iterator; + +import org.junit.Test; + +import static org.junit.Assert.*; + +import org.hibernate.internal.util.StringHelper; +import org.hibernate.mapping.Column; +import org.hibernate.mapping.Index; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.mapping.UniqueKey; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +/** + * @author Strong Liu + */ +public class IndexTest extends BaseCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Car.class }; + } + + @Test + public void testBasicIndex() { + PersistentClass carClass = configuration().getClassMapping( Car.class.getName() ); + Iterator itr = carClass.getTable().getUniqueKeyIterator(); + assertTrue( itr.hasNext() ); + UniqueKey uk = (UniqueKey) itr.next(); + assertFalse( itr.hasNext() ); + assertTrue( StringHelper.isNotEmpty( uk.getName() ) ); + assertEquals( 2, uk.getColumnSpan() ); + Column column = (Column) uk.getColumns().get( 0 ); + assertEquals( "brand", column.getName() ); + column = (Column) uk.getColumns().get( 1 ); + assertEquals( "producer", column.getName() ); + assertSame( carClass.getTable(), uk.getTable() ); + + + itr = carClass.getTable().getIndexIterator(); + assertTrue( itr.hasNext() ); + Index index = (Index)itr.next(); + assertFalse( itr.hasNext() ); + assertEquals( "Car_idx", index.getName() ); + assertEquals( 1, index.getColumnSpan() ); + column = index.getColumnIterator().next(); + assertEquals( "since", column.getName() ); + assertSame( carClass.getTable(), index.getTable() ); + } +} diff --git a/hibernate-core/src/test/resources/hibernate.properties b/hibernate-core/src/test/resources/hibernate.properties index 3d95e5b98e..cae1c6c221 100644 --- a/hibernate-core/src/test/resources/hibernate.properties +++ b/hibernate-core/src/test/resources/hibernate.properties @@ -29,6 +29,7 @@ hibernate.connection.username sa hibernate.connection.pool_size 5 hibernate.show_sql false +hibernate.format_sql true hibernate.max_fetch_depth 5