session.createQuery的参数绑定

参数绑定: 
都支持位置绑定和命名绑定, 
如: 

Java代码

  1. session.createQuery("from Person where id=:id").setInteger("id",1);   
  2. session.createQuery("from Person where id=?").setInteger(0,1);  

session.createQuery("from Person where id=:id").setInteger("id",1);session.createQuery("from Person where id=?").setInteger(0,1);



基本的参数绑定: 
setString() 
setInteger(); 
等... 


特殊的参数绑定 
(1)setEntity() 

Java代码

  1. Customer customer = new Customer();   
  2. customer.setId(1);   
  3. session.createQuery("from Order o where o.customer =:customer ").setEntity("customer", customer);  

Customer customer = new Customer();customer.setId(1);session.createQuery("from Order o where o.customer =:customer ").setEntity("customer", customer);


  等价于 

Java代码

  1. session.createQuery("from Order o where o.customer.id =:customerid ").setInteger("customerid", 1);  

session.createQuery("from Order o where o.customer.id =:customerid ").setInteger("customerid", 1);


(2)setParameter() 

Java代码

  1. Query query = session.createQuery("from Order o where o.customer=:customer and o.orderNumber like :orderNumber");   
  2. query.setParameter("customer", customer, Hibernate.entity(Customer.class));  

Query query = session.createQuery("from Order o where o.customer=:customer and o.orderNumber like :orderNumber");query.setParameter("customer", customer, Hibernate.entity(Customer.class));


query.setParameter("orderNumber", orderNumber, Hibernate.STRING); 
setParameter()方法的第三个参数显示指定Hibernate映射类型.以下代码用于绑定一个客户化映射类型.(关于客户化映射类型,参见:http://www.javaeye.com/topic/182797) 

Java代码

  1. Query query = session.createQuery("from Customer c where c.homeAddress=:homeAddress");   
  2. query.setParameter("homeAddress",homeAddress,Hibernate.custom(AddressUserType.class));  

Query query = session.createQuery("from Customer c where c.homeAddress=:homeAddress");query.setParameter("homeAddress",homeAddress,Hibernate.custom(AddressUserType.class));


  对于某些参数,Hibernate能跟根据参数值的Java类型推断出对应的映射类型,此时不需要在setParameter()方法中显式指定映射类型,如: 

Java代码

  1. Query query = session.createQuery("from Order o where o.customer=:customer and o.orderNumber like :orderNumber");   
  2. query.setParameter("customer", customer);   
  3. query.setParameter("orderNumber", orderNumber);  

Query query = session.createQuery("from Order o where o.customer=:customer and o.orderNumber like :orderNumber");query.setParameter("customer", customer);query.setParameter("orderNumber", orderNumber);


  对于日期类型,如java.util.Date类型,会对应多种Hibernate映射类型,如Hibernate.DATE或 Hibernate.TIMESTAMP,因此必须在setParameter()方法中显式指定到底对应哪种Hibernate映射类型,如: 

Java代码

  1. Query query = session.createQuery("from Customer c where c.birthday=:birthday");   
  2. query.setParameter("birthday",birthday,Hibernate.DATE);  

Query query = session.createQuery("from Customer c where c.birthday=:birthday");query.setParameter("birthday",birthday,Hibernate.DATE);


(3)setProperties()方法:用于把命名参数与一个对象的属性值绑定,如: 

Java代码

  1. Customer customer = new Customer();   
  2. customer.setName("Tom");   
  3. customer.setAge(21);   
  4. Query query = session.createQuery("from Customer as c where c.name=:name and c.age="age");   
  5. query.setProperties(customer);  

Customer customer = new Customer();customer.setName("Tom");customer.setAge(21);Query query = session.createQuery("from Customer as c where c.name=:name and c.age="age");query.setProperties(customer);


  setProperties()调用setParameter()方法,setParameter()方法再根据Customer对象的属性的Java类型来推断Hibernate映射类型.如果命名参数为日期类型,不能通过setProperties()方法来绑定. 
  参数绑定对null是安全的.

posted @ 2013-01-10 15:33  Struts-pring  阅读(682)  评论(0编辑  收藏  举报