-->
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.  [ 6 posts ] 
Author Message
 Post subject: Is it possible to add new methods to the generated classes?
PostPosted: Fri Mar 04, 2005 8:11 am 
Newbie

Joined: Thu Jun 17, 2004 4:16 pm
Posts: 18
Hi all,

I have created some mapping xml files and I also generated the java classes. Now I want to add some methods to some classes like: addChild(). However I am afraid that if I generate the code again the non generated methods will be deleted. Do you know how can I solve this problem?

Thank you in advance, Kostas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 8:58 am 
Newbie

Joined: Mon Feb 28, 2005 12:53 pm
Posts: 18
You can add other methods. The best approach will be to create a new class for the new methods and you will use it instead. If A is your java class generated by hibernate, create a new class B which contains an A object and any new methods. In this way if you modify A you are sure the new methods will not be deleted by regeneration.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 9:18 am 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
The big problem with that approach is that when you load a collection (e.g. cat.getKittens()), Hibernate will return the class defined in the mapping file, not the one you created with extra methods in it.

The Java generator tool you use probably uses templates (usually Velocity) which you could modify with your own logic. Or you could write your own simple code generator (there are libraries on the web to get you started).

A more advanced option is to use AOP to create mixin introductions. You then effectively mix methods like "addChild" into your mapped class.

HTH,

ben


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 9:39 am 
Newbie

Joined: Mon Feb 28, 2005 12:53 pm
Posts: 18
Ofcourse the class of type A will be loaded, but why do you say I cannot use B class? Whatever is your design : constructor defined, static methods ,.. you can call your other methods.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 9:05 am 
Beginner
Beginner

Joined: Sat Jan 22, 2005 9:11 am
Posts: 35
Location: London
dpmihai,

The problem is traversing associations (a.getB().getC() etc).

Imagine your mapping file contains classes A, B, C, etc where A has a collection of B i.e. class A has a method getBs() which returns a collection of B objects.

Let's say you can not modify the source code for A,B,C etc because they are generated by a tool. So you create classes A1, B1, C1 which extend (or contain) A,B,C with your "extra methods".

Now you obviously want A1 getB1s() method to return a collection of B1 objects not B objects but how do you do this?

The only way is to have to write wrappers for every accessor which is a very ugly design (even if you use code generators). You could do this:

Code:
public class A1 {
    private A a;

    public Set<B1> getB1s() {
        Set<B> hibSet = a.getBs();       
        Set<B1> newSet = new HashSet();
        for( B in hibSet ) { // yuk, forces load of whole set!
             newSet.add( new B1(B) );
        }
        return newSet;
    }
}


This is a very bad solution: it would be a performance disaster by doubling the number of objects on the heap and forcing the loading of entire associations.

Ben


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 9:36 am 
Newbie

Joined: Mon Feb 28, 2005 12:53 pm
Posts: 18
You are right. I was thinking not to create any wrapper methods.
But when I need to call some method which is not in the base class,
only then create a new object. I admit this will be a bit confusing and yes if you will call that method for all objects it will create the overhead you spoked.

I use Middlegen to create hibernate files only for the first time. Then if I need to add other properties I do it by hand. I did not use Hibernate java files with other methods than those generated (gets, sets, hashCode, toString, equals).
In the projects I worked, I had everytime some controller session bean classes and there I added all the methods I need.


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