-->
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.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Invalid identifier on a Set of a joined subclass
PostPosted: Thu Sep 18, 2003 12:27 pm 
Newbie

Joined: Thu Sep 18, 2003 11:15 am
Posts: 6
I have a class (A) which has a set of a joined subclass (C). (C)'s parent is (B). (B) contains (A)'s primary key, but when I try to access the collection of (C) objects through (A), a select is generated which attempts to find (A)'s PK in (C) when it should find it in (B).

Let me make up a real-world example which might be easier to read:

Say a person owns a bunch of Vehicles. We have a Vehicle base class/table and Boat/Car joined-subclasses. The person's ID is located in the vehicle table, but we want to have a Set of Boats and a Set of Cars in the Person object.

Vehicle/Boat/Car all use VehicleID as their PK. Now, when I try to retrieve the collection of Boats or Cars, the generated select statement attempts a WHERE boat.personId = ?, but the personId is in the vehicle table, not the boat.

Any help would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 21, 2003 10:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Then, map the collection as a collection of Vehicles. Hibernate joins to the table of the class specified in the <one-to-many> element.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2003 10:08 am 
Newbie

Joined: Thu Sep 18, 2003 11:15 am
Posts: 6
That defeats the purpose since the returned objects would not have Car/Boat specific information.

It seems like this is a bug or missing functionality. If the FK is not specified directly in the subclass, why does it not find it in the parent?

As is, how can I associate a Person to a collection of Cars and a collection of Boats so that I can retrieve Car/Boat specific data?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 1:18 pm 
Newbie

Joined: Thu Sep 18, 2003 11:15 am
Posts: 6
is this a bug?
and is there another way to represent this?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 3:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No this is not a bug. You are doing a nonsensical mapping. Your object model does not reflect the relational model.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 10:12 am 
Newbie

Joined: Thu Sep 18, 2003 11:15 am
Posts: 6
The only way the object model could represent the relational model is if I had a Collection of vehicles which were the appropriate subclasses. But if I map to a collection of Vehicles, I lose all Car/Boat specific information, correct?

I don't follow why its nonsensical to have the common attributes of Cars/Boats in a Vehicle table. Why does a joined-subclass not know about its parent's FKs? Its not a true subclass if it doesn't have its parent's behavior.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 12:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Ummmm. Java collections are untyped. You lose this information anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 1:29 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
Yeah collections are untyped but what if I need to have relationship to cars only in my rich domain model ?. Of course I can delcare that releationhip in person as realtionship to Vehicles (base class) (it will give me all vehicles) and then I will need to filter them in the domain class.
This extra overhead because I need to materilize all vehicles.

Right now with hibernate it's impossible to specify relationship (In the Person) to the joined subclass (Car in this case) if Vehicle has reference to the Person because Hibernate assumes that that reference is in Car and it never checks base class (Vehicle) meta-information for that foreign key when generating load SQL for an association.

I believe that it will be nice feature for Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 1:32 pm 
Newbie

Joined: Thu Sep 18, 2003 11:15 am
Posts: 6
What? You don't lose any information by storing it in a collection. You have to downcast the reference when you retrieve the Object from the Collection. I want to downcast to Car/Boat, but Hibernate will only create Vehicle instances if I map a collection of Vehicles.

If what you said were true and you lost information by storing an object as an Object in Java's Collections, you would lose your Vehicle information as well, not just Car/Boat information. The entire collections framework would be useless.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 7:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
To trumppc: FILTERING is a different problem and is what the collection-level where attribute is for.

To brian: Hibernate implements polymorphic associations. Please refer to the Hibernate documentation. I am speaking of _static_ type information, not _runtime_ type information.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 3:54 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
To Gavin:

collection where attribue ? I believe in this case it's useless just think about that... Because we are using joined tables In case of hierarchy per table mapping srategy (for Vehicles) sure it will work now it's table per class mapping strategy. Root table for Vehicle, joinded table for Boat and joined table for Car.

Ok, in order to simplify understanding I will give you more informative description of the problem:


Person table: person_id, firstname, lastname, ....
Vehicle table: vehicle_id_pk, name, person_id_fk
Boat table: boat_id_fk (the same value as vehicle_id_pk), ....
Car table: car_id_fk (the same value as vehicle_id_pk), ..


Scenario 1:

I have collection of vehicles in the person class in this case everything works fine


<class name="Person" ....
<set name="vehicles" inverse="true">
<key column="person_id_fk"/>
<one-to-many="Vehicle"/>
</set>
<class>


Now imagine situation that by "some miracle" in my bussiness domain class Person ( in this example it does not make any sense to have it) I can have only boats so in this case there is 3 possible solutions:


1) Try to create 1-to-n association with Boat class it's most natural solution but in Hibernate it does not work right now

2) Try to filter in domain class "by hand"

3) Try to use "where" clause in collection mapping - this one does not make any sense because I each subclass maps to different table ....



Gavin I know that you are swamped by nonsensical questions all around this forum but in this case it's definetly missing feature in the Hibernate or bug ... Which makes me do hacks in my domain classes to support hibernate ...

Piece,

Giedrius


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 4:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Now imagine situation that by "some miracle" in my bussiness domain class Person ( in this example it does not make any sense to have it) I can have only boats


Then, in this case, use the following, clearly MUCH more correct relational model:

Person table: person_id, firstname, lastname, ....
Vehicle table: vehicle_id_pk, name, ...
Boat table: boat_id_fk (the same value as vehicle_id_pk), person_id_fk
....
Car table: car_id_fk (the same value as vehicle_id_pk), ...


Can you tell me any reason on earth why the above is not a MUCH better and MUCH clearer relational model for what you are doing??

I mean, sure, I certainly could implement support for your less preferred relational model and add some extra attribute that bloats out the mapping document format, but the reality is that Hibernate cannot possibly support arbitrary "bad" relational models. In all cases, we support the "best" way of modelling something.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 4:14 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
Imagine situation that I can not change data model. Hibernate advocates non-intrusive O/R mapping but in this case I need to change data model ?

But all my subclasses have reference to the person why the HELL I need to have reference and mapping to the person in all subclasses for Vehicle ????

And of course it's beeter releational-model because there is no information dublication (more normalized model) each joined table has only additional information.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 4:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
But all my subclasses have reference to the person


EXACTLY!

I just knew it.


So really, Person has a collection of Vehicles. C'mon, there is no other possible conclusion. So, a far, far more elegant object model is to actually use polymorphism and model it as a collection of Vehicles.

It is really just bad object modelling practice to implement this is Person having a collection of cars and then a collection of boats and then a collection of buses, and then a collection of spaceships ...


Oh? Don't have spaceships yet? Well, tomorrow you _might_! And only one of these approaches can handle the addition of new subclasses!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 4:24 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
And btw you don't need to add any extra attributes in the hibernate mapping it's just how load SQL is generated for loading joined subclasses.
Right now whe generating SQL hibernat never looks for keys info in the base class


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

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.