views:

323

answers:

2

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?

+1  A: 

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

dr jerry
@dr jerry When you db schema changes, do you regenerate your pojos and then manually go back through them and readd the equals add hash code?
jschoen
No after db changes, i do not regenerate. At some point we left the generation thing and do a manual update of our pojo's. Our configuration is too much tweaked to benefit from code generation.
dr jerry
+2  A: 

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.

Pascal Thivent
@Pascal Thanks man. That was exactly what I was looking for.
jschoen
@jschoen you're welcome
Pascal Thivent