Hibernate saveOrUpdate命令将现有行的字段更改为NULL [英] Hibernate saveOrUpdate command changes fields of existing row to NULL

查看:1196
本文介绍了Hibernate saveOrUpdate命令将现有行的字段更改为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象作为另一个对象的成员,可以说Object1和Object 2.
对象2在对象1中,并且有3个字段。

我从用户处收到对象1的详细信息。
也ID和从用户对象2的名称和需要保存Object1在DB所以我用
session.saveOrUpdate(object1),



它保存object1并保留用户提供的对象2的文件,并将其他文件更改为NULL。



我是否应该通过Object2的所有文件来避免这个问题,或者是否有办法阻止将这些字段更改为NULL并保留其值的休眠? b
$ b'pre> Object1

ID
名称
@ManyToOne(级联= CascadeType.ALL)
Object2

Object2

ID
名称
年龄

示例



DB



Object2的ID = 13 name = XYZ age = 32

用户输入



Object1 name = Jack

Object2 ID = 13 name = XYZ



保存object1后,object2将如下



Object2 ID = 13 name = XYZ age = NULL
(a)如果Object1已经存在于数据库中,那么检索它:
$
b $ b

  Object1 obj1 = session.find(...); 



(b)否则,如果Object1是新的,那么创建它:

  Object1 obj1 = new Object1(); 


  • 通过调用setter方法填充obj1字段

  • $ b
  • (a)如果Object2已经存在于DB中,则加载它:

      Object2 obj2 = obj1 .getObject2(); //确保object2加载; 
    //如果需要,则延迟加载(例如,一对多)

    (b )else,如果Object2是新的,那么创建它,并将它附加到Object1中:
    $ b

      Object2 obj2 = new Object2(); 
    obj1.setObject2(obj2);


  • 通过调用setter方法填充obj2字段

  • $ b
  • 如果Object1到Object2的关系具有级联更新OR ALL:

      session.saveOrUpdate(OBJ1); 


  • 3a)(或者你在做(3a),但是用(3b)替换它)。


    I have an object as member of another object, lets say Object1 and Object 2. Object 2 is in Object 1, and has 3 fields.

    I receive the details of Object 1 from user. Also ID and name of object 2 from user and need to save the Object1 in the DB so I use session.saveOrUpdate(object1),

    but it saves object1 and keep the fileds of object 2 that user provided and change the other fileds to NULL.

    Should I pass all the fileds of Object2 avoid this problem or is there a way of preventing hibernate of changing those fields to NULL and keep their values ?

    Object1
    
     ID
     name
     @ManyToOne (cascade = CascadeType.ALL)
     Object2
    
    Object2
    
      ID 
      name
      age
    

    Example

    What I have in DB

    Object2 has ID =13 name=XYZ age=32

    User enters

    Object1 name = Jack

    Object2 ID = 13 name=XYZ

    After saving object1, the object2 will be as following

    Object2 ID = 13 name=XYZ age=NULL

    解决方案

    1. (a) If Object1 already exists in DB, then retrieve it:

      Object1 obj1 = session.find(...);
      

      (b) else, if Object1 is new then create it:

      Object1 obj1 = new Object1();
      

    2. Populate obj1 fields, by calling setter methods

    3. (a) If Object2 already exists in DB, then load it:

      Object2 obj2 = obj1.getObject2();  // ensures object2 loaded; 
                                         // does lazy load if required (e.g. one-to-many)
      

      (b) else, if Object2 is new then create it, and attach it to Object1:

      Object2 obj2 = new Object2();
      obj1.setObject2(obj2);
      

    4. Populate obj2 fields, by calling setter methods

    5. If Object1 to Object2 relationship has Cascade UPDATE OR ALL:

      session.saveOrUpdate(obj1);
      

    You're not doing (3a) (or you're doing (3a), but then replacing it with (3b)).

    这篇关于Hibernate saveOrUpdate命令将现有行的字段更改为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆