-->
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: Hibernate Code Generation - Controlling AccessType??
PostPosted: Sun Apr 22, 2007 8:39 pm 
Newbie

Joined: Tue Apr 17, 2007 8:05 pm
Posts: 5
Hi,

I posted a similar message on the general forum a few days ago, but perhaps this is the better place... I have also since found that it relates to the AccessType being Property or Field... so a re-wording of my original post taking this into consideration is below...

Just wondering if it is (relatively easily) possible to change the layout of the generated POJO code in terms of AccessType being Field rather than Property.

I have figured out how to use TableNameStrategy to put my generated classes into the right packages with a bit of renaming on the way, so that is all good (and very handy I might add!). Can you change the AccessType in a similar way?

Is the AccessType mainly a matter of style? Is there any reason to use Property over Field.

Some of the other tools I have seen, and many examples seem to use the Field AccessType rather than Property, even though Property seems to be the default. In terms of style, I think I prefer to have it on the field, unless their is a good reason not to.

I have also found that the location of the &Id annotation determines the AccessType if it is not directly specified... Is there any advantage in explicitly setting it?

Thanks for any advice

Regards
Colin


Top
 Profile  
 
 Post subject: Re: Hibernate Code Generation - Controlling AccessType??
PostPosted: Mon Apr 23, 2007 4:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
cfraser wrote:
Just wondering if it is (relatively easily) possible to change the layout of the generated POJO code in terms of AccessType being Field rather than Property.


Not currently build in; but I guess it would be trivial to add logic in the templates to check the access type to control where the annotations goes.

Quote:
I have figured out how to use TableNameStrategy to put my generated classes into the right packages with a bit of renaming on the way, so that is all good (and very handy I might add!). Can you change the AccessType in a similar way?


not currently; but definitly doable - wanna contribute it ? :)

Quote:
Is the AccessType mainly a matter of style? Is there any reason to use Property over Field.


Use property if you want to be able to easily put a breakpoint to see what hibernate actually assigns/gets to/from your pojos.

Use field if you don't have any reason to have get/set properties for your fields or if the getter/setters might contain logic that conflicts with the semantics of hibernate/jpa.

Quote:
I have also found that the location of the &Id annotation determines the AccessType if it is not directly specified... Is there any advantage in explicitly setting it?


No, besides it being explicit.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 8:01 am 
Newbie

Joined: Mon Sep 19, 2005 8:11 pm
Posts: 5
Here is a dirty hack

In your hibernate-tools.jar found in

eclipse\plugins\org.hibernate.eclipse_3.2.0.beta9a\lib\tools

change

PojoFields.ftl to

Code:
<#-- // Fields -->

<#foreach property in pojo.getAllPropertiesIterator()><#if pojo.getMetaAttribAsBool(property, "gen-property", true)> <#if pojo.hasMetaAttribute(property, "field-description")>    /**
     ${pojo.getFieldJavaDoc(property, 0)}
     */
</#if>   
  <#include "GetPropertyAnnotation.ftl"/>
${pojo.getFieldModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${property.name}<#if pojo.hasFieldInitializor(property, jdk5)> = ${pojo.getFieldInitialization(property, jdk5)}</#if>;
</#if>
</#foreach>



and PojoPropertyAccessors.ftl to

Code:
<#-- // Property accessors -->
<#foreach property in pojo.getAllPropertiesIterator()>
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
<#if pojo.hasFieldJavaDoc(property)>   
    /**       
     * ${pojo.getFieldJavaDoc(property, 4)}
     */
</#if>   
    ${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() {
        return this.${property.name};
    }
   
    ${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name}) {
        this.${property.name} = ${property.name};
    }
</#if>
</#foreach>



Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 12:20 am 
Newbie

Joined: Tue Apr 17, 2007 8:05 pm
Posts: 5
Well, it has been a while... but getting back to it, this looks like it will do what I am after and get me well and truly on my way!

Thank you!

I also found I could extract the files to a Pojo sub dir and set the 'Use Templates' setting rather than substituting the files back into the jar file.

Does anyone have any thoughts on whether the annotations should be on the public rather than the private fields???

Thanks again
Regards
Colin


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.