0

I have the following HQL query which works fine, however it returns a list of full FooD objects. I only need the ID of the FooD objects as I need to have faster query. Please not that in Hibernate mappings, FooD has a many-to-one relationship with FooB.

hqlQuery = "from FooD d left join fetch d.bill where d.ts < :ts"

I have then tried to get only the ID using the same kind of HQL query:

hqlQuery = "SELECT d.id from FooD d left join fetch d.bill where d.ts < :ts"

I got a "query specified join fetching, but the owner of the fetched association was not present in the select list".

I have then converted the query to regular Oracle SQL to get only FooD.ID:

sqlQuery = "SELECT d.id from FooD d LEFT OUTER JOIN FooB b on d.foodId=b.id where d.ts < :ts"

I have then mapped FooD and FooB objects like this:

sqlQuery.addEntity(FooD.class);
sqlQuery.addEntity(FooB.class);

and then get the resulting list by calling:

hSession.createSQLQuery(sql).setTimestamp("ts", ts).list();

But got the following error: "unexpected token: on near line 1".

Does someone know how to do get only the ID of FooD when doing a left outer join on FooB using Hibernate?

2
  • If you only need b.id, and have d.ts as the where clause, why do you need to initialize d.bill in the same query?
    – Eran Medan
    Nov 15, 2011 at 16:09
  • Because I only need d.id having bills (d.bill is FooB.id).
    – cvsoftware
    Nov 15, 2011 at 16:14

1 Answer 1

0

Update:

I didn't test it, but this should do the trick

SELECT d.id from FooD d inner join d.bill where d.ts < :ts

when you add LEFT you make it an outer join implicitly, and there is no need to initialize bill if all you need is to join by keys


Hibernate requires the object to be in the select clause to do any eager join fetches on it

But since you have no select or where clauses on d.bill, why do you need to fetch it anyway?

If all you need is the id, why not do this, there is no reason for the redundant join:

hqlQuery = "SELECT d.id from FooD d where d.ts < :ts" 
7
  • See this question as well: coderanch.com/t/217246/ORM/java/Hibernate-Eager-fetching
    – Eran Medan
    Nov 15, 2011 at 16:17
  • Because I need to get all FooD who have bills (d.bill is the foreign key linked to FooB.id).
    – cvsoftware
    Nov 15, 2011 at 16:20
  • Thanks for the link! However I don't understand how could I do my left join with it.
    – cvsoftware
    Nov 15, 2011 at 16:24
  • Check these HQL examples using the exists keyword: "from Cat cat where exists elements(cat.kittens)" but I think you must have the object in fully in the select clause as I mentioned (see docs.jboss.org/hibernate/core/3.3/reference/en/html/… for more samples)
    – Eran Medan
    Nov 15, 2011 at 16:30
  • Does it work if you try to remove the "fetch" keyword? you don't really want to initialize it, you just want to join (inner join)
    – Eran Medan
    Nov 15, 2011 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.