After first starting the api, the first 3 user creation Post requests fail (screenshot at bottom of post) with the below error (unique constraint vilocation).
Subsequent requests work, with the first created user having an id of 4, then 5, etc...
How can I make the user creation work on the first (3) tries?
I suspect this relates to the pre-seeding of my users, which I'm doing with the below script. Possibly the auto ID generation first tries 1,2,3 -- which are already in use?
INSERT INTO user
VALUES (1, 'user1', 'pass1', 'ADMIN');
INSERT INTO user
VALUES (2, 'user2', 'pass2', 'USER');
INSERT INTO user
VALUES (3, 'user3', 'pass3', 'ADMIN')
could not execute statement; SQL [n/a]; constraint [\"PRIMARY KEY ON PUBLIC.USER(ID)\"; SQL statement:\ninsert into user (name, password, role, id) values (?, ?, ?, ?) [23505-196]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
@RestController
public class UserResource {
@Autowired
private UserRepository userRepository;
@GetMapping("/users")
public List<User> retrievaAllUsers() {
return userRepository.findAll();
}
@DeleteMapping("/users/{id}")
public void deleteUser(@PathVariable Long id) {
userRepository.deleteById(id);
}
@PostMapping("/users")
public ResponseEntity<Object> createUser(@RequestBody User user) {
User savedUser = userRepository.save(user);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(savedUser.getId())
.toUri();
return ResponseEntity.created(location).build();
}
}
-
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private String password;
@Enumerated(EnumType.STRING)
private Role role;
public User() {
super();
}
public User(Long id, String name, String password, Role role) {
this.id = id;
this.name = name;
this.password = password;
this.role = role;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
edit - added role class
public enum Role { USER, ADMIN}

Roleclass ?GenerationType.AUTO@benjaminc . Both same.