I have a problem with the @Index annotation. I have code that worked with a previous version (don't remember which one, 3.1beta5?), but with latest releases (Hibernate 3.1.1 + Annotations 3.1beta8) no index is generated.
Is it possible that this is broken?
Yes, I am aware of the problem with SchemaUpdate (hibernate.hbm2ddl.auto = update) and index creation.
I am also aware of the Jira issue
http://opensource2.atlassian.com/projec ... wse/ANN-32
which says that this is _not_ a bug.
My setup:
Hibernate 3.1.1
Annotations 3.1beta8
(Same problem also in H3.1+A3.1beta7)
mysql-connector 3.1.11
MySQL 4.14 (Win32)
This code does not work:
Code:
package test.data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Index;
@Entity()
@Table(name = "my_entity")
public class MyEntity {
private String firstName;
private String lastName;
Long id;
public MyEntity() {
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Index(name = "firstNameIx")
@Column(name = "first_namex")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Index(name = "lastNameIx")
@Column(name = "last_namex")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Here is my config:
Code:
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.default_schema">test</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<!-- The list if annotated classes -->
<mapping class="test.data.MyEntity"/>
<mapping class="test.data.MyOtherEntity"/>
</session-factory>
</hibernate-configuration>
As a workaround, I have managed to get this one working (not as elegant though, especially for entities with many index columns)
Code:
package test.data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Index;
@Entity()
@Table(name="my_other_entity")
@org.hibernate.annotations.Table(name="my_other_entity",
indexes = {
@Index(name="firstNameIx", columnNames={"first_name"} ),
@Index(name="lastNameIx", columnNames={"last_name"} )
} )
public class MyOtherEntity {
private String firstName;
private String lastName;
Long id;
public MyOtherEntity() {
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="first_name")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name="last_name")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
When I run these entities in a simple test, I see the folloing in my logs (no index creation SQL is executed for MyEntity)
Code:
14:00:07,046 INFO Environment:479 - Hibernate 3.1.1
14:00:07,046 INFO Environment:509 - hibernate.properties not found
14:00:07,062 INFO Environment:525 - using CGLIB reflection optimizer
14:00:07,062 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling
14:00:07,125 INFO Configuration:1296 - configuring from resource: /hibernate.cfg.xml
14:00:07,125 INFO Configuration:1273 - Configuration resource: /hibernate.cfg.xml
14:00:07,421 INFO Configuration:1407 - Configured SessionFactory: null
14:00:07,531 INFO AnnotationBinder:322 - Binding entity from annotated class: test.data.MyEntity
14:00:07,562 INFO EntityBinder:300 - Bind entity test.data.MyEntity on table my_entity
14:00:07,625 INFO AnnotationBinder:322 - Binding entity from annotated class: test.data.MyOtherEntity
14:00:07,625 INFO EntityBinder:300 - Bind entity test.data.MyOtherEntity on table my_other_entity
14:00:07,703 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
14:00:07,703 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
14:00:07,703 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
14:00:07,718 INFO DriverManagerConnectionProvider:80 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/test
14:00:07,718 INFO DriverManagerConnectionProvider:86 - connection properties: {user=test}
14:00:07,968 INFO SettingsFactory:77 - RDBMS: MySQL, version: 4.1.14-nt-max
14:00:07,968 INFO SettingsFactory:78 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.11 ( $Date: 2005-09-21 18:20:03 +0000 (Wed, 21 Sep 2005) $, $Revision: 4287 $ )
14:00:08,000 INFO Dialect:103 - Using dialect: org.hibernate.dialect.MySQLInnoDBDialect
14:00:08,000 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
14:00:08,000 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
14:00:08,015 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
14:00:08,015 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
14:00:08,015 INFO SettingsFactory:136 - JDBC batch size: 15
14:00:08,031 INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
14:00:08,031 INFO SettingsFactory:144 - Scrollable result sets: enabled
14:00:08,031 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): enabled
14:00:08,031 INFO SettingsFactory:160 - Connection release mode: auto
14:00:08,031 INFO SettingsFactory:178 - Default schema: test
14:00:08,031 INFO SettingsFactory:184 - Maximum outer join fetch depth: 2
14:00:08,031 INFO SettingsFactory:187 - Default batch fetch size: 1
14:00:08,031 INFO SettingsFactory:191 - Generate SQL with comments: disabled
14:00:08,031 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
14:00:08,031 INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
14:00:08,031 INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
14:00:08,031 INFO SettingsFactory:203 - Query language substitutions: {}
14:00:08,031 INFO SettingsFactory:209 - Second-level cache: enabled
14:00:08,031 INFO SettingsFactory:213 - Query cache: disabled
14:00:08,031 INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.EhCacheProvider
14:00:08,031 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
14:00:08,031 INFO SettingsFactory:237 - Structured second-level cache entries: disabled
14:00:08,046 INFO SettingsFactory:257 - Echoing all SQL to stdout
14:00:08,046 INFO SettingsFactory:264 - Statistics: disabled
14:00:08,046 INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled
14:00:08,046 INFO SettingsFactory:283 - Default entity-mode: pojo
14:00:08,078 INFO SessionFactoryImpl:153 - building session factory
14:00:08,093 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/java/hibernate-3.1/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
14:00:08,406 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
14:00:08,421 INFO SchemaExport:155 - Running hbm2ddl schema export
14:00:08,421 DEBUG SchemaExport:173 - import file not found: /import.sql
14:00:08,421 INFO SchemaExport:182 - exporting generated schema to database
14:00:08,421 DEBUG SchemaExport:296 - drop table if exists test.my_entity
14:00:08,468 DEBUG SchemaExport:296 - drop table if exists test.my_other_entity
14:00:08,500 DEBUG SchemaExport:296 - create table test.my_entity (id bigint not null auto_increment, first_namex varchar(255), last_namex varchar(255), primary key (id)) type=InnoDB
14:00:08,625 DEBUG SchemaExport:296 - create table test.my_other_entity (id bigint not null auto_increment, first_name varchar(255), last_name varchar(255), primary key (id)) type=InnoDB
14:00:08,734 DEBUG SchemaExport:296 - create index lastNameIx on test.my_other_entity (last_name)
14:00:09,031 DEBUG SchemaExport:296 - create index firstNameIx on test.my_other_entity (first_name)
14:00:09,296 INFO SchemaExport:202 - schema export complete
Hibernate: insert into test.my_entity (first_namex, last_namex) values (?, ?)