File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Getting a single entity from a query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Getting a single entity from a query" Watch "Getting a single entity from a query" New topic
Author

Getting a single entity from a query

Jay Abrahm
Ranch Hand

Joined: May 28, 2008
Posts: 183
I have the following entity with a One to Many child object

I would like to get the parent record with one child record from this @OneToMany mapping so I have used the following query


This returns a list of objects and each position has two objects - an object of yearDepartment & departmentMonths.

Since I require a list of yearDepartments I iterate through this list with two objects and make it a list of yearDepartment with a single departmentMonths embedded inside.

Is there a way to get yearDepartment with just one departmentMonths inside the parent entity using the query/criterion/formula or any other means rather than manually processing it ?
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
See if "select yearDepartment from YearDepartment yearDepartment join ...." helps.

This should return a List of YearDepartment objects..
Jay Abrahm
Ranch Hand

Joined: May 28, 2008
Posts: 183
Thats doesn't help since the list of YearDepartment still has a list of departmentMonths. In fact now I cannot do the following processing which I wish to avoid....

What I need is a list of YearDepartment with a single departmentMonths. This is why I had used the query.



Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
Doesn't the "WHERE m.id=?" part of the query do this for you?
Isn't m.id unique here?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting a single entity from a query