问题:设置了dynamic-update, 可是事实上并没有按照期望进行了update。

案例代码如下:

1、持久化对象

 package com.jdw.hibernate.entities;

 import java.util.Date;

 public class News {
private Integer id;
private String title;
private String author;
private Date date; public News() {
// TODO Auto-generated constructor stub
} public News(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
} @Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", author=" + author
+ ", date=" + date + "]";
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
}
}

2、hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-3-8 20:08:39 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.jdw.hibernate.entities.News" table="NEWS" lazy="false" select-before-update="true" dynamic-update="true">
<id name="id" type="java.lang.Integer" unsaved-value="110">
<column name="ID" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR" />
</property>
<property name="date" type="java.util.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>

3、测试代码

	@Test
public void testDynamicUpdate() {
News news =new News(); news.setAuthor("test");//注意:这里只设置了test属性
news.setId(13); session.update(news);
}

4、结果

Hibernate:
select
news_.ID,
news_.TITLE as TITLE2_1_,
news_.AUTHOR as AUTHOR3_1_,
news_.DATE as DATE4_1_
from
NEWS news_
where
news_.ID=?
Hibernate:
update
NEWS
set
TITLE=?,
AUTHOR=?,
DATE=?
where
ID=?

hibernate并没有按照期望只update Author属性,而是更新了所有属性,其他属性肯定更新成了null,坏菜了!

网上查了资料,不少同学说要在设置dynamic-update的同时,设置select-before-update即可满足期望,注意上面的映射文件,是设置了该属性的。

那问题出在哪里?如何解决呢?

--------------------------------------------------------------------------------

先看另一段测试代码:

        @Test
public void testDynamicUpdate2() {
News news =(News) session.get(News.class, 13); news.setAuthor("test");//注意:这里只设置了test属性 session.update(news);
}

 结果如你所愿,只更新了author字段。两项比较,dynamic-update=true,只对持久化对象起作用,对于transient临时对象不起作用。至于为什么这么设计?求高人告之。

Hibernate之dynamic-update的更多相关文章

  1. org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

    org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua ...

  2. 20.org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

    org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua ...

  3. 关于Hibernate级联更新插入信息时提示主键不为空的问题“org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1 ”

    org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual ...

  4. Hibernate: save, persist, update, merge, saveOrUpdate[z]

    [z]https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate 1. Introduction In this ...

  5. Hibernate merge和update的区别

    今天做了个测试,写了个测试用例来看看merge与update时控制台打印出来的日志有什么不一样.实体bean很简单,就id和name两个字段,接下来分别给出以下几种测试情形的控制台日志内容: 1. 数 ...

  6. BIND9源码分析之 多个view的情况下如何做dynamic update

    BIND中view的存在提供了一种较好的智能DNS方案,BIND可以根据用户的来源IP为其返回不同的Resource Record. 但是关于DNS动态更新的RFC2136中并没有提及view(vie ...

  7. hibernate.hbm2ddl.auto=update不能自动生成表结构

    在写上篇文章<spring整合springmvc和hibernate>的时候,曾遇到一个问题 INFO: Server startup in 8102 ms Hibernate: inse ...

  8. Hibernate HQL的update方法详解

    虽然hibernate提供了许多方法对数据库进行更新,但是这的确不能满足开发需要.现在讲解一下用hql语句对数据进行更新. 不使用参数绑定格式String hql="update User ...

  9. Hibernate不调用update却自动更新

    案例: TInfCustomer cus = (TInfCustomer) this.baseDao.getOne(helper); cus.setXXX cus .setXXX 不调用update也 ...

  10. <property name="hibernate.hbm2ddl.auto">update</property> 问题

    其实这个hibernate.hbm2ddl.auto参数的作用主要用于:自动创建|更新|验证数据库表结构.如果不是此方面的需求建议set value="none".create:每 ...

随机推荐

  1. python正则表达式练习篇2

    首先生成正则表达式练习的数据: #! /usr/bin/python from random import randint, choice from string import lowercase f ...

  2. slice,substr,substring

    var s = '0123456789';   //slice 遇负数,则此负数值等同于 s.length + (负数) s.slice(3,6); //"345", 从位置3开始 ...

  3. JS如何将UTC格式时间转本地格式

    Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, //month & ...

  4. LinuxCmd

    Q1.关掉字符界面下的屏保 A:setterm -blank 0 Q2.top Top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. ...

  5. Oracle EBS-SQL (BOM-8):检查物料属性(无采购员).sql

    select       msi.segment1                                  物料编码,       msi.DESCRIPTION               ...

  6. egret-android-support-gradle版

    从3.1.3开始,Egret已经实现了Gradle构建!所以下文你爱看不看! 迟钝的Egret从3.1.3版本才开始支持Gradle,而笔者早在1.6.x版本就已经支持了,说明什么?说明Egret在某 ...

  7. linux 学习之九、Linux 磁盘与文件系统管理(1)

    原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0230filesystem.php#filesys 查看文件系统参数命令 dumpe2fs 盘符地址 例: ...

  8. 关于c++primer的一个代码错误

    近期看c++primer第四版的标准容器vector.讲到对vector容器的插入删除元素会使得end()的迭代器失效的问题,所以不建议程序猿对end()的存储. vector<int> ...

  9. 四、cocos2dx动画Animation介绍

    qinning199原创,欢迎转载,转载请注明:http://www.cocos2dx.net/?p=22 一.帧动画 你可以通过一系列图片文件,像如下这样,创建一个动画: CCAnimation * ...

  10. CSS3 简易照片墙

    代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title& ...