如何将一个特定属性更新为数据库,而不会将其余属性覆盖为NULL [英] How update one specific property to database,without overwrite to NULL the rest properties

查看:298
本文介绍了如何将一个特定属性更新为数据库,而不会将其余属性覆盖为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新到databgase其中一个属性为UserData,其中包含(名称,街道,城市等)
如此:



关系与放大;数据库属性:
此用户数据与其用户有双向关系,其中使用了User.hbm.xml中的cascade =save-update,因此



用户在DB(id_user,外键: id_UserDetail,状态)中具有此属性

UserData 有(id_UserDetail,外键: id_User和经典信息字段名称,街道,电话等)

我使用这种方法更新数据库:
更新方法

  public void ajaxNameListener (AjaxBehaviorEvent事件){

UserData usrData = new UserData();
userData.setName(name); //我只想更新name属性,但不幸的是,其余的也被覆盖为DB
$ b用户user = BeanFactory
.getHotelDAOService(User.class)
.findbyIdIndetifier(selectedUserId ); // selectedUserId只是我用户列表中的标识符,所以在这里我声明了哪个具体的用户被用户的ID更新

user.setUserData(userData);
BeanFactory.getHotelDAOService(User.class)
.update(user);
}

此方法用作侦听器形式< a4j :> 在我的Jsf页面上,但这没有问题,

我需要修复此方法,以某种方式更新此DB字段我需要和休息一定是不变的,但在这种情况下,我成功地改变了例如字段'name'这个更新字段的名称,特别是Id,但其余的UserData列被覆盖为NULL,



有人可以输入一些建议,如何修改此方法?或者可以根据这种情况编写自己的方法,这会导致只更新这个字段我想要更新的数据库中的NULL,而不是数据库中的NULL,
非常感谢你的回复和回复,
干杯,

解决方案

发生此问题是因为在保存/更新用户的实例时,它没有FK属性的值。因此,FK属性的值将变为空。



为了避免这种情况,您可以按照以下任一方式进行操作:


  1. 不用使用hibernate API更新实例,而是编写一个HQL来只修改要更改的属性。 (例如,在您的情况下名称。)

  2. 从hibernate会话加载用户实例,更改该实例中所需属性的值,更新用户实例。所有这3个操作必须在单个hibernate会话中完成。


i want update to databgase one of properties for UserData which have(name,street,city,etc) so:

Relationship & DB properties: This UserData have a bi-directional relationship with his User where cascade="save-update" on User.hbm.xml is used, so

User have this properties in DB (id_user, Foreign key: id_UserDetail, status)

UserData have (id_UserDetail,Foreign key: id_User, and classic info fields name,street,telephone,etc )

I use this method to update to database: Update method

public void ajaxNameListener(AjaxBehaviorEvent event) {         

             UserData usrData = new UserData();
             userData.setName(name);  // I want update only name property,but unfortunately rest is also overwriten to null in DB

                 User user = BeanFactory
                        .getHotelDAOService(User.class)
                         .findbyIdIndetifier(selectedUserId);   // selectedUserId is just identifier from my list of user,so here i declared which concrete User be updated by Id of Users           

                    user.setUserData(userData);
          BeanFactory.getHotelDAOService(User.class)
                    .update(user);
}

This method is used as listener form <a4j:> on my Jsf page but there is no problem with this,

I need repair this method somehow to update only this DB field what i need and rest must be unchaged, but in this case i succesfully made change for example on field 'name' this update field name by particulary Id, but rest of UserData columns is overwriten to NULL,

Can somebody type me some advice how can be modify this method? or may write own method based on this case, which cause updating only this field what i want update,not rest to NULL in database, Thank a lot for yours post and response, Cheers,

解决方案

This problem occurs because at the time you save/update User's instance, it doesn't have values to its FK properties. So FK properties' values become null.

To avoid this, you can follow either way mentioned below:

  1. Instead of updating the instance using hibernate API, write an HQL to modify only the property you want to change. (e.g. name in your case.)
  2. Load the instance of User from hibernate session, change value of the desired property in that instance, Update the User instance. All these 3 operations must be done in a single hibernate session.

这篇关于如何将一个特定属性更新为数据库,而不会将其余属性覆盖为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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