-->
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.  [ 14 posts ] 
Author Message
 Post subject: How to customize hashCode method generation?
PostPosted: Tue Nov 29, 2005 4:30 pm 
Beginner
Beginner

Joined: Tue Nov 15, 2005 6:56 pm
Posts: 39
Using latest code build of tools hibernate3.0-beta1

I want to customize the generation of the hashCode method for my classes. Right now, my hashCode method generated looks like this (generated by hibernate tools without customization):

Code:
 
   public int hashCode() {
         int result = 17;
         
         result = 37 * result + (
             getMySpecialId() == null ? 0 : this.getMySpecialId().hashCode() );
         
         return result;
   }   


I would like to customize it to look like this:

Code:
   public int hashCode() {
      return new   org.apache.commons.lang.builder.HashCodeBuilder(17,79)      .append(this.getMySpecialId()).toHashCode();
   } 


I have found that the code that generates the hashCode method is in BasicPojoClass.java (in hibernate tools), in the method generateHashCode(...). This method is called from the velocity template PojoEqualsHashcode.vm. I need to somehow override this method I think. I'm not sure how to go about doing this. If I derive my own class from BasicPojoCode then how do I make the rest of the code use my new class. Maybe I'm going about it all wrong?

Any ideas?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 5:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
would it not just be better to call some different code in the template ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 5:39 pm 
Beginner
Beginner

Joined: Tue Nov 15, 2005 6:56 pm
Posts: 39
Thanks for the suggestion.

However, I'm not sure what you're referring to when you say "template". Do you mean <hbmtemplate>? or do you mean the velocity "*.vm" template files?

I am using a custom <hbmtemplate> target in my ant file. Either way, I don't know what exact code and where I'd put it in either case to accomplish what I want.

If I create my own PojoEqualsHashcode.vm file, what do I put in there? I still need to have code somewhere that checks whether the property has the "use-in-equals" meta attribute etc. before using it in the hashCode method. Or is there a syntax to do that in the *.vm file?

Thanks,
Sam


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 5:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes, im talking about .vm and it is plain vanilla velocity so you can call java methods here too.

if you want to put some custom "tool" into the context then do something like

<hbmtemplate ...>
<property name="hibernatetool.myTool.toolclass" value="org.whatever.MyToolClass"/>
</hbmtemplate>

and $myTool will point to an instance of MyToolClass.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 6:02 pm 
Beginner
Beginner

Joined: Tue Nov 15, 2005 6:56 pm
Posts: 39
Thanks again.

I tried your suggestion and tried running something like this:

Code:
<hbmtemplate templatepath="${testoutputDir}/templates" template="pojo.vm"  exporterclass="MyPOJOExporter" filepattern="{package-name}/{class-name}">
            
<property name="hibernatetool.myTool.toolclass" value="MyExporterHelper"/>
         
</hbmtemplate>


I got an error running my ant file:

Code:
build.xml:87: Class org.apache.tools.ant.types.Environment$Variable doesn't support the "name" attribute.


I'm guessing it has to do with some wrong versions of libs in ant or something? or ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 6:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
sorry - use key instead of name on the property

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 7:40 pm 
Beginner
Beginner

Joined: Tue Nov 15, 2005 6:56 pm
Posts: 39
I tried your suggestion and keep getting this error when trying to reference my key in my .vm file:

Code:
$myTool.tester() is not a valid reference.



I'm assuming the "key" can be any string, right? So, I tried a simple test like this:

Code:
<hbmtemplate templatepath="${testoutputDir}/templates" template="pojo.vm"  exporterclass="MyPOJOExporter" filepattern="{package-name}/{class-name}">

<property key="myKey" value="com.mycompany.ExporterHelper"/>
         
</hbmtemplate>


I then try to reference it in my own PojoEqualsHashcode.vm file like so:

Code:
#if($pojo.needsEqualsHashCode() && !$clazz.Superclass)
   public boolean equals(Object other) {
         if ( (this == other ) ) return true;
       if ( (other == null ) ) return false;
       if ( !(other instanceof $pojo.getDeclarationName()) ) return false;
       $pojo.getDeclarationName() castOther = ( $pojo.getDeclarationName() ) other;
         
       return $pojo.generateEquals("this", "castOther", $jdk5);
   }
   
   public int hashCode() {
         int result = 17;
         
#foreach($property in $pojo.getAllPropertiesIterator())   
         $myKey.tester();
#end
         return result;
   }   
#end


Am I missing something here?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 8:33 pm 
Beginner
Beginner

Joined: Tue Nov 15, 2005 6:56 pm
Posts: 39
Solved the problem. Just my fat fingers misspelling some variables. Your solution worked. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 1:22 pm 
Newbie

Joined: Tue Mar 21, 2006 9:25 am
Posts: 8
Just in case anyone is looking for the answer and hasn't guessed it from the above, you have to do:

<property name="hibernatetool.myTool.toolclass" value="org.whatever.MyToolClass"/>

not just toolclass as suggested in the docs.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 2:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
where in the docs does it say that ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 12:41 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 5:26 am
Posts: 25
Location: Adelaide, South Australia
max wrote:
where in the docs does it say that ?


From:
http://www.hibernate.org/hib_docs/tools ... le/#d0e887

4.1.4.2. Getting access to user specific classes
If the templates need to access some user class it is possible by specifying a "toolclass" in the properties.

<property key="sometool.toolclass" value="x.y.z.NameOfToolClass"/>

Placing the above <property> tag in <hibernatetool> or inside any exporter will automatically create an instance of x.y.z.NameOfToolClass and it will be available in the templates as $sometool. This is usefull to delegate logic and code generation to java code instead of placing such logic in the templates.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 2:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
whoops....fixed in svn (the doc that is)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 2:53 am 
Beginner
Beginner

Joined: Fri Jun 10, 2005 5:26 am
Posts: 25
Location: Adelaide, South Australia
max wrote:
whoops....fixed in svn (the doc that is)


Much appreciated.


Top
 Profile  
 
 Post subject: Re: How to customize hashCode method generation?
PostPosted: Wed May 31, 2006 9:34 am 
Newbie

Joined: Wed May 31, 2006 6:40 am
Posts: 19
plutonianelephant wrote:
Using latest code build of tools hibernate3.0-beta1

I want to customize the generation of the hashCode method for my classes.


i'm using beta 5 and i want to customize the generation of ejb3 home... i'd like to add interface generation and add other find methods...

for example if i have entity1 linked to entity2 with default lazy loading the method

entity1 findById(entity1id)

doesn't load the linked entity2 while i'd like to have a method like


entity1 findByIdWithLinkedEntity2s(entity1id)

that load entity1 with linked entity2

is it also possible to have separeted directories or package??? like
${baseDirOrPackage}.ejb3.pojo or ${baseDirOrPackage}.ejb3.entity
${baseDirOrPackage}.ejb3.bean
${baseDirOrPackage}.ejb3.interfaces

Thanks in advice & Hi To all


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 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.