• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HQL - Look for a particular entity inside a Java object

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I'm trying to do a Java method able to retrieve a list of orders (Pedido class) that contain a certain entity called Usuario. I have the following entities in Java:






I have the following query that provides valid results when used on Hibernate Tools in eclipse:




And this is the method that I tried to code in Java:




Giving the following exception:




I think that the query is fine, but I'm not coding correctly it in the Java method. I've looked for examples but I haven't found any showing how to look for an entity inside another entity, just particular values. I would prefer looking for the object instead of looking for a match in the idUsuario property as in the Hibernate Tools example query, but any advise about what I'm doing wrong would be fine.

Thanks in advance for your support.
 
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the identifier property in hql, instead of passing the object - usuario
What happens if this is used.
 
Nikolai Povaleyev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick response, T Mishra.

To be honest I am newbie in Hibernate and I thought that passing an object instead of an ID would be more 'best practices' but I am not really sure.
I had already tried the approach you are suggesting and I didn't succeed either so I rolled back to the one I thought it was better.

This is the line with the query with your suggestion on it:



And the exception:




The variable where I am storing the Pedido objects coming from the method is also a List<Pedido> type, so I don't understand why the cast is failing. In theory, there would be only one item on the list (according to the test I'm doing) but I don't think the method is returning a single object instead of a list.

It's the first time I create methods using parameters (the ? and the setter method) so I am not sure if my approach is fine. It should be according to the examples I've seen but any suggestion about this would be very appreciated.

Thanks!
 
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This error occurred due to incorrect sql.(as the name suggests)

This error suggests that the query was excuted. It returned the data. But the expected data was not of type Pedido.

The variable where I am storing the Pedido objects coming from the method is also a List<Pedido> type, so I don't understand why the cast is failing. In theory, there would be only one item on the list (according to the test I'm doing) but I don't think the method is returning a single object instead of a list.


According to the API docs, Query.list(),

Return the query results as a List. If the query contains multiple results pre row, the results are returned in an instance of Object[].

.
So, the data i.e the single row retrieved is not of type Pedido. List<Pedido> expects that each row retrieved can be mapped to a type Pedido. But the data retrieved contains multiple result .ie. its of type List<Object[]>.

Next Steps
1. Do not map the results of Query.list() directly to Pedido.
2. Hint - iterate over the list to find Pedido
 
Nikolai Povaleyev
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, I was really mistaken about how I thought those queries worked.
I didn't know that the query was delivering also Usuario objects (I guess because of the inner join).

I finally solved the issue doing a Select of the object I was interested in:




Although I finally didn't implement the suggestion you gave me I have learnt a lot about how this works, so thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic