Creating and running queries All query hints that you set earlier with an API can also be set declaratively: Named queries don t have to be HQL or JPA QL strings; they may even be native SQL queries and your Java code doesn t need to know the difference: This is useful if you think you may want to optimize your queries later by fine-tuning the SQL. It s also a good solution if you have to port a legacy application to Hibernate, where SQL code was isolated from the hand-coded JDBC routines. With named queries, you can easily port the queries one-by-one to mapping files. We ll have much more to say about native SQL queries in the next chapter. Defining a named query with annotations The Java Persistence standard specifies the @NamedQuery and @NamedNativeQuery annotations. You can either place these annotations into the metadata of a particular class or into JPA XML descriptor file. Note that the query name must be globally unique in all cases; no class or package name is automatically prefixed. Let s assume you consider a particular named query to belong to a particular entity class: package auction.model; import …; @NamedQueries({ @NamedQuery( name = “findItemsByDescription”, query = “select i from Item i where i.description like :desc” ), … }) @Entity @Table(name = “ITEM”) public class Item { … }
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.
This entry was posted
on Friday, April 11th, 2008 at 2:17 pm and is filed under j2ee.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.