Hibernate:通过hbm2ddl反转列顺序 [英] Hibernate: Reversed column order by hbm2ddl

查看:194
本文介绍了Hibernate:通过hbm2ddl反转列顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让hbm2ddl为我创建表(用于开发目的),列的顺序与该类中的字段相反。



How我可以使它按照类的顺序创建列吗?



我认为Java类不存储字段的顺序,所以Hibernate只是简单的不知道源代码中的顺序是什么(如果我考虑更复杂的情况,这似乎是合乎逻辑的)。但是,我至少可以让Hibernate把PK和FK列作为第一个?



Hibernate 4.0.0(JBoss AS 7.1.2)
MySQL 5.1.x

已知的限制并且无法设置订单。但是在生产环境中使用Hibernate时,不应该依赖hbm2ddl,这是非常有限的,例如它可以添加列,但不能删除它。相反,人们通常使用 LiquiBase FlyWay DbDeploy ,这可以让您更灵活地控制架构更新。



为了描述这种方法如何提供帮助:比方说,你有一个UAT环境,并且你刚刚更新了模式 - 你想为现有的列添加非空约束。使用hbm2ddl这是不可能的,你会迫使你的QA从头开始重新创建数据库。使用上述工具,您需要添加一个SQL文件,这些工具将运行这个分别更新模式的新脚本。你也可以设置hbm2ddl = verify,以确保当前数据库模式是最新的,反之亦然--Hibernate映射是正确的。


I let hbm2ddl create the tables for me (for dev purposes), and the columns are in reverse order of the fields in the class.

How can I make it create the columns in the same order as the class has?

I think Java classes do not store the order of fields, so Hibernate simply does not know what the order in source is (which seems logical if I think about more complex cases).

But, can I at least ask Hibernate to put PK and FK columns as first ones?

Hibernate 4.0.0 (JBoss AS 7.1.2) MySQL 5.1.x

解决方案

Hibernate team says it's a known limitation and it's impossible to set the order. But you shouldn't rely on hbm2ddl when using Hibernate in production, it's pretty limited thing, for instance it can add column, but it can't remove it. Instead people usually create DB migrations with tools like LiquiBase or FlyWay or DbDeploy, this gives you more flexibility and control over the schema updates.

To describe how this approach helps: let's say you have a UAT environment and you just updated schema - you wanted to add not-null constraint to the existing column. With hbm2ddl it's not possible and you'll force your QAs to re-create the database from scratch. Using the tools described above you'll need to add an SQL file and those tools will run this new script which updates schema respectively. You also may want to set hbm2ddl=verify so that you're sure that current DB schema is up-to-date or vice versa - the Hibernate mapping is correct.

这篇关于Hibernate:通过hbm2ddl反转列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆