This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate: retrieving only part of child collection

 
Carl Wauters
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tested it, but it might also fill the Collection with all Languages.

Mark
 
Carl Wauters
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

Do you mean this way: I'm afraid this causes mapping exceptions...
[ July 09, 2008: Message edited by: Carl Wauters ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic