Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with a Hibernate Query

 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:

I have 2 tables (County, and AdjacentCounty). AdjacentCounty has a foriegn relationship to county.

I am trying to query for all adjacent counties by county ID

Here is my query:

select c from AdjacentCounty adj left join fetch adj.county c where adj.id.countyId = 1

I got the following error
"query specified join fetching, but ownere of fetched association was not present in the select list"

Any help is appreciated.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have c in your select portion, and that is not the table/object that is in the front of your FROM clause.

"select c from AdjacentCounty adj left join fetch adj.county c where adj.id.countyId = 1"

So you need to just move things around and also let Hibernate autogenerate your join.

"select adj from AdjacentCounty adj where adj.county.id=1"

Now in the last part I am making an assumption about your mapping, that you have a many to one mapping between AdjacentCount and County, and that County has a PK/id mapping to an attribute in County called "id"

Mark
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark:
Thanks for your reply.
Your assumption is correct about having a many to one mapping between AdjacentCount and County, and that County has a PK/id mapping to an attribute in County called "id"

I am trying to force the list of counties to be initialized in my query, so this HQL won't work

"select adj from AdjacentCounty adj where adj.county.id=1"

If I modify my original query to be:

select adj.county from AdjacentCounty adj left join fetch adj.county c where adj.id.countyId = 1

I got the same error.

Simply: I am trying to get an intialized list of all adjacent counties to the given county ID.

Thanks again
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Hibernate HQL you must have the select bring back the object type of the first object in your FROM clause, you can't have AdjacentCounties be the first object right after the FROM part. So you need to switch your join around.

So your FROM must be from County and not From AdjacentCounty.

Does that make sense?

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you don't want a list of AdjacentCounty objects, but a list of County Objects so I can switch your query around to


"select c from County c left join fetch c.adjacentCounty adj where c.id=1"

So that should give you a county where its id is 1, and it has a list of AdjacentCounty objects in it.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hanna, where did you go?

Mark
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Mark:

I get busy with other tasks, and I put this aside for now.
I will back to it tomorrow.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic