-->
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.  [ 8 posts ] 
Author Message
 Post subject: POJO code generation: how to prefix fields?
PostPosted: Wed Dec 09, 2009 5:46 pm 
Newbie

Joined: Mon Jan 12, 2009 4:38 pm
Posts: 19
Hello,

I am using hibernate tools to generate pojo from an existing database. It works fine, except that I would like to prefix the generated field names. That is, instead of e.g.
Code:
private String name;
public Criterion(String name) {
    this.name = name;
}
//...
@Column(name = "name", nullable = false)
public String getName() {
    return this.name;
}

I'd like to have
Code:
private String m_name;
public Criterion(String name) {
    this.m_name = name; // (or even better, simply m_name = name, but I don't mind so much)
}
//...
@Column(name = "name", nullable = false)
public String getName() {
    return this.m_name; // or return m_name;
}


(If you are curious, the goal is to allow leaving the compiler option to error when a variable name overrides a field name.)

This would be systematic, thus I'd like to set the option once and for all and not for every property and every class. I guess it must be feasible and simple, but could not find it, e.g., in https://www.hibernate.org/hib_docs/tool ... e/#codegen or anywhere I looked... Customizing the namingstrategy does not seem to fit the purpose either as it seems to work the other way around (deriving column names from mapping names). BTW, note that I don't need to change the mapping itself.

Thanks for any pointer.
Olivier

_________________
Olivier


Top
 Profile  
 
 Post subject: Re: POJO code generation: how to prefix fields?
PostPosted: Thu Dec 10, 2009 8:46 am 
Senior
Senior

Joined: Tue Aug 04, 2009 7:45 am
Posts: 124
Use custom templates for this.
Look at templates(in hibernate-tools.jar): PojoConstructors.ftl, PojoFields.ftl, PojoPropertyAccessors.ftl into folder pojo.


Top
 Profile  
 
 Post subject: Re: POJO code generation: how to prefix fields?
PostPosted: Thu Dec 10, 2009 11:43 am 
Newbie

Joined: Mon Jan 12, 2009 4:38 pm
Posts: 19
Thanks a lot.

For other's help, I also used an information here https://forum.hibernate.org/viewtopic.php?f=6&t=959907&start=0, about how to name directories for hibernate tools to find the template files.

I'm using eclipse. Basically, using eclipse Team synch with an SVN plug-in, I added the SVN repository 'http://anonsvn.jboss.org/repos/hibernate/branches/Branch_3_2/HibernateExt/', browsed to http://anonsvn.jboss.org/repos/hibernat ... lates/pojo, right-clicked and checked out into my existing project, in a subfolder I chose to name 'hibernate-templates', which thus contained a subfolder 'pojo' with the default templates.

Then I modified the following parts in PojoConstructors.ftl:
Code:
    public ${pojo.getDeclarationName()}() {
       /** Default generated constructor. */
    }
I added a comment to avoid the compiler to complain that a block is empty.
Code:
<#foreach field in pojo.getPropertiesForMinimalConstructor()>
        m_${field.name} = ${field.name};
</#foreach>

Code:
<#foreach field in pojo.getPropertiesForFullConstructor()>
       m_${field.name} = ${field.name};
</#foreach>
My so loved m_ prefix (when init in the constructors).


In PojoEqualsHashcode.ftl:
Code:
<#if pojo.needsEqualsHashCode() && !clazz.superclass?exists>   @Override
   public boolean equals(Object other) {
//...
   @Override
   public int hashCode() {
To avoid compiler warning that Override annotation is missing.


In PojoFields.ftl:
Code:
</#if>    ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} m_${field.name}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
Prefix when declaring the fields.

And in PojoPropertyAccessors.ftl:
Code:
    ${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() {
        return m_${property.name};
    }
   
    ${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name}) {
        m_${property.name} = ${property.name};
    }
Prefix when accessing the field.

I deleted the other ftl files from my project.

I think that the prefix thing should be optionally available using a configuration property in hibernate tools, because I think it is good practice to enable the compiler complaint when a local variable name overrides a field name, and the current default behavior of hibernate tools (requiring to modify the template, which is not such an easy thing at least for a beginner, IMHO) rather encourages the user to simply uncheck the compiler warning.

Even more advanced, the hibernate tools, when used as an eclipse plug-in, could use the fields prefix and suffix properties which can be set in Window / Preferences, Java / Code style.

Thanks again for your help.

_________________
Olivier


Top
 Profile  
 
 Post subject: Re: POJO code generation: how to prefix fields?
PostPosted: Thu Dec 10, 2009 12:58 pm 
Newbie

Joined: Mon Jan 12, 2009 4:38 pm
Posts: 19
No time to do it for now (have to register, and so on)...

Anybody reading this, if you like the idea, feel free to push it...

(May I post on http://opensource.atlassian.com/project ... teNew=true, BTW, where I already have an account?)
Olivier

_________________
Olivier


Top
 Profile  
 
 Post subject: Re: POJO code generation: how to prefix fields?
PostPosted: Thu Dec 10, 2009 7:29 pm 
Newbie

Joined: Mon Jan 12, 2009 4:38 pm
Posts: 19
Well, I've taken the time to post the feature request [https://jira.jboss.org/jira/browse/JBIDE-5441]. Thank you again for your comments.

_________________
Olivier


Top
 Profile  
 
 Post subject: Re: POJO code generation: how to prefix fields?
PostPosted: Fri Dec 11, 2009 5:35 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
OlivierMiR wrote:
Hello,
(If you are curious, the goal is to allow leaving the compiler option to error when a variable name overrides a field name.)


You could place the reverse engineered classes and hibernate.reveng.xml in a separate project and switch off all warnings there.
Then establish a dependency on that project in your main project. (Not as source folders, I think the project's properties and "Project References" will do the trick.)


Top
 Profile  
 
 Post subject: Re: POJO code generation: how to prefix fields?
PostPosted: Fri Dec 11, 2009 6:18 am 
Newbie

Joined: Mon Jan 12, 2009 4:38 pm
Posts: 19
Indeed, that's the trick I used for an other project, but I felt this was a bit of an overkill in the case here, because the db and project is rather simple, I don't want to turn it into a multi-project configuration.

_________________
Olivier


Top
 Profile  
 
 Post subject: Re: POJO code generation: how to prefix fields?
PostPosted: Thu Aug 05, 2010 5:34 am 
Newbie

Joined: Tue Aug 03, 2010 5:04 am
Posts: 2
Olivier can you clear how to apply the new templates to be used during the reverse engineering processes with eclipse?
10x


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