`

032_CoreAPI_Update

 
阅读更多

1.什么时候用update

1.可以更新一个detached对象,使其更新数据库,变为persistent

 

代码案例:

@Test

public void TestUpdate1(){

Session session = sf.getCurrentSession();

session.beginTransaction();

Teacher t = (Teacher)session.get(Teacher.class, 2);

session.getTransaction().commit();

/*System.out.println("-----detached---------");*/

 

t.setName("zhuhw1111");

Session session2 = sf.getCurrentSession();

session2.beginTransaction();

session2.update(t);

System.out.println("-----persistence---------");

session2.getTransaction().commit();

 

 

}

 

 

 

 

update星期二, 一月 05, 2016

 

vi. update

1.用来更新detached对象,更新完成后转为persist状态

2.更新transient对象会报错

3.更新自己手动设定id的transient对象可以(前提是数据库里有这条记录)

4.persistence状态的对象只要设定不同字段就会发生更新

5.更新部分更改的字段

   a)xml设定property标签的update属性;annotation设定@Column的updatable属性,

不过这种方式很少用,因为不灵活;

   b)使用xml中dynamic-update,JPA1.0 Annotation没有对应的属性,hibernate扩展?

      i.同一个session可以,跨session不行,不过可以用merge()(不重要)

   c)使用HQL(EJBQL)(建议)

 

 

第二种情况

Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

 

因为找不到id。

 

第三种情况

@Test

public void TestUpdate3(){

/*3.更新自己手动设定id的transient对象可以(前提是数据库里这条记录)*/

Teacher t = new Teacher();

t.setId(1);

t.setName("yuzhoua111");

Session session2 = sf.getCurrentSession();

session2.beginTransaction();

session2.update(t);

System.out.println("-----persistence---------");

session2.getTransaction().commit();

 

 

}

 

运行结果ok

 

四、

代码案例:

@Test

public void TestUpdate4(){

Session session = sf.getCurrentSession();

session.beginTransaction();

Teacher t = (Teacher)session.get(Teacher.class, 2);

            //4.persistence状态的对象只要设定不同字段就会发生更新

t.setName("dooder");

session.getTransaction().commit();

}

 

 

虽然看起来没执行update语句,但是t.setName("dooder");改变了缓存中的name数据;

在事务在提交,或是session被关闭的时候

session中保存的对象是否和数据库保存一致,如果不一致,会自动的执行update语句。

对应的数据库中的数据也要做update。

但是同上面三种是一样的,也是做了全部的更新。效率低。

 

运行结果:

执行了update

 

 update

        Teacher 

    set

        birthdate=?,

        _name=?,

        title=?,

        yourWifeName=?,

        zhicheng=? 

    where

        id=?

 

 

五.在Teacher类中加入不更新的注解

@Column(updatable=false)

 

代码案例:

@Test

public void TestUpdate5(){

Session session = sf.getCurrentSession();

session.beginTransaction();

Teacher t = (Teacher)session.get(Teacher.class, 2);

//4.persistence状态的对象只要设定不同字段就会发生更新

t.setName("dooder_zhuhw");

session.getTransaction().commit();

}

 

在不想更新的类中的字段加上注解

@Column(updatable=false)

public String getTitle() {

return title;

执行结果:

 

不加之前

    update

        Teacher 

    set

        birthdate=?,

        _name=?,

        title=?,

        yourWifeName=?,

        zhicheng=? 

    where

        id=?

 

 

在 title加上注解后,执行结果:

  update

        Teacher 

    set

        birthdate=?,

        _name=?,

        yourWifeName=?,

        zhicheng=? 

    where

        id=?

 

在xml对应的配置是,在property update="true|false"

 

 

<hibernate-mapping >

<class name="com.zhuhw.hibernate.model.Student" dynamic-update="true" >

在同一个session里面,在更新完,会发出update语句了。而且只更新改到的东西。

 

如果跨session时。可以不听。

0
1
分享到:
评论

相关推荐

    LibraryApp:ASP.NET Core API示例

    带有ASP.NET Core(3.1)API的示例API 它包含基本的异步HTTP方法: 得到 邮政 放 删除 发展 库API使用的是PostgreSQL。 对于本地开发环境,请运行以下docker命令: docker run --name local-postgres -e POSTGRES...

    Nucleus-Vue启动应用程序模板,该模板在后端使用ASP.NET Core API分层体系结构并基于JWT进行身份验证-Vue.js开发

    本地环境选择Nucleus.Web.Api项目“设置为启动项目”打开“软件包管理器控制台”,然后将默认项目选择为src / Nucleus.EntityFramework运行update-database命令以创建数据库。 运行(F5或CTRL + F5)Web API项目冷杉

    Delphi7.1 Update

    =====================================================CONTENTS * INSTALLING THIS UPDATE * UPDATING LOCALIZED VERSIONS OF DELPHI 7 * KNOWN ISSUES * ISSUES ADDRESSED BY THIS UPDATE - IDE - CORE DATABASE...

    ASP.NET Core Succinctly

    In ASP.NET Core Succinctly, seasoned authors Simone Chiaretta and Ugo Lattanzi update you on all the advances provided by Microsoft’s landmark framework. By learning the foundations of the library ...

    Nucleus:Vue启动应用程序模板,该模板在后端使用ASP.NET Core API分层体系结构并基于JWT进行身份验证

    运行update-database命令创建数据库。 首先运行(F5或CTRL + F5)Web API项目 在src/Nucleus.Web.Vue位置运行yarn命令以安装npm软件包。 运行yarn serve命令以运行Vue应用程序。 管理员用户名和密码: admin/123...

    Java邮件开发Fundamentals of the JavaMail API

    Fundamentals of the JavaMail API Presented by developerWorks, your source for great tutorials ibm.com/developerWorks Table of Contents If you're viewing this document online, you can click ...

    BakdelarWebShop:网上商城

    项目框架版本:Bakdelar版本.Net5.0 Bakdelar_API版本.Net Core3.1 DataAccess版本.Net Core3.1 步骤1.最初执行命令:-add-migration initialcreate-update-database您需要执行两次此命令,一次是针对Bakdelar项目...

    intel的tbb库,版本是2018 update5

    版本是2018 update5。TBB,Thread Building Blocks,线程构建模块,是Intel公司开发的并行编程开发的工具。 OSCON 上,Intel 宣布,Threading Building Blocks,Intel 众多软件开发工具中的一个,open source了。协议...

    fast_method_source:检索方法和过程的源和注释

    快速方法源 存储库: : API说明: : 描述Fast Method Source 是一个 Ruby 库,用于查询方法、过程和 lambda 的源代码和注释。 require 'fast_method_source'require 'fast_method_source/core_ext' # Adds #source ...

    soiree-core-api:Soiree geosocial 服务的 RESTful API

    Soiree geosocial 服务的 RESTful API ##路线 ###公用事业 'get /' : 'UtilityController.heartbeat', 'get /docs' : 'UtilityController.docs', ###事件 'get /events' : 'EventController.index', 'get /...

    fastapi-template

    ├── alembic│ ├── env.py│ └── versions├── alembic.ini├── app│ ├── api # API endpoints│ ├── core│ ├── crud # CRUD (Create, Read, Update, Delete) utils│ ├── db│

    boilerplate:使用.NET Core构建Restful API的简单示例

    这是使用构建Restful API的裸机样板。 宬 (框架) (ORM) (用户身份验证) (HTTP身份验证) 快速开始 跑步 git clone https://github.com/flipboxstudio/boilerplate.git cd boilerplate/App dotnet restore...

    CoreWebApi通用框架.rar

    它负责数据的CRUD(Create, Read, Update, Delete) service layer是业务逻辑层, 它常常需要访问repository层。有网友这么说:Repository(仓储):协调领域和数据映射层,利用类似与集合的接口来访问领域对象。...

    Professional C# 7 and .NET Core 2.0

    Chapter 32 Web Api PART IV: Apps Chapter 33 Windows Apps Chapter 34 Patterns With Xaml Apps Chapter 35 Styling Windows Apps Chapter 36 Advanced Windows Apps Chapter 37 Xamarin.Forms

    RAD Studio 10.2 Toyko Update2 10.2.2 KEYGEN 亲测可用

    Borland.Studio.ToolsAPI.dll, 25.0.28979.1978, D:\Program Files (x86)\Embarcadero\Studio\19.0\bin\ Borland.Studio.Vcl.Design.Refactoring.dll, 25.0.28979.1978, D:\Program Files (x86)\Embarcadero\Studio\...

    HGE_系列教材(1-9)

    3)技术优势--- 基于Direct3D API 有较好的性能和特性 4)免费--- 对于个人或者商业用户都免费,遵循zlib/libpng license 5)代码高度的一致性--- 代码是否具有一致性,是衡量代码质量的标准之一(《Co de Reading: ...

    skein加密解密C源码

    skein.c The main Skein interface functions: Init, Update, and Final, for all three Skein block sizes. Additionally, the InitExt() function allows for MAC and other extended functionality. skein_...

    ZendFramework中文文档

    4.3.1. Zend_Cache_Core 4.3.1.1. 简介 4.3.1.2. 可用选项 4.3.1.3. 例子 4.3.2. Zend_Cache_Frontend_Output 4.3.2.1. 简介 4.3.2.2. 可用的选项 4.3.2.3. 例子 4.3.3. Zend_Cache_Frontend_Function 4.3....

    DC6008_irda_manager驱动程序和APK_20150914_1424.7z

    make update-api 再输入指令 make 然后生成img烧录系统中,打开logcat 用命令查看service list 是否有irda这个service。 Irda是老的三星遥控器接口,现在很多第三方软件都是用此接口。具体在使用上可能还是会有些...

    AtomGraphics:OpenGL,CoreGraphics,Metal,Skia实现的跨平台图形框架

    作者开发AtomGraphics的初衷是基于C/C++开发一套能跨平台绘图的API,能充分利用各个平台的特性,实现高流畅度、低内存消耗的的画图SDK 工程的雏形(prototype)参考了cocos2d,有的代码来自其开源代码,后边会逐一声明...

Global site tag (gtag.js) - Google Analytics