Java AutoIncrement Id PostgreSQL 和 Spring Boot 数据 JPA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41791802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AutoIncrement Id PostgreSQL and Spring Boot Data JPA
提问by lbpeppers
I'm having problems trying to create a new record in my PostgreSQL database. I just want to POST to the REST service a new user (int:id, String:email, String:password) but, I'm having this error:
我在尝试在我的 PostgreSQL 数据库中创建新记录时遇到问题。我只想向 REST 服务发布一个新用户 (int:id, String:email, String:password) 但是,我遇到了这个错误:
"exception": "org.springframework.dao.DataIntegrityViolationException",
"message": "could not execute statement; SQL [n/a]; constraint [id]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
These are my Java classes:
这些是我的 Java 类:
Domain
领域
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String email;
private String password;
public User() {}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Controller
控制器
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(method = RequestMethod.GET)
public List<User> findAll() {
return userService.findAll();
}
@RequestMapping(method = RequestMethod.POST)
public User addUser(@RequestBody User user) {
userService.addUser(user);
return user;
}
}
Service
服务
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> findAll() {
return (List<User>) userRepository.findAll();
}
public User addUser(User user) {
userRepository.save(user);
return user;
}
}
Repository
存储库
public interface UserRepository extends CrudRepository<User, Integer> {
// TODO
}
SQL
SQL
CREATE TABLE users(
id INT PRIMARY KEY NOT NULL,
email TEXT NOT NULL,
password CHAR(20) NOT NULL
);
Please, somebody help me, because I don't know how to tackle this issue.
请有人帮助我,因为我不知道如何解决这个问题。
采纳答案by lbpeppers
I found the solution. I need to change the script for these one:
我找到了解决方案。我需要更改这些脚本:
CREATE TABLE users(
id SERIAL PRIMARY KEY NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL
);
Then, the Entity should be annotated with this:
然后,实体应该用以下注释:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "serial")
private Long id;
private String email;
private String password;
public User() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
回答by Prashant Katara
SQL should be like this..
SQL应该是这样的..
CREATE TABLE users(
id INT PRIMARY KEY BIGINT NOT NULL AUTO_INCREMENT,
email TEXT NOT NULL,
password CHAR(20) NOT NULL
);