views:

214

answers:

1

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.

+1  A: 

HibernateToolTask (hbm2hbmxml) doesn’t generate index in hibernate-mapping from @o.h.a.Index annotations

The intention is not clear but this might just not be implemented. From the documentation:

4.4.3. Hibernate Mapping files exporter (<hbm2hbmxml>)

<hbm2hbmxml> generates a set of .hbm files. Intended to be used together with a when performing reverse engineering, but can be used with any kind of configuration. e.g. to convert from annotation based pojo's to hbm.xml.

Not every possible mapping transformation is possible/implemented (contributions welcome) so some hand editing might be necessary.

Contributions welcome :)

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.

You don't have to generate mapping for this, you can generate EJB 3 annotated POJOs from the database with <hbm2java>. Maybe you should explain what you're trying to do exactly.

Pascal Thivent
I added some explanation on what I am trying to do to the original post
vsevik