前一例中BookHibernateDao的list方法用到了getSession().createQuery("HQL statement"); 返回一个Query, 这个Query是面向对象查询的最重要的接口.相当于jdbc中的statement,Query通过HQL和数据库交互.最简单的HQL: from Book 列出所有的Book对象. select到哪儿去了?这句话隐含select,可以这么写:
select b from Book b , 但是不能这么写: select * from Book
HQL可以带条件,比如 from Book b where b.author='sunxing007'.
HQL还可以带占位符:比如:
query = getSession().createQuery("from Book b where b.author=?");
query.setParameter(0, 'sunxing007');注意是从0开始, 和PreparedStatement不一样;
HQL还可以带命名参数,比如:
query = getSession().createQuery("from Book b where b.author=:authur");
query.setParameter("author", 'sunxing007');
以上的HQL都是写在代码中, 还可以把HQL写在配置文件中集中管理。下面采用这种方式来改进Book.hbm.xml和完善BookDao:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="model"> <class name="Book" table="book" schema="dbo" catalog="hibernate"> <id name="id" column="id"> <generator class="uuid" /> </id> <property name="name" /> <property name="author" /> <property name="price" /> </class> <!--在这里配置一个query--> <query name="book.byAuthor"> <!--[CDATA[from Book b where b.author=:author]]--> </query> </hibernate-mapping> <!-- create table book ( id int identity(1,1) primary key, name varchar(20) not null, author varchar(20), price float ) -->
package dao; import java.util.List; import model.Book; public interface BookDao { public List<Book> list(); public Book findById(String id); public void save(Book book); public void delete(String id); public List findByAuthor(String author); public Book findByName(String name); }
package dao.hibernate; import java.util.List; import model.Book; import dao.BookDao; public class BookHibernateDao extends BaseHibernateDao implements BookDao{ @SuppressWarnings("unchecked") public List<Book> list() { return getSession().createQuery("from Book").list(); } public Book findById(String id) { return (Book) getSession().load(Book.class, id); } public void save(Book book) { //Transaction tx = getSession().beginTransaction(); getSession().saveOrUpdate(book); getSession().flush(); //tx.commit(); } public void delete(String id) { Book b = (Book) getSession().load(Book.class, id); //Transaction tx = getSession().beginTransaction(); getSession().delete(b); //tx.commit(); } public List findByAuthor(String author) { //book.byAuthor配置在Book.hbm.xml中 return getSession().getNamedQuery("book.byAuthor").setString("author", author).list(); } public Book findByName(String name) { String hql = "from Book b where b.name=?"; return (Book) getSession().createQuery(hql).setString(0, name).list().get(0); } //test case @SuppressWarnings("unchecked") public static void main(String[] args){ BookDao dao = new BookHibernateDao(); Book b = dao.findByName("ww2"); System.out.println(b); List<Book> list = dao.findByAuthor("author2"); System.out.println(list.size()); for(Book b1 : list){ System.out.println(b1); } /** Book b = new Book(null, "book name", "book author", 11.5F); //dao.save(b); Book b1 = dao.findById("204"); System.out.println(b1); **/ } }