Question

Using Northwind database I would like to make queries like

  • Number of orders from each Customer.Country
  • What is the sum of OrderDetails.UnitPrice for every day of week
  • Average time from Order.RequiredDate to Order.ShippedDate each year
  • ... more queries like these

I have to use hibernate to obtain the result, but I have strong feeling that it is pointless. Now I use createSQLQuery() because I can easily get result using SQL.

But how to do this using HQL? Could you please give me any example? I think using HQL isn't exactly the most resonable, but I would like to know how it can be done.

[Edit]

For example, can query:

select avg(DATEDIFF(ShippedDate, OrderDate)), year(ShippedDate) from orders group by year(ShippedDate)

be translated to HQL? or Should I get all Orders first and calculate result myself in my application?

or

select CompanyName, year(ShippedDate), sum(UnitPrice)/sum(Quantity) from shippers inner join orders on ShipperID = ShipVia inner join orderdetails on orderdetails.OrderID = orders.OrderID group by CompanyName, year(ShippedDate)

If I don't ask for set of object like Order or Shipper, can I use HQL?

I've seen a lot of tutorials, but they all refer to "objective oriented" queries. Could you please show me the way to translate to HQL these "unusual" queries?

Was it helpful?

Solution

How are these "unusual" queries? These are exactly the kinds of queries that show why RDBMS is still around. Unfortunately you do not show your mappings, so I will make a few assumptions... Looking at some of your bullets:

First, Number of orders from each Customer.Country. I will assume you have mapped Order->Customer as a @ManyToOne association

List results = session.createQuery( "select c.country, count(o.id) from Order o join o.customer c" ).list();
Object[] result = (Object[]) results.get( 0 );
print( "Country : " + result[0] );
print( "Number of Orders : " + result[1] );

or how about assuming a wrapper (OO!) for the results:

List results = session.createQuery( "select new CountryOrderCount( c.country, count(o.id) ) from Order o join o.customer c" ).list();
CountryOrderCount result = (CountryOrderCount) results.get( 0 );
print( "Country : " + result.getCountry() );
print( "Number of Orders : " + result.getOrderCount() );

What is the sum of OrderDetails.UnitPrice for every day of week. I will assume you have OrderDetails->Order mapped and that "day of the week" refers to Order.OrderDate:

List results = session.createQuery( "select dayOfWeek(o.orderDate), sum(od.unitPrice) from OrderDetails od join od.order o group by dayOfWeek(o.orderDate)" ).list();
...

One thing to note about the above is the use of a function. This might translate directly into a database function, but is mapped through function templates mapped with the Hibernate Dialect. For additional info, see http://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/dialect/function/package-summary.html or http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#portability-functions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top