CHAPTER 15 Advanced query options This chapter explains

CHAPTER 15 Advanced query options This chapter explains all query options that you may consider optional or advanced. You ll need the first subject of this chapter, the Criteria query interface, whenever you create more complex queries programmatically. This API is much more convenient and elegant than programmatic generation of query strings for HQL and JPA QL. Unfortunately, it s also only available as a native Hibernate API; Java Persistence doesn t (yet) standardize a programmatic query interface. Both Hibernate and Java Persistence support queries written in native SQL. You can embed SQL and stored procedure calls in your Java source code or externalize them to mapping metadata. Hibernate can execute your SQL and convert the resultset into more convenient objects, depending on your mapping. Filtering of collections is a simple convenience feature of Hibernate you won t use it often. It helps you to replace a more elaborate query with a simple API call and a query fragment, for example, if you want to obtain a subset of the objects in a collection. Finally, we ll discuss the optional query result cache we ve already mentioned that it s not useful in all situations, so we ll take a closer look at the benefits of caching results of a query and when you d ideally enable this feature. Let s start with query by criteria and query by example. 15.1 Querying with criteria and example The Criteria and Example APIs are available in Hibernate only; Java Persistence doesn t standardize these interfaces. As mentioned earlier, it seems likely that other vendors, not only Hibernate, support a similar extension interface and that a future version of the standard will include this functionality. Querying with programmatically generated criteria and example objects is often the preferred solution when queries get more complex. This is especially true if you have to create a query at runtime. Imagine that you have to implement a search mask in your application, with many check boxes, input fields, and switches the user can enable. You must create a database query from the user s selection. The traditional way to do this is to create a query string through concatenation, or maybe to write a query builder that can construct the SQL query string for you. You d run into the same problem if you d try to use HQL or JPA QL in this scenario. The Criteria and Example interfaces allow you to build queries programmatically by creating and combining objects in the right order. We now show you how
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Leave a Reply