Web hosting reviews - CHAPTER 10 n ADVANCED QUERIES USING CRITERIA 223

CHAPTER 10 n ADVANCED QUERIES USING CRITERIA 223 n ADVANCED QUERIES USING CRITERIA 223 Criteria prdCrit = session.createCriteria(Product.class); Product product = new Product(); product.setName(”M%”); Example prdExample = Example.create(product); prdExample.excludeProperty(”price”); prdExample.enableLike(); Criteria suppCrit = prdCrit.createCriteria(”supplier”); Supplier supplier = new Supplier(); supplier.setName(”SuperCorp”); suppCrit.add(Example.create(supplier)); prdCrit.add(prdExample); List results = prdCrit.list(); We also ignore the price property for our product, and we use LIKE for object comparison, instead of equals. The QBE API works best for searches in which you are building the search from user input. The Hibernate team recommends using QBE for advanced searches with multiple fields, because it s easier to set values on business objects than to manipulate restrictions with the Criteria API. Summary Using the Criteria API is an excellent way to get started developing with HQL. The developers of Hibernate have provided a clean API for adding restrictions to queries with Java objects. Although HQL isn t too difficult to learn, some developers prefer the Criteria Query API, as it offers compile-time syntax checking although column names and other schema-dependent information cannot be checked until run time. In the next chapter, we discuss the use of Hibernate filters to restrict the range of data against which queries are applied.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Leave a Reply