This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes HQL - Look for a particular entity inside a Java object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "HQL - Look for a particular entity inside a Java object" Watch "HQL - Look for a particular entity inside a Java object" New topic
Author

HQL - Look for a particular entity inside a Java object

Nikolai Povaleyev
Greenhorn

Joined: Mar 06, 2013
Posts: 3
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.
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

You can use the identifier property in hql, instead of passing the object - usuario
What happens if this is used.


Thanks,
Tushar (SCJP 1.5)
Nikolai Povaleyev
Greenhorn

Joined: Mar 06, 2013
Posts: 3
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

Joined: Apr 04, 2006
Posts: 108


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

Joined: Mar 06, 2013
Posts: 3
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.
 
subject: HQL - Look for a particular entity inside a Java object
 
Similar Threads
What's wrong with the ModelDriven
How to join two tables on columns that have different names?
Stuck on manyToMany association
incomplete @JoinColumns
NullPointerException returned usig a StoredProcedureCall