SQL can be combined with an object-oriented approach. What we end up with is Hibernate Query Language, or HQL. This article discusses how HQL works, and shows how to use it in the real world.
Hibernate: HQL in Depth - HQL in the Real World (Page 3 of 4 )
Until now I was doing all the setting up and configuration in the main(). This time I will be creating a class with a reusable and (almost) generic function. The setting up of the Hibernate will happen in the constructor. So let's begin.
public OrderOP(){ Configuration cfg = new Configuration() .addClass(Order.class);
sf = cfg.buildSessionFactory();
} : : }
The next function retrieves the data(Orders and Product) on the basis of the upper and lower priceTotals passed as arguments.
public class OrderOP {
SessionFactory sf;
public OrderOP(){ Configuration cfg = new Configuration() .addClass(Order.class);
sf = cfg.buildSessionFactory();
}
public Order getOrder(String lower, String upper){ // open session Session sess = sf.openSession(); String query = "select o from o " + "Order as o join o.products as p " + "where o.priceTotal > :priceTotalLower " + "and o.priceTotal < :priceTotalUpper";
And last but not least, the main function for testing:
public class OrderOP {
SessionFactory sf;
public OrderOP(){ Configuration cfg = new Configuration() .addClass(Order.class);
sf = cfg.buildSessionFactory();
}
public Order getOrder(String lower, String upper){ // open session Session sess = sf.openSession(); String query = "select o from o " + "Order as o join o.products as p " + "where o.priceTotal > :priceTotalLower " + "and o.priceTotal < :priceTotalUpper";