-->
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: adding unpersisted fields to a POJO
PostPosted: Tue Aug 08, 2006 7:45 pm 
Newbie

Joined: Sun Aug 06, 2006 12:55 am
Posts: 11
I'm using the "Hibernate Code Generation" wizard in Eclipse to create my POJO model classes.

My web tier would like to add a few fields (that wont ever be persisted) to one of the generated POJO classes.

How do I go about configuring various XML file(s) so that when I do a code generation
a. the additional fields are included in a generated POJO
b. subsequent code generations dont cause my additional fields to be overwritten?

For example, lets say I want to add an emailAddress field to my Employee POJO?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 9:22 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
You could use the class-code metadata on the *.hbm.xml file to do this.

For example:

Code:
<class name="Person">
    <meta attribute="class-code">
        private String emailAddress;
        public String getEmailAddress() {
            return this.emailAddress;
        }
        public void setEmailAddress(String emailAddress) {
            this.emailAddress = emailAddress;
        }
    </meta>
    <id name="id" type="long">
        <generator class="increment"/>
    </id>
    ...
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 5:07 pm 
Newbie

Joined: Sun Aug 06, 2006 12:55 am
Posts: 11
Thanks for that.

Is there a way this can be done so the 'class code' part is included every time i reverse engineer my POJOs from my schema?

I assume if I hand edit my hbm.xml file it will be overwritten next time I run the code generator.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 5:41 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Hmm...

I would create a custom template based on the hbm/persistentclass.hbm.ftl template. Right before the close tag you can insert Freemarker conditional logic based on the class name (of course, you can base the logic on any property you like, e.g. table name, schema, catalog, mutability, etc.).

Before:
Code:
<#foreach property in clazz.getUnjoinedPropertyIterator()>
<#include "${c2h.getTag(property)}.hbm.ftl"/>
</#foreach>

</${c2h.getTag(clazz)}>


After:
Code:
<#foreach property in clazz.getUnjoinedPropertyIterator()>
<#include "${c2h.getTag(property)}.hbm.ftl"/>
</#foreach>

<#if c2h.getClassName(clazz) == "org.mycompany.Person">
    <meta attribute="class-code">
        private String emailAddress;
        public String getEmailAddress() {
            return this.emailAddress;
        }
        public void setEmailAddress(String emailAddress) {
            this.emailAddress = emailAddress;
        }
    </meta>
</#if>

</${c2h.getTag(clazz)}>


It may seem a bit intense, but you will only have to do this once and then subsequent code generations will stay true to your requirements.


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.