I am trying to generate hibernate-mapping from POJOs with hibernate annotations. Then I want to use liquibase to generate database schema. So I need indexes to be defined in my POJOs.
Sample POJO:
@Entity
public class A {
@Id
@GeneratedValue
private Long id;
@Index(name = "IDX_NAME")
@ForeignKey(name="sd")
private String name;
}
But when I run HibernateToolTask in ant:
<hibernateTool>
<classpath>
<path location="${path}"/>
</classpath>
<annotationconfiguration configurationfile="src/hibernate.cfg.xml"/>
<hbm2hbmxml destdir="${project.dir}"/>
<hbm2ddl destdir="database/liquibase" export="false" outputfilename="update_${stamp}.sql" />
</hibernateTool>
I don't get any indexes in mapping:
<class name="A" table="A">
<id name="id" type="java.lang.Long" access="field">
<column name="id" />
<generator class="native"></generator>
</id>
<property name="name" type="java.lang.String" access="field">
<column name="name" />
</property>
</class>
At the same time, when I do hbm2ddl - 'create index' is generated:
create table A (id bigint not null auto_increment, name varchar(255), primary key (id)) type=InnoDB;
create index IDX_NAME on A (name);
How can I make hibernate generate indexes in the mapping?
UPDATE:
I found out, that liquibase uses annotations to generate schema, so this part of problem is solved. I still have another one:
I want to reverse engineer existing database to POJOs. POJOs are generated from mapping and mapping (generated using jdbcannotation-hbm2hbmxml) doesn't have any indexes. I believe this is essentially the same problem: hbm2hbmxml doesn't generate indexes.
UPDATE 2:
Why do I need that? I have an existing database schema. I used to change it and then reverse engineer POJOs. Now I want to work with POJOs and generate mapping and schema by annotations.
So I'd like to have POJOs matching current database schema to move on with them. Apparently everything besides foreign key names and indexes is matching. But hbm2java doesn't generate @Index annotation., e.g.
<hibernateTool>
<jdbcconfiguration propertyfile="${build.dir}/etc/hibernate.properties" packagename="${doPackageName}"/>
<hbm2java destdir="${destinationDir}" jdk5="true" ejb3="true"/>
<hbm2ddl destdir="${destinationDir}" export="false" outputfilename="update_${stamp}.sql" />
</hibernateTool>
This task generates indexes in ddl and doesn't generate indexes in POJOs.