-->
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.  [ 5 posts ] 
Author Message
 Post subject: Confusion over Inheritance!
PostPosted: Sun Feb 14, 2010 8:08 pm 
Newbie

Joined: Tue Feb 02, 2010 8:44 pm
Posts: 6
Hi team,
I am new to Hibernate so please execuse my ignorance. I read so many articles about inheritance but couldn't find a clear answer to the following question.

For using SINGLE_TABLE inheritancer strategy I have to have a special field (DTYPE) in my table to store the class type, is this correct? This sounds like changing a DB design to serve an ORM requirements?

If this is so, say I have one table called MEMO and two classes; MemoHeader which is extended by another class MemoDetails which extends MemoHeader. All my Memo information are saved using instances of MemoDetails. I guess using the SINGLE_TABLE strategy will insert a value - say "details" - in the discriminator column, so:
Q 1- If I want to get a List<MemoHeader> object from my MEMO table will I be able to do that when all the records were saved using MemoDetails entity class, or Hibernate will understand how to get back those records typed "details" but wraps them up as MemoHeader objects?

Q 2- Will it still work the other way around? I mean, save MemoHeader as "header" and get it back as a List<MemoDetails>?

Thanks for any clarification.

Daniel,


Top
 Profile  
 
 Post subject: Re: Confusion over Inheritance!
PostPosted: Sun Feb 14, 2010 11:47 pm 
Newbie

Joined: Tue Jun 02, 2009 4:06 am
Posts: 16
Hi,

It is possible to do that.I have an Furniture class which is a super class and chair and desk as the subclass and i am using table-per-inheritance as shown below

<class name="Furniture" table="FURNITURE_DETAILS" discriminator-value="F">
<id name="id" column="FUR_ID">
<generator class="increment" />
</id>
<discriminator column="type" length="1" />
<property name="materialUsed" length="20"/>

<subclass name="Desk" discriminator-value="D">
<property name="surfaceArea" />
<property name="width" />
</subclass>

<subclass name="Chair" discriminator-value="C">
<property name="noOfLegs" />
<property name="noOfHands" />
</subclass>

</class>

Now i need only the list of the chairs from this table then i do the following

Session session = sessionFactory.openSession();

Transaction tx = session.beginTransaction();

Query query=session.createQuery("from Chair");

List<Chair> chairlist =query.list();
System.out.println("The List size is "+chairlist.size());
for(int i=0;i<chairlist.size();i++)
{
Chair chair =chairlist.get(i);
System.out.println("No of Legs of Chair is ####### "+chair.getNoOfLegs());
}

tx.commit();

session.close();

It works.

kartik


Top
 Profile  
 
 Post subject: Re: Confusion over Inheritance!
PostPosted: Mon Feb 15, 2010 7:10 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
Q1) will work fine.
In hibernate, If you query like Query query=session.createQuery("from java.lang.Object");

It will return all the objects that are persisted. You don't have to do anything specific to achieve this. Hibernate offers implicit polymorphism here.

Q2) second one won't work. Memo header is not Memo Details. But Memo details is a Memo header (the parent/child).
So when you query Memo Details [ i.e session.createQuery("from MemoDetails");], Hibernate will only look for MemoDetails[or its sub classes which you don't have any]


Top
 Profile  
 
 Post subject: Re: Confusion over Inheritance!
PostPosted: Mon Feb 15, 2010 11:17 pm 
Newbie

Joined: Tue Jun 02, 2009 4:06 am
Posts: 16
Hi,

The answer for your second Question.

I am sorry but in my case Char is a Furniture (parent/child)relationship and when i query for Chair("from Chair") i get the List<chair> only.

I hope this is what you are expecting.Retrieving collection of child elements.

Kartik


Top
 Profile  
 
 Post subject: Re: Confusion over Inheritance!
PostPosted: Mon Feb 15, 2010 11:42 pm 
Newbie

Joined: Tue Feb 02, 2010 8:44 pm
Posts: 6
Thanks to you both,

The thing is that Hibernate DOES require an additional field to be added to the database (as DTYPE) for the inheritance to work properly. If Hibernate is to be introduced into a legacy system then the DB schema needs to be changed to add a discriminator column.

I now understand the answer to Q2, thanks.

Daniel,


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