-->
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.  [ 8 posts ] 
Author Message
 Post subject: How do I map to a column in another table
PostPosted: Sun Oct 05, 2003 9:13 am 
Beginner
Beginner

Joined: Sun Oct 05, 2003 9:07 am
Posts: 47
This seems like it should be simple, so it's quite possible I just "don't get it".

I have a Project object that has a statusId column. This works for most of my CRUD situations. However, when searching and displaying results, I wan't to show the text representation of statusId, which I have as a statusDesc getter/setter.

It seems like I should be able to say "Hibernate, get me the statusDesc column's value from the project_status table". What's the best way to go about doing this?

2ndly, is it possible to map this with XDoclet?


Top
 Profile  
 
 Post subject: Map it
PostPosted: Sun Oct 05, 2003 6:06 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
In some of my scenarios like this I personally just threw it onto my object and mapped it as a many-to-one when it wasn't internationalized and when it was internationalized I mapped it as many-to-many or one-to-many as appropriate.

In the case of rendering a view with many results, you could just load all of the statuses independently and pick the right one for each record you are showing as you render it.

e.g. 1
Code:
<many-to-one name="statusDesc" class="com.something.StatusDescription" insert="false" update="false" cascade="none" column="statusId" outer-join="true"/>


e.g. 2
Code:
List allStatusDescriptions = Session.get(StatusDescription.class);
// ...
String showMe = allStatusDescriptions.get(desiredIndex);
// ...


Or something like that.

As for X-Doclet, I don't use it but this seems pretty simple so I don't see why it wouldn't be possible.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 05, 2003 6:36 pm 
Newbie

Joined: Sun Oct 05, 2003 6:23 pm
Posts: 3
try this out. (i'm assuming statusid is an int).

use a PersistentEnum for the statusid, map it to a class called Status.

when you start up your app, in the initialization, create a List of all the Status's with the data from your product_status table.

in the fromInt method, return one of the Status's from the List you created.

then in your Project object, have it return Status objects instead of ints. now you can have multiple getters in the Status object to get either the id or the text description.

you can also override toString(), so in your JSP or whatever, it'll print out the text description instead of the id.

hope this helps.

--alex


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 05, 2003 10:20 pm 
Beginner
Beginner

Joined: Sun Oct 05, 2003 9:07 am
Posts: 47
I don't think either of these solutions will help in my situation. I want to use the Criteria/Expression API to search by a status's text description when a user wants to search on a status field. So far, the only way I think I can do it is to select all records in a table (and do an outer join with the status table) and then do a match, but that's a pain when the Expression API is so simple.


Top
 Profile  
 
 Post subject: Using the Criteria API...
PostPosted: Mon Oct 06, 2003 12:45 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
You can use the the Criteria API to search based on the description if you mapped it as the first way I suggested.
Quote:
<many-to-one name="statusDesc" class="com.something.StatusDescription" insert="false" update="false" cascade="none" column="statusId" outer-join="true"/>

In your criteria you can then do the following:
Code:
Criteria criteria = Session.createCriteria(YourClass.class);
criteria = criteria.createCriteria("statusDesc").add(Expression.eq("description", whateverTheUserTypedToSearchBy));
List results = criteria.list();


This will give you back all of the instances of YourClass that have a status description where the description String == what your users are searching for.

I know this works because I've done it in my project.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 12:58 pm 
Beginner
Beginner

Joined: Sun Oct 05, 2003 9:07 am
Posts: 47
Awesome David - thanks for this info. I'll try this when I get home tonight.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2003 12:15 pm 
Beginner
Beginner

Joined: Sun Oct 05, 2003 9:07 am
Posts: 47
David - you solution worked, I did have to change to using a "ProjectStatus" object which represents the project_status table. Then I added a simple getter for getStatusDesc() that returns projectStatus.getStatusDesc(). It's too bad I can't just set the statusDesc field as a String, but I can live with this solution. Here's the XDoclet markup for it.

/**
* @return String
* @hibernate.many-to-one class="com.centres.redd.persistence.ProjectStatus"
* insert="false" update="false" cascade="none" column="status_id"
* outer-join="true"
*/
public ProjectStatus getProjectStatus() {
return projectStatus;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 10, 2003 1:06 pm 
Beginner
Beginner

Joined: Sun Oct 05, 2003 9:07 am
Posts: 47
David - the following line doesn't even compile - did you have something else in mind:
Code:
criteria = criteria.createCriteria("statusDesc").add(Expression.eq("description", whateverTheUserTypedToSearchBy));
[/code]


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