HibernateToolTask​​ (hbm2hbmxml) 不会从 @org.hibernate.annotations.Index 注释在 hibernate 映射中生成索引

发布于 2024-09-16 02:44:51 字数 2166 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中楼上月下 2024-09-23 02:45:00

HibernateToolTask​​ (hbm2hbmxml) 不会从 @ohaIndex 注释在 hibernate 映射中生成索引

意图尚不清楚,但这可能无法实现。从文档中:

4.4.3。 Hibernate 映射文件导出器 ()

生成一组.hbm
文件。旨在一起使用
与当
执行逆向工程,但是
可以与任何类型的
配置。例如转换自
基于 pojo 的 hbm.xml 注释。

并非所有可能的映射转换都是可能/实现的(欢迎贡献),因此可能需要进行一些手动编辑。

欢迎贡献:)

我想将现有数据库逆向工程为 POJO。 POJO 是从映射生成的,并且映射(使用 jdbcannotation-hbm2hbmxml 生成)没有任何索引。我相信这本质上是同一个问题:hbm2hbmxml 不生成索引。

您不必为此生成映射,您可以使用 。也许你应该准确地解释一下你想要做什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文