-->
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: Dumb Question - want description not code set in bean
PostPosted: Sat May 05, 2007 6:58 pm 
Newbie

Joined: Sat May 05, 2007 6:48 pm
Posts: 4
Hello,

This isn't really a problem, I just need to know how to do it. I have tried to search and tried a few things, unfortunately I think the biggest problem I am having is I don't know what I am really looking for to even try to do a good search, I assume this is a simple problem.

Situation:

Lets assume I have 2 tables, one called Article, the other called Category. Article contains content of a article and a category code article (i.e., 01=sports). It has a foreign key into the Category table, obviously.

SO, when I get a set of articles obviously it returns the category code in the bean, i.e., getCategory returns "01". How can I get it to return the string description from the Category table instead of the Category code stored in the Article table, i.e., getCategory returns "Sports"?

I put together a quick hack together to fix the immediate problem that just gets all the categories and then goes through each Article object and replaces it and I know there is no reason for me to do it this way because I know it can be down via hibernate.

Again, I am sure this is super simple, but I appreciate the direction.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 06, 2007 11:17 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
So in your Category table, you'd have two columns for example. categoryID and categoryName.

You'd have two hibernate mapping files, one for Article and one for Category.
The mapping file will generate two Java POJO, Article.java, and Category.java.

I assume you'd want a Many-One relationship from Article to Category (i.e. many articles can be in the same category).

In the Article mapping file you'd specify the foreign key relationship, for example:

Code:
<many-to-one name="category" class="Category" fetch="select">
            <column name="categoryid" />
</many-to-one>


What this says is that in the Article entity, you have a property called "category" which is actually a foreign key to the Category (using the categoryid column).

Now whenever you load an Article through hibernate, you can do Article.getCategory().getCategoryName(). As you can see, hibernate maps the Category object in.

Hope that hepls.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 12:48 pm 
Newbie

Joined: Sat May 05, 2007 6:48 pm
Posts: 4
That makes sense!

I appreciate the input.


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.