-->
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.  [ 7 posts ] 
Author Message
 Post subject: Using my own class from templates
PostPosted: Sat Feb 17, 2007 12:19 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Hi,

I'm trying to customize my equals() and hasCode() methods so that it is based on the columns that have a unique constraint. So I was able to actually get part of it to work.

I defined the following in my ant target, within the hbm2java task.
Code:
<property key="hibernatetool.myequals.toolclass"
         value="org.hibernate.tool.hbm2x.pojo.MyPojoClass"/>


Here's my PojoEqualsHashcode.ftl
------------------------------------------------------------
<#if myequals.isUniqueConstraintExist(pojo) && !clazz.superclass?exists>
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 ${myequals.generateEquals(pojo, "this", "castOther", jdk5)};
}

public int hashCode() {
int result = 17;

<#foreach property in pojo.getAllPropertiesIterator()>
${pojo.generateHashCode(property, "result", "this", jdk5)}
</#foreach> return result;
}
</#if>
----------------------------------------------------------------

If I don't use myequals in the <#if ...> statement this whole thing works. But if I used it like shown above, I get the following error.

Code:
ERROR -
[hibernatetool] Expression myequals.isUniqueConstraintExist is undefined on
line 1, column 6 in pojo/PojoEqualsHashcode.ftl.
[hibernatetool] The problematic instruction:
[hibernatetool] ----------
[hibernatetool] ==> if myequals.isUniqueConstraintExist(pojo) && !clazz.superclass?exists [on line 1, column 1 in pojo/PojoEqualsHashcode.ftl]
[hibernatetool] in include "PojoEqualsHashcode.ftl" [on line 16, column 1 in
pojo/Pojo.ftl]
[hibernatetool] ----------


Thanks in advance.
Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 17, 2007 12:22 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Sorry. just one clarification point. The error says that the expression is undefined. I verified that it actually does exist.

Am I using it with the wrong syntax?

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 17, 2007 1:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
show the code of your MyPojoClass....

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 17, 2007 1:29 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Hi Max,

Here it is.

I'm sure there are better ways of doing the same thing. I would be open to suggestions as well.

So the generateEquals() method works. The isUniqueConstraintExist is the one giving me problems.

Code:

package org.hibernate.tool.hbm2x.pojo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Property;

public class MyPojoClass {
   private static final Logger logger = Logger.getLogger(MyPojoClass.class);
   
   // This class is instantiated once during reverse engineering
   public MyPojoClass () {
      logger.info("MyPojoClass constructed.");
   }
   
   public String generateEquals(BasicPOJOClass pojo,
         String thisName, String otherName, boolean useGenerics) {
      logger.info("Calling my generateEquals, pojo: " +
pojo.getClass().getName());
      
      List uniqueProps = getUniqueKeys(pojo);
      
      return pojo.generateEquals(thisName, otherName,
uniqueProps.iterator(), useGenerics);
      
   }
   
   public List getUniqueKeys(BasicPOJOClass pojo) {
      List uniqueProps = new ArrayList();
      Iterator i = pojo.getAllPropertiesIterator();
      while (i.hasNext()) {
         Property p = (Property)i.next();
         //logger.info("Iterating through props: " + p.getName());
         Iterator colIter = p.getColumnIterator();
         while (colIter.hasNext()) {
            Column c = (Column)colIter.next();
            //logger.info("Column iter: " + c.getName());
            
            if (c.isUnique() && !c.getName().equals("id")) {
               logger.info("UNIQUE: " + p.getName());
               uniqueProps.add(p);
            }
         }
      }
      
      return uniqueProps;
   }
   
   boolean isUniqueConstraintExist(BasicPOJOClass pojo) {
      List uniqueProps = getUniqueKeys(pojo);
      
      if (uniqueProps.size() > 0) {
         return true;
      }
      
      return false;
   }

}



Thanks
Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 2:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hmmm....could you try and rename the method to something without *is* in it ? just to check that it's not freemarker messing up the names...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 4:07 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Ok this was a stupid mistake. I didn't have a "public" keyword for the method in question.

I was trying to use the same method someplace else and it gave me the same error. So It forced me to look athe the signature closer.

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 4:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
ah of course...sorry for not spotting that one ;)

_________________
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.  [ 7 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.