Creating and running queries Query q (Web site templates) = session.createQuery(”from

Creating and running queries Query q = session.createQuery(”from Item”) .setComment(”My Comment…”); Criteria criteria = session.createCriteria(Item.class) .setComment(”My Comment…”); Query q = em.createQuery(”select i from Item i”) .setHint(”org.hibernate.comment”, “My Comment…”); The hints you ve been setting so far are all related to Hibernate or JDBC handling. Many developers (and DBAs) consider a query hint to be something completely different. In SQL, a query hint is a comment in the SQL statement that contains an instruction for the SQL optimizer of the database management system. For example, if the developer or DBA thinks that the execution plan selected by the database optimizer for a particular SQL statement isn t the fastest, they use a hint to force a different execution plan. Hibernate and Java Persistence don t support arbitrary SQL hints with an API; you ll have to fall back to native SQL and write your own SQL statement you can of course execute that statement with the provided APIs. (With some database-management systems you can control the optimizer with an SQL comment at the beginning of an SQL statement; in that case, use Query.setComment() to add the hint. In other scenarios, you may be able to write an org.hibernate.Interceptor and manipulate an SQL statement in the onPrepareStatement(sql) method before it s sent to the database.) Finally, you can control whether a query should force a pessimistic lock in the database management system a lock that is held until the end of the database transaction: Query q = session.createQuery(”from Item item”) .setLockMode(”item”, LockMode.UPGRADE); Criteria criteria = session.createCriteria(Item.class) .setLockMode(LockMode.UPGRADE); Both queries, if supported by your database dialect, result in an SQL statement that includes a … FOR UPDATE operation (or the equivalent, if supported by the database system and dialect). Currently, pessimistic locking isn t available (but it s planned as a Hibernate extension hint) on the Java Persistence query interface. Let s assume that queries are now prepared, so you can run them. 14.1.2 Executing a query Once you ve created and prepared a Query or Criteria object, you re ready to execute it and retrieve the result into memory. Retrieving the whole result into
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Leave a Reply