Dzień dobry, tworze sobie aplikację RESTową, w której podczas zapisu obiektu do bazy wyskakuje mi wyjątek java.lang.IllegalArgumentException: The given id must not be null!
. Korzystam z adnotacji @GeneratedValue, pomimo prób nie mogę znaleźć błędu w kodzie, ktoś mógłby na ten kod zerknąć?
@Entity
@Table(name = "PERSON_IN_NEED")
public class PersonInNeed implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@NotEmpty
@Column(name="NAME",nullable = false)
private String name;
@NotEmpty
@Column(name="SURNAME",nullable = false)
private String surname;
@NotEmpty
@Column(name="DESCRIPTION",nullable = false)
private String description;
@Column(name = "NUMBER_OF_TODAY_PRAYERS")
private Long numberOfTodayPrayers;
@Column(name = "TOTAL_PRAYERS")
private Long totalPrayers;
//gettery,settery,hashcode,equals,toString
@PostMapping("/person/")
public ResponseEntity<?> savePerson(@RequestBody PersonInNeed personInNeed, UriComponentsBuilder componentsBuilder) {
logger.info("Saving Person: {}", personInNeed);
if(personInNeedService.isPersonExist(personInNeed)) {
logger.error("Unable to save person. Person already exist.");
return new ResponseEntity(new CustomErrorType("Unable to save person. Person already exist."),
HttpStatus.CONFLICT);
}
personInNeedService.savePerson(personInNeed);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(componentsBuilder.path("/api/person/{id}").buildAndExpand(personInNeed.getId()).toUri());
return new ResponseEntity<String>(headers, HttpStatus.CREATED);
create table PERSON_IN_NEED (
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
surname VARCHAR(30) NOT NULL,
description VARCHAR(300) NOT NULL,
number_of_today_prayers VARCHAR(30),
total_prayers VARCHAR(30),
PRIMARY KEY (id),
UNIQUE (id)
);