保存新记录并提供PK时的OneToOne ConstraintViolation [英] OneToOne ConstraintViolation while saving a new Record, PK Provided

查看:84
本文介绍了保存新记录并提供PK时的OneToOne ConstraintViolation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个名为Customers的实体,该实体与该实体Address具有OneToOne关系.

We have an Entity called Customers that has a OneToOne relationship to the Entity Address.

Customer的PK应该手动定义. Address'PK应该被自动定义.

The Customer's PK should be manually defined. The Address' PK should be automatically defined.

因此,在Customer中,我省略了@GeneratedValue,而是手动提供了值.但是,当尝试保存时,出现以下错误:

So, in Customer I omitted the @GeneratedValue and I'm providing is value manually. But, when trying to save I'm getting the following error:

2018-11-07 10:42:17.810 ERROR 1257 --- [nio-8080-exec-2] o.h.i.ExceptionMapperStandardImpl        : HHH000346: Error during managed flush [Validation failed for classes [br.com.customers.entity.Address] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='não pode ser nulo', propertyPath=street, rootBeanClass=class br.com.customers.entity.Address, messageTemplate='{javax.validation.constraints.NotNull.message}'}

问题是提供了address.street,我不知道为什么JPA抱怨它为空...

The problem is that the address.street is being provided and I can't realize why JPA is complaining that it's null...

这是我要保存的JSON正文. (正在正确反序列化,因为Address不为NULL)

Here are the JSON body that I'm trying to save. (It's being deserialized correctly, as, Address is not NULL)

{
    "customer_Id": 50,
    "name": "name",
    "company_name": "company_name",
    "email": "email@provider.com",
    "business_phone": "(00) 1111-2222",
    "mobile_phone": "(00) 1111-2222",
    "document": "123456789",
    "state_registration_number": "ISENTO",
    "state_registration_type": "NO_CONTRIBUTOR",
    "city_registration_number": "ISENTO",
    "classification": "AUTO",
    "address": {
        "street": "STREET NAME",
        "number": "NUMBER",
        "complement": "COMPLEMENT",
        "zip_code": "ZIP_CODE",
        "neighborhood": "NEIGHBORHOOD",
        "city": "CITY",
        "state": "STATE"
    }
}

这里是客户实体:

@Data
@Entity(name = "X_CUSTOMERS")
public class Customer {

    @Id
    private int customer_Id;

    @NotNull
    private String name;

    private String company_name;

    private String email;

    private String business_phone;

    private String mobile_phone;

    @NotNull
    private String document;

    private String state_registration_number;

    private String state_registration_type;

    private String city_registration_number;

    @NotNull
    private String classification;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    @JoinColumn(name = "address_id")
    private Address address;

}

在这里,地址实体:

@Data
@Entity(name = "X_ADDRESS")
public class Address {

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

    @NotNull
    private String street;

    private String number;

    private String complement;

    private String zip_code;

    private String neighborhood;

    private String city;

    private String state;

}

我做错了什么? 谢谢!!!

What Am I doing wrong? Thanks!!!

添加代码确实会保留实体:

Adding the code do persist the entities:

客户资料库:

public interface CustomerRepository extends JpaRepository<Customer, Integer> {

}

要坚持下去:

@RestController
@RequestMapping("/customers")
public class CustomersController {

    private CustomerRepository customerRepository;

    public CustomersController(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    @PostMapping
    public Customer postCustomer(@RequestBody Customer customer) {
        return customerRepository.save(customer);
    }


}

推荐答案

通过阅读Hibernate文档,保存操作仅保留具有自动生成ID的实体.因此,如果您打算自己设置ID,那么您需要的是将您的insert方法更改为persist.并且由于您的客户的ID并非自动生成,因此可能是问题所在.您可以在此博客中了解更多信息.

From reading the Hibernate documentation, the save operation only persist entities with auto generated ids. So, if you intend to set the id yourself, then what you need, is to change your insert method for persist. And since you customer has an id that is not auto generated, maybe this could be the issue. You can read more in this blog.

@PostMapping
public Customer postCustomer(@RequestBody Customer customer) {
    return customerRepository.persist(customer);
}

希望有帮助.

这篇关于保存新记录并提供PK时的OneToOne ConstraintViolation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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