问题:设置了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. sqlserver数据库三范式的理解

    从来都是听过概念,过一段时间就忘记了,根本就没有深入的理解.这次梳理一遍,用自己的方式记录一下. 1nf 原子性,不可拆分性 例如一张表里包含一个class属性(软件系,外语系,经贸系...)字段,这 ...

  2. BFC块级排版上下文

    1.BFC 全称是块级排版上下文,用于对块级元素排版,默认情况下只有根元素(body)一个块级上下文,但是如果一个块级元素 设置了float:left,overflow:hidden或position ...

  3. C#中in,out,ref,params的作用和区别

    ref和out的区别在C# 中,既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值,并保持该更改.若要通过引用传递参数, 可使用ref或out关键字.ref和out这两个关键 ...

  4. 绝对好文C#调用C++DLL传递结构体数组的终极解决方案

    C#调用C++DLL传递结构体数组的终极解决方案 时间 2013-09-17 18:40:56 CSDN博客相似文章 (0) 原文  http://blog.csdn.net/xxdddail/art ...

  5. WindowsForm 公共控件 菜单和工具栏

                                                      公共控件   菜单栏 状态栏   布局    公共控件 textbox:  text属性:用于获取或 ...

  6. A==?B(A,B超级大)

    #include <iostream>#include <string.h>#include <cstring>using namespace std;struct ...

  7. 如何截取url中的各个参数?

    在页面跳的时候,目的界面可能会根据url中的某些参数进行数据处理,这个时候如何能快速并设计一个通用的截取url中的参数,并且获取各个参数值? 代码: url = location.search;//获 ...

  8. 打造坚固的安全的Linux服务器(ssh登录篇)

      Nov 3 01:22:06 server sshd[11879]: Failed password for root from 123.127.5.131 port 38917 ssh2Nov ...

  9. MRD-5012型RS232,RS485有源隔离中继模块,采用磁隔离技术,金升阳DC-DC隔离电源,纯硬件自适应方向,速度高达256000bps

    RS485\RS232磁隔离中继模块MRD-5012能够实现232转485或者485转485通信信号的电气隔离,同时提高驱动能力,能够在实现通信信号隔离并且延长通信距离,使485节点可以最大增加到25 ...

  10. Mac OS X:Dock 的附加功能

    Dock 提供了可能并非显而易见的有用控件和菜单.通过修饰键的不同组合(如 Option 键.Control 键)及鼠标点按的不同类型(点按与按下及按住),即可使用下列附加功能.本文适用于 Mac O ...