-->
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: hbm2java: boolean getter name style
PostPosted: Wed Oct 31, 2007 9:34 pm 
Newbie

Joined: Wed Oct 31, 2007 9:16 pm
Posts: 9
I am using hbm2java in ant (hibernate-tools-3.2.0beta9.jar).

I want to generate my getters/setters for booleans properties
like isFoobar and hasFoobar in the style of

public boolean getIsFoobar() { ... }
public boolean getHasFoobar() { ... }

instead it is doing

public boolean isFoobar() { ... }
public boolean isHasFoobar() { ... }

(in case you wonder, I need to do this for backwards
compatibility as I convert to hibernate)

Is there anyway to do this?

I tried changing the generated name in PojoPropertyAccessors.ftl
but that does not work, because it does not get changed for
toString()/hashCode()/equals() generation. And I can fix PojoToString.ftl
but I don't see anyway to fix PojoEqualsHashcode.ftl (since it is all
generated by a call to pojo.generateEquals() and pojo.generateHashCode()).

Thank you for your help.
- Dan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 01, 2007 6:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what is your input here ?

if the property name is "hasFoobar" it must be "isHasFoobar" to be correct.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 01, 2007 12:08 pm 
Newbie

Joined: Wed Oct 31, 2007 9:16 pm
Posts: 9
For booleans either getXyz() for isXyz() is correct.
This has been a choice for POJO beans since the
very beginning of Java. I prefer getXyz() because of the consistency.
But more importantly we are converting EJBs to hibernate,
we used XDoclet to generate the data-objects for EJBs,
and it used the getXyz() form when generating getters for booleans.
So to avoid changing my code everywhere I would like to
have hbm2java also generate the getXyz() form.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 01, 2007 1:22 pm 
Newbie

Joined: Wed Oct 31, 2007 9:16 pm
Posts: 9
I have confirmed with the Java Bean Spec 1.01 that either form is acceptable. See http://java.sun.com/products/javabeans/docs/spec.html

And I looked at the code so I am pretty sure there is no way to choose.

I have an idea for a patch.
Can anyone tell me the best way to submit it?

Code:
--- tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java    (revision 14167)
+++ tools/src/java/org/hibernate/tool/hbm2x/pojo/BasicPOJOClass.java    (working copy)
@@ -566,7 +566,11 @@
         * @return String
         */
        public String getGetterSignature(Property p) {
-               String prefix = c2j.getJavaTypeName( p, false).equals( "boolean" ) ? "is" : "get";
+               String prefix = "get";
+               if ( c2j.getJavaTypeName(p, false).equals( "boolean" ) ) {
+                       MetaAttribute bgp = p.getMetaAttribute("boolean-getter-prefix");
+                       prefix = (bgp == null) ? "is" : MetaAttributeHelper.getMetaAsString(bgp);
+               }
                return prefix + beanCapitalize( p.getName() );
        }


Then you can add
Code:
          <meta attribute="boolean-getter-prefix">get</meta>

and get what you want.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 01, 2007 5:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
ok - now i understand what you want.

I don't like the fix though.

The workaround you can do is to customize the *.ftl's to generate a "getter" for every boolean property that will delegate to the "isser"

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 01, 2007 7:14 pm 
Newbie

Joined: Wed Oct 31, 2007 9:16 pm
Posts: 9
So you are saying to have both a getXyz() and a isXyz()?
Yes, that would work. I prefer my patch (so you just have one getter),
but I certainly don't want to maintain my own version of hbm2java :-)

In case anyone else needs it here is the code I added to PojoPropertyAccessors.ftl:

Code:
<#if pojo.getGetterSignature(property).startsWith("is") >
    /**
     * Calls ${pojo.getGetterSignature(property)}().
     * For backward compatability with old code that expects "get" instead of "is".
     */
    ${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} get${pojo.getPropertyName(property)}() {
        return ${pojo.getGetterSignature(property)}();
    }
</#if>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 02, 2007 2:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
exactly - also thinks this represents what you are actually doing (working around old code generation that didn't use the more proper "is" ;)

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