-->
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.  [ 11 posts ] 
Author Message
 Post subject: Mapping two persistent classes to one table
PostPosted: Fri Sep 22, 2006 1:54 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
I've got a database table with many columns (30+) and if I mapped only one class to it i'd be retrieving too much data on each db call.

Are there any foreseeable problems with mapping 2 different persistent classes to the same database table?

Any other ways of overcoming this? Can you lazy load certain properties or components?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 2:21 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
Of course you can,

Two solutions:
1- Define a super class for two entities, and use table per class hierarchy strategy.
2- Define two classes, each with a discriminator value with attribute force="true"

---------------
don't forget credits.

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 2:36 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
Unfortunately, I can't alter the table structure because I'm working with a content management system that is quite restrictive.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 3:12 am 
Newbie

Joined: Tue May 10, 2005 2:32 pm
Posts: 14
Location: Quito, Ecuador South America
you may declare properties as lazy or proxy so they do not load when you call session.get(). If you are using hibernate3 when you call to session.load() no property is loaded from database unless you call a getter from a property different than id.

_________________
Hernan Leon
Mindsoft


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 3:29 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
are there any other solutions that do not involve byte code instrumentation?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 22, 2006 3:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
entity-name, look for it in the docs.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 23, 2006 10:01 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
there must be a way you distinguish between two entities, isn't it?

so use it as a discriminator column, or discriminator formula!

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 1:39 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
But entity-name is for one class to multiple tables right? I'm doing the opposite, multiple classes one table.

For example,
[code]
<class name="ClassOne" table="Information" >
<-- map columns 1-5 -->
</class>

<class name="ClassTwo" table="Information" >
<-- map columns 6-10 -->
</class>
[/code]

I'm just worried that this might cause some weird side effects. One thing i should mention is that the classes are immutable so i don't have to worry about inserts or updates.

As for discriminators, this is not exactly an inheritance model. Its more delegation like a one-to-one mapping or a class with components.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 2:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so multiple class'es to one table is not a problem *unless* the instances map to the same rows ...then you have to be carefull.

If they are completely disjunct no the only issue is if you are using versioning since they will "argue' against which entity should win the "edit right".

Are you sure you don't want to have component's here instead ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 3:33 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
So you're saying if i'm not doing any insertions or updates it should be fine right? Basically I am working with a Content management system so all i do is read and query.

I was thinking about using components but it requires byte code modification if i want to lazy load the components. I always thought that one of the strengths of hibernate is that it didn't require byte code modification so I'm not sure if i want to go down that path.

One more thing. Is it possible to retrieve the component itself without retrieving the main class that holds the component?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 3:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if it is pure read then I don't see any major problems with this approach.


and regarding getting the component then you should be able to do:

select p.compo from Parent

to just get the compo returned.

_________________
Max
Don't forget to rate


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