HibernateToolTask (hbm2hbmxml) 不会从 @org.hibernate.annotations.Index 注释在 hibernate 映射中生成索引
我正在尝试使用 hibernate 注释从 POJO 生成 hibernate 映射。 然后我想使用 liquibase 生成数据库架构。 所以我需要在 POJO 中定义索引。
示例POJO:
@Entity
public class A {
@Id
@GeneratedValue
private Long id;
@Index(name = "IDX_NAME")
@ForeignKey(name="sd")
private String name;
}
但是当我在ant中运行HibernateToolTask时:
<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>
我在映射中没有得到任何索引:
<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>
同时,当我执行hbm2ddl时 - 生成“创建索引”:
create table A (id bigint not null auto_increment, name varchar(255), primary key (id)) type=InnoDB;
create index IDX_NAME on A (name);
如何让hibernate在映射中生成索引?
更新:
我发现liquibase使用注释来生成模式,所以这部分问题解决了。我还有另外一个:
我想将现有数据库逆向工程为 POJO。 POJO 是从映射生成的,并且映射(使用 jdbcannotation-hbm2hbmxml 生成)没有任何索引。 我相信这本质上是同一个问题:hbm2hbmxml 不生成索引。
更新2:
为什么我需要它? 我有一个现有的数据库架构。我曾经更改过它,然后对 POJO 进行逆向工程。 现在我想使用 POJO 并通过注释生成映射和模式。
因此,我希望 POJO 与当前数据库模式匹配,以便继续使用它们。显然,除了外键名称和索引之外的所有内容都是匹配的。但是hbm2java不会生成@Index注释。,例如,
<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>
此任务在ddl中生成索引,并且不会在POJO中生成索引。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
意图尚不清楚,但这可能无法实现。从文档中:
欢迎贡献:)
您不必为此生成映射,您可以使用
。也许你应该准确地解释一下你想要做什么。The intention is not clear but this might just not be implemented. From the documentation:
Contributions welcome :)
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.