tailieunhanh - manning Hibernate in Action phần 8
Tham khảo tài liệu 'manning hibernate in action phần 8', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Executing queries 249 setProperties matches the names of JavaBean properties to named parameters in the query string using setParameter to guess the Hibernate type and bind the value. In practice this turns out to be less useful than it sounds since some common Hibernate types aren t guessable date in particular . The parameter-binding methods of Query are null-safe making this code legal from User as u where name .setString name null . list However the result of this code is almost certainly not what we intended. The resulting SQL will contain a comparison like username null which always evaluates to null in SQL ternary logic. Instead we must use the is null operator from User as u where is null .list So far the HQL code examples we ve shown all use embedded HQL query string literals. This isn t unreasonable for simple queries but once we start considering complex queries that must be split over multiple lines it starts to get a bit unwieldy. Using named queries We don t like to see HQL string literals scattered all over the Java code unless they re necessary. Hibernate lets you externalize query strings to the mapping metadata a technique that is called named queries. This allows you to store all queries related to a particular persistent class or a set of classes encapsulated with the other metadata of that class in an XML mapping file. The name of the query is used to call it from the application. The getNamedQuery method obtains a Query instance for a named query findItemsByDescription .setString description description . list In this example we execute the named query findItemsByDescription after binding a string argument to a named parameter. The named query is defined in mapping metadata . in using the query element query name findItemsByDescription CDATA from Item item where like description query Named queries don t have to be HQL .
đang nạp các trang xem trước