-->
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: Hibernate mapping - Get single item of a one-to-many mapping
PostPosted: Mon Feb 06, 2006 9:09 am 
Newbie

Joined: Mon Feb 06, 2006 8:53 am
Posts: 7
Location: Antwerp Belgium
Hi

I have two tables Project And ProjectAssignment, a one-to-many mapping from Project to ProjectAssignment
Project
- ID
- Title
- StartDate
- EndDate

ProjectAssignment
- ID
- ProjectID
- IsProjectManager

I have to display all project with their project manager (there can be only one projectmanager for each project). The question is, can I map such a thing.

So If I load a project I would like to get all ProjectAssignments in a list, with the manager as wel (no problem here) and I want the projectManager of the project (ProjectAssignment where IsProjectManager = true) so I can ask to the project object
Project.ProjectManager
Is there any way to achieve this last option with hibernate ?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 2:28 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We need to do this as well. From what I understand, it can't be done through the mapping (I expected to be able to define a one-to-one property with a "where" attribute, but that attribute is not supported for one-to-one mappings). We haven't tried it yet, but I think you'll need to create an unmapped property in your class that simply loops through the mapped collection property to find the correct one. Unforuntately, this defeats lazy-loading on the collection ...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 2:40 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Of course, you can still get the project manager explictly through a query:

Code:
SELECT Project.Title, ProjectAssignment.Whatever
FROM Project AS Project
LEFT JOIN Project.Assignments AS ProjectAssignment
WHERE ProjectAssignment.IsProjectManager = true


This is undesirable, though, if you'll have many queries where you want to refer to the project manager. It would be much cleaner if it could be done through the mapping so you could do

Code:
SELECT Project.Title, ProjectManager.Whatever
FROM Project AS Project
LEFT JOIN Project.Manager AS ProjectManager


Almost all of our collection items have a property called IsPrimary, and there will be one and only one item where it's set to true. Most of our queries will want to select the primary collection item, so we'd like to be able to define them as one-to-ones in the mapping so all our queries are cleaner.


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.