-->
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: Returning a subclass as a superclass.
PostPosted: Thu Oct 05, 2006 7:09 am 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
Hi,

I'm posting this question on the general Hibernate forum to ensure best visibility.

I have a class A and a subclass B that extends A.

From our business model point of view, A is the "light" version of B, containing only some basic informations ; whereas B is a more complete definition of our concept. Note that both are valid entities on their own, and A and B instances can be saved without any business logic problem.

Fact is that sometimes, on "summary screen" use cases of our application, we need to display all A properties ONLY of all A AND B's instances.

And we don't want Hibernate to return B's specific properties but only A's properties of B's instances.

To express it in HQL, in a completely invented syntax : 'from B casted as A"

I didn't found the way to do this simply.

So all suggestions are highly welcomed ! :)

Thanks in advance,

sne.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 9:03 am 
Newbie

Joined: Fri Mar 03, 2006 8:52 am
Posts: 5
Hi,

this is really a newbie question! As defined in object-oriented programming you everytime have access to all bass-class properties, even if your object is of type B:

Best regards,
Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 11:01 am 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
The key phrase in my question is
"And we don't want Hibernate to return B's specific properties but only A's properties of B's instances"

The main aspect here (and I think I should have stressed it) is for 'performance purpose'.

A has 3 fields. B have 6 more (including long String and references to others entities).

Why should I fetch 6 (* number of instances returned) uninteresting fields for my use case from the db if I don't need them ?

Hence my question ! ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 06, 2006 8:54 am 
Newbie

Joined: Thu Sep 21, 2006 9:50 am
Posts: 5
I'm interisting on this topic too:

official reference (chapter 14: hql par:14.5 select) report 2 solutions:
you can use select clause to pick properties in you interest, so queries return multiple objects and/or properties as an array of type Object[].
Code:
select s.firstName, s.lastName from Student s


or for maintain typesafe you can use:
Code:
select new Student(s.firstName, s.lastName) from Student s

if there is an appropriate constructor in Student class

Code:
class Student
    {
     public Student(String, String){ ...}
    }

in this way you have a Student object(s)partially inizialized.

note that if your query changes at runtime and you get also birthdate of Student, you must have another constructor.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 06, 2006 12:40 pm 
Beginner
Beginner

Joined: Fri Sep 16, 2005 7:51 am
Posts: 25
Ha, well, I was expecting this answer...
I was temptated by the "typesafe" one, but didn't found it as elegant as that.

So, last call to the 'Hibernate Team' : what's your advice on that matter ? :)


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.