-->
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.  [ 3 posts ] 
Author Message
 Post subject: Best approach for multiple entities mapped on the same table
PostPosted: Mon Nov 21, 2016 4:32 am 
Newbie

Joined: Mon Nov 21, 2016 4:12 am
Posts: 2
Hi everyone,

we develop an enterprise HR web application with backend in Java + Hibernate 3 and I have a doubt for a potential misuse we made handling inheritance and bean mapping.

To map our beans (we use hbm mapping) we frequently create "light" and "heavy" versions of the objects mapped by the same table, lets say for example we have an Employee bean to map the basic "primitive" properties and a "HeavyEmployee" bean to map the very same table but with the addition of many-to-ones and sets.
In this way we can better manage the queries so to avoid loading every object for a query that needs only the basic properties (PS: I don't know why lazy loading wasn't used instead, i was told me that there was some sort of problem but it's not clear to me).

The fact is that from the java side HeavyEmployee was made a subclass of Employee, and other people told me that this is in fact wrong since an "HeavyEmployee" isn't in a "is a " relationship with Employee.

I would like any advice on the subject and how to better handle this cases in future.

Maybe subclassing was an error and we should have used composition instead? Something like an Employee property in the HeavyEmployee bean?
And in this case since the table is the same, how we should have managed the mapping of the heavy bean?
With a formula in the property? Or a many-to-one?
Wouldn't that be worse for the performances (added self join)?

Sorry for the many questions, I just trying to question myself and improve a bit.

thanks


Top
 Profile  
 
 Post subject: Re: Best approach to manage light and "heavy" beans
PostPosted: Mon Nov 21, 2016 8:01 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Inheritance for reusing data structures is not a good idea. Inheritance is meant for providing different behavior across multiple implementations of the same base class.

Both inheritance and composition will not buy you much in this context. Composition is meant for embeddable types only, but, here, you have multiple subentities instead, which can even be managed concomitantly by a given Persistence Context.

Do you really fear that duplicating some properties is going to cause you much trouble? If you have unit tests and integration tests, then you should not worry about changing one mapping without changing the other one.

Sometimes, adding yet-another abstraction will cause more harm than good. Remember that multiple tables have an identifier column. Does it make sense to create a base class to hold that identifier? I doubt so.

As I explained in this article, I'd rather duplicate the property names than introducing another abstraction which might heart maintainability or performance.


Top
 Profile  
 
 Post subject: Re: Best approach to manage light and "heavy" beans
PostPosted: Tue Nov 22, 2016 10:19 am 
Newbie

Joined: Mon Nov 21, 2016 4:12 am
Posts: 2
vlad wrote:
Inheritance for reusing data structures is not a good idea. Inheritance is meant for providing different behavior across multiple implementations of the same base class.

Both inheritance and composition will not buy you much in this context. Composition is meant for embeddable types only, but, here, you have multiple subentities instead, which can even be managed concomitantly by a given Persistence Context.

Do you really fear that duplicating some properties is going to cause you much trouble? If you have unit tests and integration tests, then you should not worry about changing one mapping without changing the other one.

Sometimes, adding yet-another abstraction will cause more harm than good. Remember that multiple tables have an identifier column. Does it make sense to create a base class to hold that identifier? I doubt so.

As I explained in this article, I'd rather duplicate the property names than introducing another abstraction which might heart maintainability or performance.


Thanks Vlad for your answer!
It was an informative read also your link, I really have a lot to learn regarding Hibernate and Java


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