-->
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.  [ 2 posts ] 
Author Message
 Post subject: DB to Entity field annotation
PostPosted: Thu Mar 27, 2008 9:11 pm 
Newbie

Joined: Thu Mar 27, 2008 9:00 pm
Posts: 2
I'm using hbm2java under Ant to generate EJB3 Entity classes from a Sybase DB, everything works fine except for the fact by default annotation is on property (i.e., on getXXX() methods). I'm wondering if there is any way to switch to field annotation? I did not find anything in Hibernate Tool reference.

BTW, I'm using Hibernate Tools 3.2.0 GA.

Thanks


Top
 Profile  
 
 Post subject: Re: DB to Entity field annotation
PostPosted: Fri Mar 28, 2008 3:57 pm 
Newbie

Joined: Thu Mar 27, 2008 9:00 pm
Posts: 2
Here is what did so far:

1. create two new files under pojo:
Ejb3FieldGetAnnotation.ftl
Code:
<#if ejb3>
<#if pojo.hasIdentifierProperty()>
<#if field.equals(clazz.identifierField)>
${pojo.generateAnnIdGenerator()}
<#-- if this is the id field -->
<#-- explicitly set the column name for this field-->
</#if>
</#if>
<#if c2h.isManyToOne(field)>
<#--TODO support @OneToOne true and false-->   
${pojo.generateManyToOneAnnotation(field)}
<#--TODO support optional and targetEntity-->   
${pojo.generateJoinColumnsAnnotation(field, cfg)}
<#elseif c2h.isCollection(field)>
${pojo.generateCollectionAnnotation(field, cfg)}
<#else>
${pojo.generateBasicAnnotation(field)}
${pojo.generateAnnColumnAnnotation(field)}
</#if>
</#if>


GetFieldAnnotation.ftl
Code:
<#include "Ejb3FieldGetAnnotation.ftl"/>


2. modified PojoFields.ftl to include ejb ennotation.
Code:
<#-- // Fields -->

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

</#if>
</#foreach>


3. modified PojoPropertyAccessors.ftl to get rid of ejb3 annotation.
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>


So far it works except missing annotations for identity ID field, the reason seems to be field.equals(clazz.identifierField) returns false. What is the correct expression to check if a field is ientifier field?


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