So, im trying to write a microservice with basic GET POST PUT DELETE commands. Its a Spring Maven Java programm with a mysql server
When I try to post something it works, however if I dont include the Id it returns an exception:
"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",
This is weird, because in the mysql console I can Insert a customer without the id, it just autoincrements. Also null works too. Posting however with the Id being null doesn't work
Here some pieces of my code:
@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);
}
}
@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
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;
}
//the rest of the code is just getters and setters
SOLVED solution is:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id",unique=true, nullable = false)
private Long id;
thanks guys!