Hibernate的性能优化之 dynamic-update dynamic-insert

Guestbook.java

代码:

package com.b510.examples;

import java.util.Date;

public class Guestbook implements java.io.Serializable {

 private static final long serialVersionUID = -7004492417383895995L;
 private Integer id;
 private String name;
 private String email;
 private String phone;
 private String title;
 private String content;
 private Date createdTime;
 private Integer version;

 // Constructors

 /**
  * @return the version
  */
 public Integer getVersion() {
  return version;
 }

 /**
  * @param version
  *            the version to set
  */
 public void setVersion(Integer version) {
  this.version = version;
 }

 /**
  * @return the name
  */
 public String getName() {
  return name;
 }

 public Integer getId() {
  return this.id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 /**
  * @param name
  *            the name to set
  */
 public void setName(String name) {
  this.name = name;
 }

 /**
  * @return the email
  */
 public String getEmail() {
  return email;
 }

 /**
  * @param email
  *            the email to set
  */
 public void setEmail(String email) {
  this.email = email;
 }

 /**
  * @return the phone
  */
 public String getPhone() {
  return phone;
 }

 /**
  * @param phone
  *            the phone to set
  */
 public void setPhone(String phone) {
  this.phone = phone;
 }

 /**
  * @return the title
  */
 public String getTitle() {
  return title;
 }

 /**
  * @param title
  *            the title to set
  */
 public void setTitle(String title) {
  this.title = title;
 }

 /**
  * @return the content
  */
 public String getContent() {
  return content;
 }

 /**
  * @param content
  *            the content to set
  */
 public void setContent(String content) {
  this.content = content;
 }

 /**
  * @return the createdTime
  */
 public Date getCreatedTime() {
  return createdTime;
 }

 /**
  * @param createdTime
  *            the createdTime to set
  */
 public void setCreatedTime(Date createdTime) {
  this.createdTime = createdTime;
 }

 /** default constructor */
 public Guestbook() {
 }
}

Guestbook.hbm.xml

代码:

<?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>
 <class name="com.b510.examples.Guestbook" table="guestbook"
  catalog="users" optimistic-lock="version">
  <id name="id" type="java.lang.Integer">
   <column name="id" />
   <generator class="increment" />
  </id>
  <version name="version" column="version" access="field"></version>
  <property name="name" type="java.lang.String">
   <column name="name" length="200" />
  </property>
  <property name="email" type="java.lang.String">
   <column name="email" length="50" />
  </property>
  <property name="phone" type="java.lang.String">
   <column name="phone" length="20" />
  </property>
  <property name="title" type="java.lang.String">
   <column name="title" length="200" />
  </property>
  <property name="content" type="java.lang.String">
   <column name="content" length="1000" />
  </property>
  <property name="createdTime" type="java.util.Date">
   <column name="created_time" length="10" />
  </property>
 </class>
</hibernate-mapping>

测试代码:

HibernateTest.java

代码:

/**
 *
 */
package com.b510.examples;

import org.hibernate.Session;

/**
 *
 * @author XHW
 *
 * @date 2011-7-18
 *
 */
public class HibernateTest {
 public static void main(String[] args) {
  new HibernateTest().update();
 }
 public void update(){
  Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  Guestbook gb=(Guestbook)session.get(Guestbook.class, 2);
  System.out.println(gb.getName());
  gb.setName("北京");
  System.out.println("----------------------------");
  gb=(Guestbook)session.get(Guestbook.class, 2);
  System.out.println(gb.getName());
  session.getTransaction().commit();  
 }
}

运行效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
广州
----------------------------
北京
Hibernate:
    update
        users.guestbook
    set
        version=?,
        name=?,
        email=?,
        phone=?,
        title=?,
        content=?,
        created_time=?
    where
        id=?
        and version=?
我们看到的是我们只是跟新的是Guestbook对象的name属性值,但是呢,一个update语句却是

把所有的属性都给跟新了一遍,这样不就是很浪费时间吗?

怎样做才可以让Hibernate只是跟新我们所关注的东西,而不是我们所关注的东西,就不要去跟新他?

有一个办法可行滴!

Guestbook.hbm.xml

代码:

<?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>
 <class name="com.b510.examples.Guestbook" table="guestbook"
  catalog="users" optimistic-lock="version" dynamic-update="true">                  <!--          在这里加入动态跟新-->
  <id name="id" type="java.lang.Integer">
   <column name="id" />
   <generator class="increment" />
  </id>
  <version name="version" column="version" access="field"></version>
  <property name="name" type="java.lang.String">
   <column name="name" length="200" />
  </property>
  <property name="email" type="java.lang.String">
   <column name="email" length="50" />
  </property>
  <property name="phone" type="java.lang.String">
   <column name="phone" length="20" />
  </property>
  <property name="title" type="java.lang.String">
   <column name="title" length="200" />
  </property>
  <property name="content" type="java.lang.String">
   <column name="content" length="1000" />
  </property>
  <property name="createdTime" type="java.util.Date">
   <column name="created_time" length="10" />
  </property>
 </class>
</hibernate-mapping>

同样的测试代码:

运行效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
    select
        guestbook0_.id as id0_0_,
        guestbook0_.version as version0_0_,
        guestbook0_.name as name0_0_,
        guestbook0_.email as email0_0_,
        guestbook0_.phone as phone0_0_,
        guestbook0_.title as title0_0_,
        guestbook0_.content as content0_0_,
        guestbook0_.created_time as created8_0_0_
    from
        users.guestbook guestbook0_
    where
        guestbook0_.id=?
广州
----------------------------
北京
Hibernate:
    update
        users.guestbook
    set
        version=?,
        name=?
    where
        id=?
        and version=?
我们看到这里显示的是跟新name和version属性

测试二:

HibernateTest.java

代码:

/**
 *
 */
package com.b510.examples;

import org.hibernate.Session;

/**
 *
 * @author XHW
 *
 * @date 2011-7-18
 *
 */
public class HibernateTest {
 public static void main(String[] args) {
  new HibernateTest().update();
 }
 public void update(){
  Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  Guestbook gb=new Guestbook();
  gb.setName("南京");
  gb.setPhone("119");
  gb.setTitle("hello world!");
  session.save(gb);
  session.getTransaction().commit();  
 }
 
 
}

运行代码:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
    select
        max(id)
    from
        guestbook
Hibernate:
    insert
    into
        users.guestbook
        (version, name, email, phone, title, content, created_time, id)
    values
        (?, ?, ?, ?, ?, ?, ?, ?)
我们会发现一个问题,那就是,我们只是插入了name,phone,title属性值,但是,insert语句却同时把

其他的东西也给添加了进去,我们是不希望看到这样的结果的,要解决这样的问题,我们同样有办法滴!

Guestbook.hbm.xml

代码:

<?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>
 <class name="com.b510.examples.Guestbook" table="guestbook"
  catalog="users" optimistic-lock="version" dynamic-update="true"  dynamic-insert="true">                       动态插入数据
  <id name="id" type="java.lang.Integer">
   <column name="id" />
   <generator class="increment" />
  </id>
  <version name="version" column="version" access="field"></version>
  <property name="name" type="java.lang.String">
   <column name="name" length="200" />
  </property>
  <property name="email" type="java.lang.String">
   <column name="email" length="50" />
  </property>
  <property name="phone" type="java.lang.String">
   <column name="phone" length="20" />
  </property>
  <property name="title" type="java.lang.String">
   <column name="title" length="200" />
  </property>
  <property name="content" type="java.lang.String">
   <column name="content" length="1000" />
  </property>
  <property name="createdTime" type="java.util.Date">
   <column name="created_time" length="10" />
  </property>
 </class>
</hibernate-mapping>

运行效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
    select
        max(id)
    from
        guestbook
Hibernate:
    insert
    into
        users.guestbook
        (version, name, phone, title, id)
    values
        (?, ?, ?, ?, ?)
这样就达到了我们的预期要求,                                    要说明的是:version属性是不可为空的,所以我们没有设置值的时候,Hibernate也会给我们添加记录的

posted @ 2011-07-18 22:36  Hongten  阅读(792)  评论(0编辑  收藏  举报
Fork me on GitHub