-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Including primary key fields in tostring and equals methods
PostPosted: Mon Apr 02, 2007 1:04 pm 
Newbie

Joined: Mon Apr 02, 2007 12:09 pm
Posts: 4
I am using hibernate tools to generate hbm files and pojos. I would like generated pojos to have toString() and equals() methods. Also equals() method should only compare primary key field(s). Since I don't want to manually edit the hbm.xml files every time I generate them before generating pojos, I have including the following two lines in "persistentclass.hbm.ftl" file.

<meta attribute="use-in-tostring">true</meta>
<meta attribute="use-in-equals">true</meta>

This works fine and generated pojos with toString and equals() methods however equals() method compares all the fields. How can I restrict it to include only primary key field(s).

I am using

Hibernate 3.2.2

Here is the generated hbm.xml after above modification to "persistentclass.hbm.ftl"


<hibernate-mapping>
<class name="com.icrossing.audienceengine.dao.hibernate.mapping.keyword_list" table="keyword_lists" schema="public">
<meta attribute="use-in-tostring">true</meta>
<meta attribute="use-in-equals">true</meta>
<comment>This table stores the main components of user work list</comment>
<id name="keywordListId" type="java.lang.Integer">
<column name="kl_id" />
<generator class="org.hibernate.id.SequenceGenerator">
<param name="sequence">keyword_list_keword_list_id_seq</param>
</generator>
</id>
..........
..........
list of fields removed to reduce size
..........
</class>
</hibernate-mapping>

_________________
Deepak Pydimarry


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 3:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
don't but use-in-equals in the root of <class> if you don't mean it for all properties.

Only put it on the fields you want it for.

And btw. *DONT* use the primary key in equals/hashcode if you use automatic id's and detached objects with hash based collections. (see www.hibernate.org/109.html)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 3:37 pm 
Newbie

Joined: Mon Apr 02, 2007 12:09 pm
Posts: 4
Is there any documentation on the freemarker templates used by hibernate tools to generate hbm and java files. It would be very helpfult when we need some cutomizations like including few fields in toString or equals etc..

_________________
Deepak Pydimarry


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 3:45 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the documentation contains doc for the meta tags etc.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.