Reputation: 275
I'm quite newbie to Hibernate, so I'm still learning how to do things that should be easy.
I have the following simple relational model:
I have all Hibernate XML files already configured and the mappings from my DB done. All POJOs and XMLs are good because NetBeans' HQL editor runs without any problem all these three queries:
from Device
from Site
from Country
If I run the following code everything works good:
public void getAllDevices() throws Exception {
List<Device> devicesList = (List<Device>) session.createQuery("from Device").list();
int size = devicesList.size();
System.out.println("Size was " + size);
for(int i=0; i<size; i++){
System.out.println(devicesList.get(i).getDeviceOperationsName());
}
}
However, if I try to run a more complex query (not that complex...):
FROM Device As device
INNER JOIN device.site
INNER JOIN device.site.country
WHERE device.site.country.countryCode = 'ES'
NetBeans' HQL editor runs it without a problem, but when used in the above Java code (replacing "from Device"
) it throws the following error referring to the System.out.println
line:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to Entity.Device
at DAO.MasterDataDAO.getAllDevices(MasterDataDAO.java:41)
at CapaDominio.Main.main(Main.java:40)
It appears that Hibernate is trying to populate a list of Device POJOs, while the query is actually returning data from several tables. I expected Hibernate to populate not only Device but Country and Site tables as well, while I can manage all data from the returned List<Device>
and navigate from each Device object to their other corresponding objects.
Please, could anybody point to what I'm doing wrong or not understanding? How can I achieve the desired way of managing the objects (getting a Device list and iterating through them)?
Thanks!!
Upvotes: 1
Views: 1393
Reputation: 275
OK, I was finally able to solve the problem myself, after searching more about the issue and clearing some bad assumptions about how Hibernate works. The right code to run (already testd) should be:
public void getAllDevices() throws Exception {
List<Device> devicesList = (List<Device>) session.createQuery("SELECT device " +
"FROM Device As device " +
"INNER JOIN device.site " +
"INNER JOIN device.site.technology " +
"INNER JOIN device.site.country " +
"WHERE device.site.country.countryCode = 'ES'").list();
int size = devicesList.size();
System.out.println("Size was " + size);
for(int i=0; i<size; i++){
System.out.println(d.getDeviceOperationsName() + " in site " + d.getSite().getSiteName()
+ " with technology " + d.getSite().getTechnology().getTechnologyName()
+ " in country " + d.getSite().getCountry().getCountryName());
}
}
The main differences in this code regarding the initial one is:
SELECT device
. This indicates Hibernate that I only want to retrieve data from Device table. However, my query is joined with other tables, in order to be able to get those tables' data afterwords, by navigating from each Device object.System.out.println
includes some of the navigation to the other objects (tables) that I was mentioning.This works perfect and fulfills my expectations :) Hope it helps someone else.
Upvotes: 1
Reputation: 174
Its seems your PK_FK relation not specified in correctly Specify relation in correct way and use cascade. when you will get Device object all dependent object fetch with that.
Upvotes: 0