默认情况下,在hibernate启动的时候,会为每个实体类生成简单的读取,删除,更新,创建语句,它是怎么生成更新的呢,它怎么知道更新的列,生成更新所有列的sql语句,如果值没有修改,则被设置为它的旧值,如果表的列比较多,即使只更新了一个列,也会生成很长的sql语句,必须关闭启动时生成sql语句,hibernate提供了dynamic-insert和dynamic-update属性,用来关闭生成sql语句,看下面例子。
持久化类
调用hibernate的保存和更新方法,会生成如下的sql语句,
如果把属性修改为true,则hibernat在生成insert语句时,会根据持久化类的属性,如果属性的值为null,则生成不插入该字段的sql语句,在更新的时候,如果该实体在session管理中,修改某个属性,则只生成修改该字段的更新语句,而不是把所有的属性都更新。
如果使用jpa,使用注解元数据的话,则要使用hibernate提供的注解,实体代码如下
加上这两个属性,就避免了当数据表中字段很多的情况下,更新没有修改的字段,插入的时候,就不生成插入属性为null的字段。
持久化类
package com.own.model;
import java.io.Serializable;
public class Goods implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String goodsName;
private Double price;
private String goodsDescription;
public Goods(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getGoodsDescription() {
return goodsDescription;
}
public void setGoodsDescription(String goodsDescription) {
this.goodsDescription = goodsDescription;
}
@Override
public boolean equals(Object o) {
if(o == null || o.getClass() != this.getClass()){
return false;
}
if(o == this){
return true;
}
Goods goods = (Goods) o;
if(id == null ? goods.id == null : this.id.equals(goods.id)){
return true;
}
return false;
}
@Override
public int hashCode() {
return this.id.hashCode() ;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="com.own.model.Goods" table="goods" dynamic-insert="false" dynamic-update="false" >
<id name="id" column="goods_id" >
<generator class="native"></generator>
</id>
<property name="price" column="goods_price" type="double" ></property>
<property name="goodsName" column="goods_name" type="string" ></property>
<property name="goodsDescription" column="goods_description" type="string" ></property>
</class>
</hibernate-mapping>
调用hibernate的保存和更新方法,会生成如下的sql语句,
insert
into
goods
(goods_price, goods_name, goods_description)
values
(?, ?, ?)
update
goods
set
goods_price=?,
goods_name=?,
goods_description=?
where
goods_id=?
如果把属性修改为true,则hibernat在生成insert语句时,会根据持久化类的属性,如果属性的值为null,则生成不插入该字段的sql语句,在更新的时候,如果该实体在session管理中,修改某个属性,则只生成修改该字段的更新语句,而不是把所有的属性都更新。
insert
into
goods
(goods_name)
values
(?)
update
goods
set
goods_name=?
where
goods_id=?
如果使用jpa,使用注解元数据的话,则要使用hibernate提供的注解,实体代码如下
package com.own.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="goods")
public class Goods implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String goodsName;
private Double price;
private String goodsDescription;
public Goods(){}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="goods_id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="goods_name",length=40,nullable=false)
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
@Column(name="goods_price")
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Column(name="goods_description")
public String getGoodsDescription() {
return goodsDescription;
}
public void setGoodsDescription(String goodsDescription) {
this.goodsDescription = goodsDescription;
}
@Override
public boolean equals(Object o) {
if(o == null || o.getClass() != this.getClass()){
return false;
}
if(o == this){
return true;
}
Goods goods = (Goods) o;
if(id == null ? goods.id == null : this.id.equals(goods.id)){
return true;
}
return false;
}
@Override
public int hashCode() {
return this.id.hashCode() ;
}
}
加上这两个属性,就避免了当数据表中字段很多的情况下,更新没有修改的字段,插入的时候,就不生成插入属性为null的字段。