通过字段中的设置进行补丁映射对象更新的最佳方法

发布于 2025-02-02 06:39:17 字数 945 浏览 3 评论 0原文

在这样的对象上进行补丁映射的最佳方法是什么:

@Data
public class UnifiedOfferEntity {
    private String companyName;
    private String city;
    private String title;
    private Set<SkillEntity> skills;

}

在实体中,我有很多经营。我做了更新的方法,以发送映射&lt; fieldName,value&gt;它可以正常工作,直到我试图将其设置为请求主体为止。

public UnifiedOfferEntity patchEntity(String id, Map<Object, Object> fields) {
    UnifiedOfferEntity unifiedOfferEntity = getEntityById(id);
    fields.forEach((key, value) -> {
        Field field = ReflectionUtils.findField(UnifiedOfferEntity.class, (String) key);
        field.setAccessible(true);
        ReflectionUtils.setField(field, unifiedOfferEntity, value);
    });
    return unifiedOfferEntity;
}

我得到:

java.lang.IllegalArgumentException: Can not set java.util.Set field com.example.jobfinder.entity.UnifiedOfferEntity.seniority to java.util.ArrayList

What is the best way to do patch mapping on object like this:

@Data
public class UnifiedOfferEntity {
    private String companyName;
    private String city;
    private String title;
    private Set<SkillEntity> skills;

}

In entity I have many to many realtion. I did method for update where I send map<fieldName, value> and it works properly until I am trying to put set to the request body.

public UnifiedOfferEntity patchEntity(String id, Map<Object, Object> fields) {
    UnifiedOfferEntity unifiedOfferEntity = getEntityById(id);
    fields.forEach((key, value) -> {
        Field field = ReflectionUtils.findField(UnifiedOfferEntity.class, (String) key);
        field.setAccessible(true);
        ReflectionUtils.setField(field, unifiedOfferEntity, value);
    });
    return unifiedOfferEntity;
}

I am getting:

java.lang.IllegalArgumentException: Can not set java.util.Set field com.example.jobfinder.entity.UnifiedOfferEntity.seniority to java.util.ArrayList

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶情人眼里出诗心の 2025-02-09 06:39:17

您是否在这种方法中考虑过:

 public UnifiedOfferEntity patchEntity(String id, Map<Object, Object> fields) {
        UnifiedOfferEntity unifiedOfferEntity = getEntityById(id);
        enhanceUnitedOfferEntity(unifiedOfferEntity,fields);
    
        return unifiedOfferEntity;
    }

该方法将根据键作为常数手动填充OBJET ..或另一个对象...

 private void enhanceUnitedOfferEntity(UnifiedOfferEntity unifiedOfferEntity, Map<Object, Object> fields){
    fields.forEach((k, v) ->
      switch(k) {
        case "COMPANY_NAME":
          unifiedOfferEntity.setCompanyName(v);
        case "CITY":
          unifiedOfferEntity.setCity(v);
        case "TITLE":
          unifiedOfferEntity.setTitle(v);
          break;
        case "SKILLS":
          unifiedOfferEntity.setSkills(v);
          break;
        default:
      });
    }

have you thought in this approach:

 public UnifiedOfferEntity patchEntity(String id, Map<Object, Object> fields) {
        UnifiedOfferEntity unifiedOfferEntity = getEntityById(id);
        enhanceUnitedOfferEntity(unifiedOfferEntity,fields);
    
        return unifiedOfferEntity;
    }

where the method would manually populate the objet based on keys as constants.. or another object...

 private void enhanceUnitedOfferEntity(UnifiedOfferEntity unifiedOfferEntity, Map<Object, Object> fields){
    fields.forEach((k, v) ->
      switch(k) {
        case "COMPANY_NAME":
          unifiedOfferEntity.setCompanyName(v);
        case "CITY":
          unifiedOfferEntity.setCity(v);
        case "TITLE":
          unifiedOfferEntity.setTitle(v);
          break;
        case "SKILLS":
          unifiedOfferEntity.setSkills(v);
          break;
        default:
      });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文