-->
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: preserving custom java code?
PostPosted: Fri Mar 17, 2006 7:57 pm 
Newbie

Joined: Fri Mar 17, 2006 7:45 pm
Posts: 3
According to James Elliott's Hibernate: A Developer's Notebook:
"If you want to use Hibernate's generated code as a starting point and then add some business logic or other features to the generated class, be aware that all your changes will be silently discarded the next time you run the code generator..."

How do you guys add business logic? Do you simply avoid automatic code generation after you begin making changes to a class?

For example, let's say I'm making a shopping cart, I'd like to add a helper function that returns the credit card number in the ************3222 format. I thought it'd be nice to add that as a function to the class that Hibernate creates.

Thanks in advance!!!
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 18, 2006 10:57 am 
Newbie

Joined: Sat Mar 18, 2006 1:13 am
Posts: 10
The trick is to use two classes. The generator creates the super class automatically, the subclass has the business logic.

Try setting up mapping file like this:
<hibernate-mapping package="my.package">
<class name="MyTable" table="TABLE">
<meta attribute="generated-class" inherit="false">my.package.MyTableBase</meta>
<MUNCH>

When you run hbm2java, it will generate a class called MyTableBase and embed all the mappings that you define.

What you then do is create a real class called MyTable in the "my.package" package and put your custom logic therein. MyTable has to extend MyTableBase

----------------------------
In this fashion, MyTable is a 'real' class and the biz-logic baked into it will stick around, but you can run hbm2java repeatedly (generating MyTableBase) without losing customizations.

Regards, and don't forget to credit-
-Jeff


Top
 Profile  
 
 Post subject: business logic
PostPosted: Mon Mar 20, 2006 7:07 pm 
Newbie

Joined: Fri Mar 17, 2006 12:41 am
Posts: 3
Here's an article that might help you with this:

http://www.onjava.com/pub/a/onjava/2005 ... tml?page=1

Check out page 6.

--DRR


Top
 Profile  
 
 Post subject: Thank you!
PostPosted: Mon Mar 20, 2006 8:02 pm 
Newbie

Joined: Fri Mar 17, 2006 7:45 pm
Posts: 3
Hi Jeff- Thank you VERY MUCH for helping me with that and providing your sample code. I kept trying to simply extend a base class on my own and then I was trying to typecast my objects to get Hibernate to save them, but Hibernate wasn't biting.

And thank you too, DRR, for sharing that link. That was very useful too!

This makes all the difference in the world!


Top
 Profile  
 
 Post subject: MappingException on base class code generation
PostPosted: Mon Oct 01, 2007 12:03 pm 
Newbie

Joined: Mon Oct 01, 2007 11:50 am
Posts: 4
Hi guys,

I've been trying to do the generated code as base class, custom code as extension pattern using

Code:
<class name="Owner"...>
<meta attribute="generated-class" inherit="false">BaseOwner</meta>
...


Here's the wrinkle -- my classes reference each other, so hbm2java wants to return an Owner object. This causes a MappingException because only BaseOwner exists. Of course, I WANT this function to return Owner (not BaseOwner) so that the returned object has my custom code.

Does anyone know a way around this? One solution I've considered is adding an empty union-subclass with generated-class = Owner, and extending code from there. But this seems like a kludge as it falsely implies a distinction in the schema, and because I will have to move my custom code to protect it on re-generation.

Thanks for ideas,
G


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.