弹簧引导访问用户更新更新仅仅特定领域

0

的问题

所以我遇到这个问题与更新的实体数据库。 同时穿整个实体和更新,只有特定的领域对待触及的领域的空,其结果我得到的一个例外,因为这些领域是 @Not-Null,

我已经尝试过寻找类似的问题,但不能解决我的问题。

公司实体:

@Entity
@Table (name = "companies")
@Data
@ToString(exclude = "perfumes")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotNull
    private String name;

    @NotNull
    @Email(message = "Wrong input. please enter a VALID email address")
    private String email;

    @NotNull
    @Size(min = 4, max = 14, message = "Password range must be between 4 - 14 digits")
    private String password;

    @NotNull
    @Enumerated(EnumType.STRING)
    private Country country;

    @Singular
    @OneToMany(mappedBy = "company", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Perfume> perfumes = new ArrayList<>();
}

大多数领域都是 @NotNull 为创造,但是,我需要更新的实体,有时只有特定的领域。

服务:

@Override
public String updateCompany(int id, Company company) throws DoesNotExistException {
    if(!companyRepository.existsById(id))
    {
        throw new DoesNotExistException(id);
    }

    companyRepository.saveAndFlush(company);
    return company.getName() + " has been UPDATED";
}

正如你可以看到一个 ENTITY 已经通过这会导致其余的属性可以自动空,如果不修改。

控制器:

  @PutMapping("/updateCompany/{id}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public String updateCompany(@PathVariable int id, @RequestBody Company company) throws DoesNotExistException {
        return admin.updateCompany(id,company);
    }

例外:

Validation failed for classes [com.golden.scent.beans.Company] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=password, rootBeanClass=class com.golden.scent.beans.Company, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]

谢谢。

hibernate java jpa
2021-11-21 18:38:43
1

最好的答案

0

控制器具有约束力的价值你传递到一个新的公司实体。 新的实体不是附着于持久性方面,它不具备国家预先存在的实体。 当你保存它访问用户认为你想要空出来的所有领域的你没有价值。

相反,你可以控制结合其参数也可与检. 然后在服务的你看看现有的客户,用findById和复制领域的你想要的更新也可与检的实体。 然后打电话给saveAndFlush传递更新的实体。

它看起来像是有改进也可与检,可以使用aJsonPatch举行更新过去了,看看 https://www.baeldung.com/spring-rest-json-patch. 修补方法似乎是一个更好的匹配你在做什么无论如何。

在服务器上的重要事情就是看看现有的实体让你有一个实体,是附着于持久性方面和具有其所有领域的电流。

2021-11-21 20:00:13

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................