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也会给我们添加记录的

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏函数式编程语言及工具

Akka(34): Http:Unmarshalling,from Json

  Unmarshalling是Akka-http内把网上可传输格式的数据转变成程序高级结构话数据的过程,比如把Json数据转换成某个自定义类型的实例。按具体流...

23470
来自专栏码匠的流水账

聊聊resilience4j的CircuitBreakerStateMachine

本文主要研究一下resilience4j的CircuitBreakerStateMachine

13220
来自专栏算法修养

FZU Moon Game(几何)

Accept: 710    Submit: 2038 Time Limit: 1000 mSec    Memory Limit : 32768 KB  P...

32750
来自专栏函数式编程语言及工具

Akka(40): Http:Marshalling reviewed - 传输数据序列化重温

   上篇我们讨论了Akka-http的文件交换。由于文件内容编码和传输线上数据表达型式皆为bytes,所以可以直接把文件内容存进HttpEntity中进行传递...

24880
来自专栏冷冷

SpringMVC 之 ModelAndView和ModelAttribute的使用

ModelAndView解释 : This class merely holds both to make it possible for a controll...

27350
来自专栏用户画像

webService 接受提交的JSon数据

11730
来自专栏我是业余自学C/C++的

sprintf_s的使用

47520
来自专栏Hongten

Hibernate中的二级缓存 EHCache

<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC ...

9610
来自专栏一个会写诗的程序员的博客

来自国外Kotin 布道师的 完整版【Kotlin 简明教程】1Kotlin 开发者社区

A programming language is usually designed with a specific purpose in mind. This...

23420
来自专栏Hongten

HIbernate 中的 继承映射

数据库中Delivery表:id,recipient,phone,address,postcode,delivery_type,parcel_number,ex...

7310

扫码关注云+社区

领取腾讯云代金券