Hibernate 使用 Equals 生成 POJO

发布于 2024-08-26 11:43:08 字数 381 浏览 10 评论 0原文

我们在一个新项目中使用 hibernate,然后使用 hibernate.reveng.xml 创建 *.hbm.xml 文件和 POJO。我们希望在每个 POJO 中都有 equals 方法。 我发现您可以在 hbm 文件中使用 true 来标记在 equals 中使用哪些属性。但这意味着编辑大量文件,然后在将来如果/当我们修改数据库中的表或列时再次重新编辑文件。

所以我想知道是否有一种方法可以在 hibernate.reveng.xml 文件中的每个 pojo(table) 的 equals 方法中放置要使用的属性?

We are using hibernate in a new project where we use the hibernate.reveng.xml to create our *.hbm.xml files and POJOs after that. We want to have equals methods in each of our POJOs.
I found that you can use <meta attribute="use-in-equals">true</meta> in your hbm files to mark which properties to use in the equals. But this would mean editing alot of files, and then re-editing the files again in the future if/when we modify tables or columns in our DB.

So I was wondering if there is a way to place which properties to use in the equals method for each pojo(table) in the hibernate.reveng.xml file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

半透明的墙 2024-09-02 11:43:08

这是一个未记录的功能(在线 DTD 已过时)但您可以在 元素中添加 。换句话说,您应该能够执行以下操作:

<table name="PROPERTY" catalog="DATA" schema="PUBLIC">
  <meta attribute="extends">BasicDataObject</meta>
  <meta attribute="scope-class">public abstract</meta>
  <column name="ID">
    <meta attribute="use-in-equals">true</meta>
    <meta attribute="use-in-tostring">true</meta>
  </column>
</table>

hibernate-tools.jar 中的 DTD 应该是最新的(并且看起来像 这个)并声明:

<!ELEMENT column (meta*) >

Seam in Action 论坛中对此的讨论

This is an undocumented feature (and the online DTD is out of date) but you can add <meta> in the <table> and <colum> elements. In other words, you should be able to do this:

<table name="PROPERTY" catalog="DATA" schema="PUBLIC">
  <meta attribute="extends">BasicDataObject</meta>
  <meta attribute="scope-class">public abstract</meta>
  <column name="ID">
    <meta attribute="use-in-equals">true</meta>
    <meta attribute="use-in-tostring">true</meta>
  </column>
</table>

The DTD in hibernate-tools.jar should be up-to-date (and looks like this one) and declare:

<!ELEMENT column (meta*) >

There is a discussion about this in the Seam in Action forums.

莳間冲淡了誓言ζ 2024-09-02 11:43:08

eclipse 有 equal 和 hash 代码生成器。我们用它来生成这些方法。

eclipse has equal and hash code generator. We use that for generation of those methods.

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