-->
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.  [ 8 posts ] 
Author Message
 Post subject: subclass problem
PostPosted: Tue Jan 06, 2004 8:30 am 
Newbie

Joined: Tue Jan 06, 2004 8:06 am
Posts: 13
Hi all

I am new to this hibernate, pls bear with me if question of similar nature has been answer b4.

Supposing I have a BRAND table with following attributes

--------------------
CODE : Non unique 3 letter code
TYPE : e.g. drink, car
NAME : Pepsi, Coke, Toyota, Nissan

composite key (CODE, TYPE)
--------------------


I am further defining a separate Car and Drink hibernate object (which will be having separate tables). Each Car or Drink object contains a CODE field that correspond to the entry in BRAND. The task here is to look up the brand name when I query the Car or Drink object, like car.getBrand().getName().

----------------------------------------------------

(a) I am tempted to use subclass by

i. create Brand.java
ii. create CarBrand.java (extends Brand.java)
iii. define in Brand.hbm.xml:
<discriminator-column="type">
<subclass name="CarBrand" discriminator-value="car"/>
iv. Create Car.java
v. define in Car.hbm.xml:
<many-to-one name="carBrand" column="carBrand" class="brand.car"/>

But It seems I can't use subclass bcos:
- Actual Car does not extend Brand
- Car and Brand do not share the same table
Is this understanding correct?

Anyway, I have tried the above and system returns me: ObjectNotFoundException


----------------------------------------------------

(b) That leave me the option of using joined-class. However, I lost the ability to use discriminator-value. And I am kinda stuck here.

Is this even a subclass problem, or I can better resolve it using different approach? Any suggestion will be much appreciated.

Cheers
-WK-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 12:23 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
AFAIK, brand and cars are not hierarchically linked, so don't inherit car from brand.

Your constraints are pretty confuse, so it's hard to help

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 12:23 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
AFAIK, brand and cars are not hierarchically linked, so don't inherit car from brand.

Your constraints are pretty confuse, so it's hard to help

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 9:47 pm 
Newbie

Joined: Tue Jan 06, 2004 8:06 am
Posts: 13
Its an inherited problem from old design that I cant change much. Anyway, let see if I can clear things up a little. Here's a simplified object's relationship.

-------------------------------
CAR table
-------------------------------
ID : PK
COLOR : direct attributes (blue, red)
CODE : Indirect attributes to BRAND.CODE for car's brand name


-------------------------------
BRAND table
-------------------------------
CODE : Non unique 3 letter code
TYPE : e.g. drink, car
NAME : Pepsi, Coke, Toyota, Nissan

composite key (CODE, TYPE)

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

On the back end, when I query the car brand (in Plain old SQL) I will specifically indicate TYPE = car:

SELECT car.*, brand.name
FROM car, brand
WHERE car.code=brand.code AND
brand.type='car'
AND car.ID='3'

I am porting the application over to hibernate and here's the original intention.

----------
Brand.java
----------
private String code;
private String type;
private String name;
...

----------
Car.java
----------
private String id;
private String color;
private Brand brand;
...

And by way of subclassing, I want to retrieve car's brand via:
car.getBrand().getName().

However as I read further, given Car and Brand are 2 different entity altogether, I can't use subclass and that probably leave me the option of joined-subclass, which I am still reading up at the moment.

Cheers
-WK-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 3:52 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you're mismatching asociation and subclassing.
car.getBrand() means a many-to-one relationship between those entities.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 4:56 am 
Newbie

Joined: Tue Jan 06, 2004 8:06 am
Posts: 13
Thanks for the reply. Yes its more of association than subclass problem but the problem of using assiciation is that I cannot further specify a restricting TYPE value:

<class name="car" table="CAR">
...
<many-to-one name="code" column="code" class="BRAND">
(In order to lookup name in BRAND, I need to further specify BRAND.type='Car' but there is no facility to do that in hbm)
...
</class>


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

I have thought of following by subclass CarBrand from Brand (thus restricting the value to code='car'). It seems logical but I cant get it to run. Can you comment if its workable, thx.

BRAND, CAR table definition same as above.

----------
Brand.java
----------
private String code;
private String type;
private String name;
...

----------
CarBrand.java extends Brand.java
----------
{}


----------
Car.java
----------
private String id;
private String color;
private CarBrand carBrand;
...

----------
hbm.xml
----------
<class name="Brand" table="Brand">
<id name="id" column="id" type="string">
<generator class="uuid.hex"/>
</id>
<discriminator column="code" type="string"/>
<property name="name" column="name" type="string"/>
<subclass name="CarBrand" discriminator-value="Car" />
</class>

<class name="Car" table="Car">
<id name="id" column="id" type="string">
<generator class="uuid.hex"/>
</id>
<property name="color" column="color" type="string"/>
<many-to-one name="carBrand" column="code" class="CarBrand" />
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 5:37 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Define Brand PK as a composite id code + type and do
Code:
<many-to-one>
  <column name="code"/>
  <column name="type"/>
</many-to-one>


Have a look at <any>, migth help.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 1:54 am 
Newbie

Joined: Tue Jan 06, 2004 8:06 am
Posts: 13
My last design was actually legit.

1. define BaseBrand
2. extend CarBrand on BaseBrand
3. subclass carbrand using TYPE as discriminator-column and "car" as discriminator-value
4. define in car.hbm.xml, many-to-one relationship to CarBrand

I made a mistake defining in Brand id=id. Using id=code solved the problem.

Thx for e help.

Cheers
-WK-


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