-->
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: newbie, pls help with ddl
PostPosted: Fri Jan 14, 2005 4:44 pm 
Newbie

Joined: Thu Dec 16, 2004 2:19 pm
Posts: 12
The answer is probably somewhere in this forum but I can't formulate the correct search terms.

Code:
abstract class A {}

class Ax extends A{}
class Ay extends A{}

class B {
   A anA;
   void setA(A a) { anA = a; }
}

I am using table per subclass.

Code:
A a = new Ax();
session.save(a);
B b = new B();
b.setA(ax);
session.save(b);

... later...

Code:
B b = Bdao.findById(foo);  // assuming foo is the id of b above
A a = b.getA();  ***


*** questions:

1. the actual class of "a" would be Ax ?

2. what the DDL for class B should look like ?

Would this suffice ?

table B [
anA long foreign key to table A
]

3. how does hibernate knows to create an Ax (instead of an Ay) if class B DDL only contains a foreign key id to table A ?

Thanx.

-cs


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 5:26 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
It's my understanding that Hibernate locates the proper subclass by querying the subclass tables to find the correct row. At any rate, the actual class of A would be Ax, just like you'd expect. It's not voodoo or anything, Hibernate just does the extra work of locating the class in the correct table.


Top
 Profile  
 
 Post subject: Re: newbie, pls help with ddl
PostPosted: Fri Jan 14, 2005 10:37 pm 
Newbie

Joined: Tue Sep 14, 2004 5:57 pm
Posts: 6
Location: Sunnyvale
chacs wrote:


1. the actual class of "a" would be Ax ?

Yes
chacs wrote:

2. what the DDL for class B should look like ?

Would this suffice ?

table B [
anA long foreign key to table A
]

Yes
chacs wrote:

3. how does hibernate knows to create an Ax (instead of an Ay) if class B DDL only contains a foreign key id to table A ?

Hibernate throws a big case statement and join to the subclass tables when it queries for A. So if it sees an entry in Ax with the same PK as anA then it will get any other mapped properties from Ax and create an instance of Ax with all the right properties.

Turn on "show_sql" and step through in a debugger to see the SQL and to see the proper subclass get created.


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.