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 hibernate: retrieving only part of child collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate: retrieving only part of child collection" Watch "hibernate: retrieving only part of child collection" New topic
Author

hibernate: retrieving only part of child collection

Carl Wauters
Greenhorn

Joined: Aug 02, 2007
Posts: 19
Hi everyone,

I'm quite new to Hibernate, and in case this question had been posted many times before, I'm sorry for that. I have the following situation:

Table A (Product)
-----------------
- Id (PK)
- related fields

Table B (Product description)
----------------------------
- Id (FK)
- Language
- Description

* There's a composite PK in table B (combination of Id and Language column)

Now, I would like to write a query that returns products - including the description - but only in the language that was passed as a parameter. I tried many things, but I always get a list with all languages, so the parameter has no influence on the result. I can understand why this is the case, but however, I would like to get only the descriptions I need. Is there a way to do this via Hibernate, and if this is possible, what do I have to do then?

I appreciate your help!

regards,

Carl


SCJP 5.0, SCWCD 1.4, SCBCD 5.0, SCJD, SCDJWS 5.0, SCEA/OCMJEA 5
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

What have you tried. You can write an HQL query that joins the two objects, then filters on the second object's language attribute.

Something like

"Select a from A a join fetch B b where b.language = :language"

then create the Query object, and before calling list(), set the "language" parameter using the setParameter() method of the Query object.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

I haven't tested it, but it might also fill the Collection with all Languages.

Mark
Carl Wauters
Greenhorn

Joined: Aug 02, 2007
Posts: 19
Hi Mark,

Thanks for your answer. The solution you proposed is one of the things I tried before, and this still returns a list with all descriptions (having the same foreign key value, of course), instead of a filtered list.

regards,

Carl
Cristian Vrabie
Ranch Hand

Joined: Jul 09, 2008
Posts: 71
The `where b.language = "language"` condition should work in Iibernate. It's one of the basic things in any query language. Maybe there's something in the mappings or code. Can you paste the ".hbm.xml" files and the part of the code where you do the query?
Carl Wauters
Greenhorn

Joined: Aug 02, 2007
Posts: 19
Hi Cristian,

Ik work with annotations coming from the persistence 1.0 API instead of the hibernate .hbm.xml file. So my problem could be caused to a wrong implementation of those annotations. The queries are written via Hibernate API.

I'm gonna show you the mapping code I used:

Entity Product
--------------


Entity Description
------------------

Query:
------



I changed the query a bit because it's a little bit more complicated.

when I loop through my returned product list, the descriptions map still contains all descriptions per productId. I would like to have only the descriptions in the specified language.

Regards,

Carl
[ July 09, 2008: Message edited by: Carl Wauters ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

What if you switch your query where the language is the first object in the from clause. I wonder if that would work.

like

Select a from B b fetch join A a Where ....

I am also wondering if this might be a case where a "filter" would be used, and you set the filter at runtime to the value you want. I haven't used filters.

I am just guessing here.

Mark
Carl Wauters
Greenhorn

Joined: Aug 02, 2007
Posts: 19
Hi Mark,

Do you mean this way: I'm afraid this causes mapping exceptions...
[ July 09, 2008: Message edited by: Carl Wauters ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate: retrieving only part of child collection
 
Similar Threads
Hibernate Mapping & POJO Inheritance & Composition
Hibernate mapping of many-to-many relationship with a joining table
Two PKs
multiple composit key
HTML form returns entity key -- what's best practice?