-->
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.  [ 6 posts ] 
Author Message
 Post subject: Query in a table per class hierarchy?
PostPosted: Tue Oct 25, 2005 3:05 pm 
Newbie

Joined: Tue Oct 25, 2005 2:34 pm
Posts: 12
I'm attempting to make a simple category system, consisting of category classes containing subcategory classes. I've mapped it as a table per class hierarchy. Where Category is the parent and SubCategory is the subclass. The problem for me comes when I get all the Category classes. It loads the SubCategory classes as well. I understand this is how Hibernate works and for other mappings this is just fine and what I want. But in this case I want to load only the main Category class. In my case I'm looking for the correct method to use. I'm ok with remapping or changing my object hierarchy completely to do it the correct way. I'm simply trying to make a two level category system for my program.

Here's a simple example class to illistrate what I'm doing:

Class Category{
id
name
Set SubCategory
}

Class SubCategory extends Category{
parent Category
Set object of other type
}

Then my web app displays them like this:

Category 1
------SubCategory
------SubCategory
------SubCategory
Category 2
------SubCategory
------SubCategory
Category 3
------SubCategory
------SubCategory
------SubCategory
------SubCategory

But what I'm getting is this:

Category 1
------SubCategory
------SubCategory
------SubCategory
Category 2
------SubCategory
------SubCategory
Category 3
------SubCategory
------SubCategory
------SubCategory
------SubCategory
SubCategory
SubCategory
SubCategory
SubCategory

Because it's loading all the Category classes and it's sub classes.

Simple looking for advice, thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 9:25 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
What sort of query are you using to get your categories? If you simply use HQL, saying "from Category", it should work fine, at least it always does for me. After all, you can assign the results to a List<Category> variable, which would lead to an exception if a subcategory was delivered.

When you say "that's how Hibernate works" -- do you mean eager fetching? If you specified eager fetching for this association (or as a default, or for your query), then the subcategories would be retrieved together with the categories. You can switch this on and off by setting lazy="false". But the output you show indicates to me that your problem isn't eager or lazy (which are issues of performance, not of query results) but your query delivered too many objects.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 10:51 am 
Newbie

Joined: Tue Oct 25, 2005 2:34 pm
Posts: 12
I'm using the following HQL "from com.collegevitae.vitae.Category as category". What I meant by that's how it works is the Hibernate will retrieve the superclass and subclasses when I query for Category, and SubCategory is a subclass to Category. I don't think the lazy has anything to do with it. What I'm having trouble with is what if you don't want to get the subclasses in your query results? What if you just want the superclass, how would you go about doing that? That's my question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 11:58 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 8:22 pm
Posts: 48
Perhaps this little expresion from the Polymorphoc queries section in the manual would help.

Code:
from Cat cat where cat.class = DomesticCat


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 3:06 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
Sorry, when writing "at least it always does for me", I had overlooked that SubCategory is a subclass of category.

The reason I overlooked it is that all my superclasses are abstract. I would set up my class hierarchy as something like
Code:
abstract class Category{
  id
  name
  set subCategory
}

class MainCategory extends Category{
}

class SubCategory extends Category{
  Category parent
  set object of other type
}


At first glance, this looks like two additional lines to write without any payback. But I bet quite soon you'll want to reference those category trees from somewhere in your application. Then it does make a difference whether you can only write

Code:
...
  Category c   // can point both to categories or subcategories
...


or

Code:
...
   MainCategory c   // here you can be sure it points to the root of the tree
...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 11:23 am 
Newbie

Joined: Tue Oct 25, 2005 2:34 pm
Posts: 12
Ya, that is what I suspected. I have another setup like that. Like you say it's probably a better method. I have another issue I'm having that you may be able to help me with. I'll start a new thread for it. It's just a mapping bi-directional issue.


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