-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can I mapping two class to one table?
PostPosted: Thu Apr 22, 2004 9:53 pm 
Newbie

Joined: Thu Apr 22, 2004 9:50 pm
Posts: 1
My Hibernate version: 2.0.3

I have a problem when i designing my system.
I try to mapping two classes to one table. Both class don't know each other.
For example:
Class A{
long id;
int common_attr_1;
String common_attr_2;
String ext_a_attr_1;
}
Class B{
long id;
int common_attr_1;
String common_attr_2;
boolean ext_b_attr_1;
}
Above code ignored get/set methods.
The table contains all the fields of the two classes.
The mapping of them are:
<class name="A" table="T1">
<id column="id" length="22" name="id" type="long">
<generator class="native"/>
</id>
<property name="common_attr_1" type="integer"/>
<property name="common_attr_2" length="100" type="string"/>
<property name="ext_a_attr_1" length="100" type="string"/>
</class>
<class name="B" table="T1">
<id column="id" length="22" name="id" type="long">
<generator class="native"/>
</id>
<property name="common_attr_1" type="integer"/>
<property name="common_attr_2" length="100" type="string"/>
<property name="ext_b_attr_1" type="boolean"/>
</class>

above mapping file mayby has some minor mistake, but it should present my meaning.

My question is :
1. Is the practice (mapping two classes to one table) good practice of hibernate? Are there any pattern or best practice can guide me
how to design my system?
2. If hibernate can do this, How to resolve the problem that when i save class A, the ext_b_attr_1 is null.
When I load object as type B, it doesn't work because the ext_b_attr_1 is null.
3. If I add some relation to A or B, it make the problem more complex. When B has one relation to another class, Class A can't aware the
relation.
4. If above problem can be solved, are there any thing i should notice? Are there any thing i should not do?

Thank you for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 7:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This is not possible, refactor your code to use a common superclass.


Top
 Profile  
 
 Post subject: Mapping 2 classes to the same table
PostPosted: Wed May 26, 2004 5:13 pm 
Regular
Regular

Joined: Wed Jan 07, 2004 5:16 pm
Posts: 65
Location: CA, USA
Quote:
This is not possible, refactor your code to use a common superclass


In this case if a class hierarchy is mapped to the same table do you have to add an additional discriminator column to the table to indicate to Hibernate which subclass is to be instantiated when a row is retrieved? (as per the docs for maping class hierarchies)

Thanks, Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 26, 2004 5:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you use table-per-class-hierarchy, yes you have to use a discriminator column.


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