Can I use the hibernation query language for objects that are not mapped to a table?

The following is the mySQL query that I use to retrieve HolidayPackages for a given Hotel :

 SELECT pkg.idHolidayPackage, pkg.name FROM holidaypackage pkg INNER JOIN holidaypackagehotel hph ON pkg.idHolidayPackage = hph.idHolidayPackage INNER JOIN hotelroom hr ON hr.idHotelRoom = hph.idHotelRoom WHERE hr.idHotel = 1; 

I have a POJO with mapping for:

  • Holidaypackage
  • Hotel
  • hotel room

I do not have a POJO for the HolidayPackageHotel .

Can I use the criteria API or HQL to execute an SQL query without creating a POJO for the HolidayPackageHotel?

For curiosity, the DB relationship: DB relations

+6
source share
2 answers

Not. You cannot use non-displayable objects inside HQL .

If you want to generate a list of beans from a query, you can use ResultSet transformers that can convert query results (arrays of objects) to beans . By doing this, you save the overhead of creating and populating POJO beans.

Read here for an example.

+2
source

yes, you can use non-displayable objects inside HQL.Below is an example of what I did in one of my projects.

 List list = session.createQuery("select p, a from UserAccount p, Channels a " + "where p.uid = a.uid").list(); Iterator iter = list.iterator(); while (iter.hasNext()) { Object[] objArray = (Object[]) iter.next(); UserAccount p = (UserAccount) objArray[0]; Channels a = (Channels) objArray[1]; System.out.println(p.getUsername()); System.out.println(a.getTitle()); } 
-3
source

Source: https://habr.com/ru/post/911172/


All Articles