CHAPTER 12 Modifying objects efficiently the state of (Web hosting domain)
CHAPTER 12 Modifying objects efficiently the state of a persistent object from the database, if you know it s been modified behind the back of the persistence context. Hibernate and JPA offer DML operations that are a little more powerful than plain SQL. Let s look at the first operation in HQL and JPA QL, an UPDATE: Query q = session.createQuery(”update Item i set i.isActive = :isActive”); q.setBoolean(”isActive”, true); int updatedItems = q.executeUpdate(); This HQL statement (or JPA QL statement, if executed with the EntityManager) looks like an SQL statement. However, it uses an entity name (class name) and a property name. It s also integrated into Hibernate s parameter binding API. The number of updated entity objects is returned not the number of updated rows. Another benefit is that the HQL (JPA QL) UPDATE statement works for inheritance hierarchies: Query q = session.createQuery( “update CreditCard set stolenOn <= :now where type = 'Visa'" ); q.setTimestamp("now", new Date() ); int updatedCreditCards = q.executeUpdate(); The persistence engine knows how to execute this update, even if several SQL statements have to be generated; it updates several base tables (because Credit- Card is mapped to several superclass and subclass tables). This example also doesn t contain an alias for the entity class it s optional. However, if you use an alias, all properties must be prefixed with an alias. Also note that HQL (and JPA QL) UPDATE statements can reference only a single entity class; you can t write a single statement to update Item and CreditCard objects simultaneously, for example. Subqueries are allowed in the WHERE clause; any joins are allowed only in these subqueries. Direct DML operations, by default, don t affect any version or timestamp values of the affected entities (this is standardized in Java Persistence). With HQL, however, you can increment the version number of directly modified entity instances: Query q = session.createQuery( "update versioned Item i set i.isActive = :isActive" ); q.setBoolean("isActive", true); int updatedItems = q.executeUpdate(); (The versioned keyword is not allowed if your version or timestamp property relies on a custom org.hibernate.usertype.UserVersionType.)
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.