天天看点

hibernate动态更新

 这两天把原来的hibernate又复习了一下,意外发现hibernate有一个动态增加和动态更新功能。就是说hibernate生成的sql语句只set,有变化的字段。这样确实会从一定程度上提高性能。

      可是动态更新的问题是查询和更新必须同时在一个相同的session中,否则hibernate无法判断这是不是一个相同的对象。可是实际上我们用更新基本上都是查询出来之后,在前台做一些从新的赋值,在放到一个专有的update方法里更新。这样动态update的功能意义不大了。怎么办呢?大家还记得吗HibernateSessionFactory中的session都是单例的这样,我们要DAO层的查询和更新不关闭session.close,将关闭session的任务交给业务逻辑层来完成不就解决了这个问题吗。

这是我的代码不知道这么做会不会有什么潜在问题,希望大家指教。还有动态update真会有性能提升吗?

Xml代码 复制代码

   1. <?xml version="1.0" encoding="utf-8"?> 

   2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 

   3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

   4. <!-- 

   5.     Mapping file autogenerated by MyEclipse Persistence Tools

   6. --> 

   7. <hibernate-mapping> 

   8.     <class name="hibrenate.vo.Customer" table="customer" catalog="dept_development" dynamic-insert="true" dynamic-update="true"> 

   9.         <id name="id" type="java.lang.Integer"> 

  10.             <column name="id" /> 

  11.             <generator class="assigned" /> 

  12.         </id> 

  13.         <many-to-one name="creditCard" class="hibrenate.vo.CreditCard" fetch="select"> 

  14.             <column name="card_id" not-null="true" /> 

  15.         </many-to-one> 

  16.         <property name="firstname" type="java.lang.String"> 

  17.             <column name="firstname" not-null="true" /> 

  18.         </property> 

  19.         <property name="lastname" type="java.lang.String"> 

  20.             <column name="lastname" not-null="true" /> 

  21.         </property> 

  22.         <property name="creattime" type="java.util.Date"> 

  23.             <column name="creattime" length="0" not-null="true" /> 

  24.         </property> 

  25.     </class> 

  26. </hibernate-mapping> 

<?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">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="hibrenate.vo.Customer" table="customer" catalog="dept_development" dynamic-insert="true" dynamic-update="true">

        <id name="id" type="java.lang.Integer">

            <column name="id" />

            <generator class="assigned" />

        </id>

        <many-to-one name="creditCard" class="hibrenate.vo.CreditCard" fetch="select">

            <column name="card_id" not-null="true" />

        </many-to-one>

        <property name="firstname" type="java.lang.String">

            <column name="firstname" not-null="true" />

        </property>

        <property name="lastname" type="java.lang.String">

            <column name="lastname" not-null="true" />

        </property>

        <property name="creattime" type="java.util.Date">

            <column name="creattime" length="0" not-null="true" />

        </property>

    </class>

</hibernate-mapping>

Java代码 复制代码

   1. package hibernate.dao; 

   2.  

   3. import org.hibernate.Session; 

   4. import org.hibernate.Transaction; 

   5.  

   6. import hibernate.HibernateSessionFactory; 

   7. import hibrenate.vo.Customer; 

   8.  

   9. public class CustomerDao { 

  10.     //查询一个对象 

  11.     public Customer selectOne(Integer id) 

  12.     { 

  13.         Customer customer = null; 

  14.         Session session = HibernateSessionFactory.getSession(); 

  15.         Transaction transaction =   session.beginTransaction(); 

  16.         customer = (Customer) session.get(Customer.class, id); 

  17.         transaction.commit(); 

  18.         //不关闭session 

  19.     //  HibernateSessionFactory.closeSession(); 

  20.         return customer; 

  21.     } 

  22.     //更新一个对象 

  23.     public void update(Customer customer) 

  24.     { 

  25.         Session session = HibernateSessionFactory.getSession(); 

  26.         Transaction transaction =   session.beginTransaction(); 

  27.         session.update(customer); 

  28.         transaction.commit(); 

  29.         //不关闭session 

  30.         //HibernateSessionFactory.closeSession(); 

  31.          

  32.     } 

  33. } 

  34.  

  35.  

  36.  

  37. package hibernate.dao; 

  38.  

  39. import hibernate.HibernateSessionFactory; 

  40. import hibrenate.vo.Customer; 

  41.  

  42. public class Test{ 

  43.     public static void main(String[] args) { 

  44.         CustomerDao  dao = new CustomerDao (); 

  45.         Customer customer = dao.selectOne(1); 

  46.         customer.setFirstname("张欣"); 

  47.         dao.update(customer); 

  48.         //关闭Session 

  49.         HibernateSessionFactory.closeSession(); 

  50.          

  51.     } 

《Hiberante In Action》上介绍说 除非表里含有大字段或者字段数超过50个以上 否则不建议开启“动态更新”和“动态插入”

因为update xxxxx及insert xxxxx语句默认都是SessionFactory建立时就生成好的

开启“动态”后就需要运行时现组装了 也需要挨个验证各属性是否改变

这些操作也会浪费一定的效率 会抵消“动态”的好处

  52. } 

继续阅读