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.  [ 4 posts ] 
Author Message
 Post subject: [solved] Inheritance: how to query subclasses ?
PostPosted: Sat Dec 29, 2007 8:35 pm 
Newbie

Joined: Sat Dec 29, 2007 8:16 pm
Posts: 3
Hello,

I'm very new to Hibernate and am actually training with the help of the book
"Java Persistence with Hibernate".
I have a problem understanding a very trivial concept.
If i refer to page 202 of the book, about table per class hierarchy strategy, i should be able to query for CreditCar, by number, for example.
Let's say something like "from CreditCard where number=......', but i get a
Code:
org.hibernate.hql.ast.QuerySyntaxException: CreditCard is not mapped

Of course, i mapped it (using annotations).
On the other hand, if i query directly on BillingDetails, i get a
Code:
Could not resolve property: number...
Did i simply make a stupid typo in my annotations, or is there another way to query on subclasses ?
Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 30, 2007 7:13 pm 
Newbie

Joined: Sat Dec 29, 2007 8:16 pm
Posts: 3
Actually, this had nothing to do with inheritance, just a plain old pebcak instead !
I omitted to declare my subclasses in hibernate.cfg.xml...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 1:06 am 
Beginner
Beginner

Joined: Mon Feb 04, 2008 1:48 pm
Posts: 30
I am trying to do the same thing. Can you please elaborate on your solution and the comments you made about... "i mapped it (using annotations). " and "I omitted to declare my subclasses in hibernate.cfg.xml"

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 5:15 pm 
Newbie

Joined: Sat Dec 29, 2007 8:16 pm
Posts: 3
Ok, let's take a simple example :
You have a Person class, extended by Vip.
In Person, your annotations will look like (assuming you have a 'person_type' column to tell you what kind of person you deal with) :

Code:
@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "person_type", discriminatorType = DiscriminatorType.STRING)


In Vip :

Code:
@Entity
@DiscriminatorValue("VIP")


This means that every Vip record will have the value 'VIP' in its personType field.This works fine, but don't forget to declare not only Person but also Vip in your hibernate.cfg.xml file.


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