-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate + Middlegen + generating code
PostPosted: Thu Dec 15, 2005 3:09 pm 
Newbie

Joined: Thu Dec 15, 2005 3:03 pm
Posts: 5
I use Hibernate and Middlegen for working with Oralce database. Middlegen generated java classes successfully. But I want to add some new methods and attributes to it. Can I specified that modification and apply it every time when I regenerate classes?
For example: it generates class A:

Code:
public class A {
  private BigDecimal xmlFile;
  public A() {};
  // ...
}


and i want to generate class as

Code:
public class A {
  private BigDecimal xmlFile;
  // new property that is not mapped on the database.
  int new_prop;
  public A() {
    // read data from file and specify new_prop.
    new_prop = 2;
  }
  // ....
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 3:39 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
what you can do is the following. Have middlegen generate a base class and then write another class extending that base class and adding that extra bit of business logic. This way, if the db changes, you run middlegen and the whole class changes w/o having to write anything.


Code:
<class
    name="package.ClassA"
    table="table_a"
   
>
    <meta attribute="generated-class" inherit="false">package.BaseClassA</meta>



this will generate a POJO named BaseClassA. Then, you write another class named ClassA which extends BaseClassA and adds that little bit of business logic you need. By declaring it this way you still call this in hibernate/hql using the orig. class name of ClassA.

_________________
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: Thu Dec 15, 2005 3:58 pm 
Newbie

Joined: Thu Dec 15, 2005 3:03 pm
Posts: 5
It seems great. Thanks. I'll test it now.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2005 8:30 am 
Newbie

Joined: Thu Dec 15, 2005 3:03 pm
Posts: 5
I have added "meta" tag to the BaseClassA.hbm.xml.

But when i write in a code:

Code:
List list = session.createQuery("from table_a").list();
ClassA cl = (ClassA) list.get(0);


I have a java.lang.ClassCastException:
And
Code:
BaseClassA cl = (BaseClassA) list.get(0);
works correctly


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2005 10:16 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
Are you sure you declared it correctly? I think it'd be better if the mapping file was named ClassA.hbm.xml instead of BaseClassA.hbm.xml.

also, HQL runs off class names.

change that query to "from ClassA"

_________________
Chris

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


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