Spring微服务POST请求无ID返回Hibernate约束500异常解决方案
问题描述
我正在开发一款支持基础GET、POST、PUT、DELETE操作的微服务,项目为基于MySQL数据库的Spring Maven Java程序。
调用POST接口新增数据时,若请求参数不包含ID会返回如下异常:
"status": 500, "error": "Internal Server Error", "message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
这一现象不符合预期:我在MySQL控制台直接插入不带ID的客户数据时,ID可以正常自增,传ID为null也能插入成功,但通过POST请求传null ID就会报错。
相关业务代码
Service层代码
@Service public class CustomerService { private final CustomerRepository customerRepository; @Autowired public CustomerService(CustomerRepository customerRepository) { this.customerRepository = customerRepository; } public List<Customer> getCostumer() { return customerRepository.findAll(); } public void addNewCustomer(Customer customer) { customerRepository.save(customer); System.out.println(customer); } public void deleteCustomer(Long customerId) { if (!customerRepository.existsById(customerId)) { throw new IllegalStateException("customer with id " +customerId+ " does not exist"); } customerRepository.deleteById(customerId); } }
Controller层代码
@RestController @RequestMapping(path = "api/v1/customer") public class CustomerController { private final CustomerService customerService; @Autowired public CustomerController(CustomerService customerService) { this.customerService = customerService; } @GetMapping public List<Customer> getCostumer() { return customerService.getCostumer(); } @PostMapping public void registerNewCustomer(@RequestBody Customer customer) { customerService.addNewCustomer(customer); } @DeleteMapping(path = "{customerId}") public void deleteCustomer(@PathVariable("customerId") Long customerId) { customerService.deleteCustomer(customerId); } }
Repository与实体类代码
@Repository public interface CustomerRepository extends JpaRepository<Customer, Long> { Optional<Customer> findCustomerById(Long id); } @Entity @Table public class Customer { @Id private String name; private String contactFirstName; private String contactLastName; private String phoneNumber; private String fax; private String city; private String region; private String country; private String zip; private float creditLimit; private String adress_1; private String adress_2; private Long id; public Customer() { } public Customer(String name, String contactFirstName, String contactLastName, String phoneNumber, String fax, String city, String region, String country, String zip, float creditLimit, String adress_1, String adress_2, Long id) { this.name = name; this.contactFirstName = contactFirstName; this.contactLastName = contactLastName; this.phoneNumber = phoneNumber; this.fax = fax; this.city = city; this.region = region; this.country = country; this.zip = zip; this.creditLimit = creditLimit; this.adress_1 = adress_1; this.adress_2 = adress_2; this.id = id; } public Customer(String name, String contactFirstName, String contactLastName, String phoneNumber, String fax, String city, String region, String country, String zip, float creditLimit, String adress_1, String adress_2) { this.name = name; this.contactFirstName = contactFirstName; this.contactLastName = contactLastName; this.phoneNumber = phoneNumber; this.fax = fax; this.city = city; this.region = region; this.country = country; this.zip = zip; this.creditLimit = creditLimit; this.adress_1 = adress_1; this.adress_2 = adress_2; } // 剩余代码为Getter和Setter方法 }
解决方案
问题根源为实体类主键配置错误,正确配置如下:
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "id",unique=true, nullable = false) private Long id;
注:需删除原实体类中加在name字段上的@Id注解
内容的提问来源于stack exchange,提问作者Miku
相关产品推荐
相关产品推荐

