hibernate dynamic-insert 和 dynamic-update使用

本文介绍了Hibernate中的dynamic-insert和dynamic-update属性,它们用于优化数据库操作。dynamic-insert只插入非空字段,dynamic-update只更新已改变的字段,从而节省了数据库资源。示例展示了如何在XML映射文件和注解中配置这两个属性。
摘要由CSDN通过智能技术生成
默认情况下,在hibernate启动的时候,会为每个实体类生成简单的读取,删除,更新,创建语句,它是怎么生成更新的呢,它怎么知道更新的列,生成更新所有列的sql语句,如果值没有修改,则被设置为它的旧值,如果表的列比较多,即使只更新了一个列,也会生成很长的sql语句,必须关闭启动时生成sql语句,hibernate提供了dynamic-insert和dynamic-update属性,用来关闭生成sql语句,看下面例子。 
持久化类 
Java代码   收藏代码
  1.   package com.own.model;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Goods implements Serializable {  
  6.       
  7.       
  8.     private static final long serialVersionUID = 1L;  
  9.     private Integer id;  
  10.     private  String goodsName;  
  11.     private Double price;  
  12.     private String goodsDescription;      
  13.       
  14.     public Goods(){}  
  15.       
  16.     public Integer getId() {  
  17.         return id;  
  18.     }  
  19.       
  20.     public void setId(Integer id) {  
  21.         this.id = id;  
  22.     }  
  23.     public String getGoodsName() {  
  24.         return goodsName;  
  25.     }  
  26.     public void setGoodsName(String goodsName) {  
  27.         this.goodsName = goodsName;  
  28.     }  
  29.     public Double getPrice() {  
  30.         return price;  
  31.     }  
  32.     public void setPrice(Double price) {  
  33.         this.price = price;  
  34.     }  
  35.     public String getGoodsDescription() {  
  36.         return goodsDescription;  
  37.     }  
  38.     public void setGoodsDescription(String goodsDescription) {  
  39.         this.goodsDescription = goodsDescription;  
  40.     }  
  41.   
  42.       
  43.     @Override  
  44.     public boolean equals(Object o) {  
  45.           
  46.         if(o == null || o.getClass() != this.getClass()){  
  47.             return false;  
  48.         }  
  49.           
  50.         if(o == this){  
  51.             return true;  
  52.         }  
  53.           
  54.         Goods goods = (Goods) o;  
  55.           
  56.         if(id == null ? goods.id == null : this.id.equals(goods.id)){  
  57.             return true;  
  58.         }  
  59.           
  60.           
  61.         return false;  
  62.     }  
  63.       
  64.     @Override  
  65.     public int hashCode() {  
  66.         return this.id.hashCode()  ;  
  67.     }  
  68.       
  69. }  

Xml代码   收藏代码
  1.   <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping >  
  7.   
  8.         <class name="com.own.model.Goods"  table="goods" dynamic-insert="false" dynamic-update="false"     >  
  9.             
  10.            <id name="id"  column="goods_id" >  
  11.               <generator class="native"></generator>  
  12.            </id>  
  13.             <property name="price"  column="goods_price" type="double"  ></property>    
  14.             <property name="goodsName"  column="goods_name" type="string"  ></property>    
  15.             <property name="goodsDescription"  column="goods_description" type="string"  ></property>    
  16.               
  17.               
  18.         </class>  
  19.     
  20.     
  21.    
  22.         
  23. </hibernate-mapping>  
  24.      

调用hibernate的保存和更新方法,会生成如下的sql语句, 
Sql代码   收藏代码
  1. insert   
  2.  into  
  3.      goods  
  4.      (goods_price, goods_name, goods_description)   
  5.  values  
  6.      (?, ?, ?)  
  7.   
  8. update  
  9.      goods   
  10.  set  
  11.      goods_price=?,  
  12.      goods_name=?,  
  13.      goods_description=?   
  14.  where  
  15.      goods_id=?  

如果把属性修改为true,则hibernat在生成insert语句时,会根据持久化类的属性,如果属性的值为null,则生成不插入该字段的sql语句,在更新的时候,如果该实体在session管理中,修改某个属性,则只生成修改该字段的更新语句,而不是把所有的属性都更新。 
Sql代码   收藏代码
  1.    insert   
  2.  into  
  3.      goods  
  4.      (goods_name)   
  5.  values  
  6.      (?)  
  7.   
  8. update  
  9.      goods   
  10.  set  
  11.      goods_name=?   
  12.  where  
  13.      goods_id=?  

如果使用jpa,使用注解元数据的话,则要使用hibernate提供的注解,实体代码如下 
Java代码   收藏代码
  1.   package com.own.model;  
  2.   
  3.   
  4. import java.io.Serializable;  
  5.   
  6. import javax.persistence.Column;  
  7. import javax.persistence.Entity;  
  8. import javax.persistence.GeneratedValue;  
  9. import javax.persistence.GenerationType;  
  10. import javax.persistence.Id;  
  11. import javax.persistence.Table;  
  12.   
  13. @Entity  
  14. @org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)  
  15. @Table(name="goods")  
  16. public class Goods implements Serializable {  
  17.       
  18.       
  19.     private static final long serialVersionUID = 1L;  
  20.     private Integer id;  
  21.     private  String goodsName;  
  22.     private Double price;  
  23.     private String goodsDescription;      
  24.       
  25.     public Goods(){}  
  26.       
  27.     @Id  
  28.     @GeneratedValue(strategy=GenerationType.AUTO)  
  29.     @Column(name="goods_id")  
  30.     public Integer getId() {  
  31.         return id;  
  32.     }  
  33.       
  34.     public void setId(Integer id) {  
  35.         this.id = id;  
  36.     }  
  37.       
  38.     @Column(name="goods_name",length=40,nullable=false)  
  39.     public String getGoodsName() {  
  40.         return goodsName;  
  41.     }  
  42.     public void setGoodsName(String goodsName) {  
  43.         this.goodsName = goodsName;  
  44.     }  
  45.       
  46.     @Column(name="goods_price")  
  47.     public Double getPrice() {  
  48.         return price;  
  49.     }  
  50.     public void setPrice(Double price) {  
  51.         this.price = price;  
  52.     }  
  53.       
  54.     @Column(name="goods_description")  
  55.     public String getGoodsDescription() {  
  56.         return goodsDescription;  
  57.     }  
  58.     public void setGoodsDescription(String goodsDescription) {  
  59.         this.goodsDescription = goodsDescription;  
  60.     }  
  61.   
  62.       
  63.     @Override  
  64.     public boolean equals(Object o) {  
  65.           
  66.         if(o == null || o.getClass() != this.getClass()){  
  67.             return false;  
  68.         }  
  69.           
  70.         if(o == this){  
  71.             return true;  
  72.         }  
  73.           
  74.         Goods goods = (Goods) o;  
  75.           
  76.         if(id == null ? goods.id == null : this.id.equals(goods.id)){  
  77.             return true;  
  78.         }  
  79.           
  80.           
  81.         return false;  
  82.     }  
  83.       
  84.     @Override  
  85.     public int hashCode() {  
  86.         return this.id.hashCode()  ;  
  87.     }  
  88.       
  89. }  

加上这两个属性,就避免了当数据表中字段很多的情况下,更新没有修改的字段,插入的时候,就不生成插入属性为null的字段。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值