-->
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: add arbitrary field/method to POJO?
PostPosted: Tue Nov 08, 2005 5:54 am 
Newbie

Joined: Tue Oct 18, 2005 8:43 am
Posts: 15
hello,
I have three tables; A, B and C. There are one-to-many relations between A and B and between B and C. When generating my POJOs using Middlegen and hbm2java, I get all the POJOs I could wish for, except one thing: in my generated class C, I would like to have an additional field pwrLimit with a getter/setter. This additional field is not directly mappable to the database, but is derived from other values in other tables.

My classes A, B and C are the bases for generating a report. A user presses a "generate"-button, and a report containing all data is presented. Class A iterates over B, which iterates over C.

So my question is: how should I go about to be able to use my class C with the extra field pwrLimit? Should I build a CVao which contains all fields in C together with my new field pwrLimit? This would then mean that I would have to use a BVao which have a Set of CVaos. And AVao which have a Set of BVaos. Seems like a bit of overhead just to get an additional field in my class. I guess I could edit my mapping-files directly, but as these are generated by Middlegen, I do not want to do that. And in Middlegen, I can not find any way to add arbitrary fields. It is, however, possible to force the class to implemenet an interface, so this could maybe be a solution?

Any ideas would be highly appreciated!

cheers,
pj


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 11:16 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
for simplicity, I'd recommend keeping your middlegen xml and pojo files the same so any db changes only result in you having to run middlegen over.

Then, in your code, have the xml file generate instead of Class C, generate Class BaseC. Then in your code, write Class C to extend Class BaseC and in Class C have the getters/setters for that extra attribute and create the value programatically.

This way any DB changes get automatically reflected in Class C if you run middlegen again because it extends Class BaseC.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 11:21 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
using some code like the following, you would still call everything by C for hql queries.

Code:
<class
    name="package.then.class.C"
    table="c"
>
    <meta attribute="generated-class" inherit="false">package.then.class.BaseC</meta>


_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 6:12 am 
Newbie

Joined: Tue Oct 18, 2005 8:43 am
Posts: 15
thanks for the answer. I ended up doing it the other way around. I generate class C and tells Middlegen that this should extend class BaseC. Class BaseC then has the additional getters/setters. BaseC is declared abstract, with one abstract method:
Code:
public abstract C getC();


Which unfortunately means that C needs to implement this method. Seems like an anti-pattern to me, where superclass knows of it's subclass, but was the only way I could get it to work. The reason for this is that I use Struts to iterate over my B-objects which iterates over my C-objects. And when iterating over my C-objects, I use the getters/setters in BaseC. If BaseC were to extend C instead, iterating over C would not give me access to the getters/setters in BaseC

cheers,
pj[/code]


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.