星空网 > 软件开发 > Java

Hibernate之dynamic

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

案例代码如下:

1、持久化对象

Hibernate之dynamicHibernate之dynamic
 1 package com.jdw.hibernate.entities; 2  3 import java.util.Date; 4  5 public class News { 6   private Integer id; 7   private String title; 8   private String author; 9   private Date date;10 11   public News() {12     // TODO Auto-generated constructor stub13   }14 15   public News(String title, String author, Date date) {16     super();17     this.title = title;18     this.author = author;19     this.date = date;20   }21 22   @Override23   public String toString() {24     return "News [id=" + id + ", title=" + title + ", author=" + author25         + ", date=" + date + "]";26   }27 28   public Integer getId() {29     return id;30   }31 32   public void setId(Integer id) {33     this.id = id;34   }35 36   public String getTitle() {37     return title;38   }39 40   public void setTitle(String title) {41     this.title = title;42   }43 44   public String getAuthor() {45     return author;46   }47 48   public void setAuthor(String author) {49     this.author = author;50   }51 52   public Date getDate() {53     return date;54   }55 56   public void setDate(Date date) {57     this.date = date;58   }59 }

View Code

2、hbm.

<?

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

关键词:Hibernate

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

海外仓优缺点有哪些:https://www.goluckyvip.com/tag/33239.html
中东贸易风险:https://www.goluckyvip.com/tag/3324.html
海外仓邮费:https://www.goluckyvip.com/tag/33240.html
海外仓有多少:https://www.goluckyvip.com/tag/33241.html
海外仓有何优缺点:https://www.goluckyvip.com/tag/33242.html
海外仓有哪几家:https://www.goluckyvip.com/tag/33243.html
川藏线自驾游要怎么走才比较划算呢?:https://www.vstour.cn/a/411240.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流