英文:
Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.STUDENT(STUDENT_ID) [0, NULL, NULL]"; SQL statement:
问题
以下是翻译的内容:
在尝试通过REST Post方法插入Student对象时出现以下错误:
2020-10-08 18:50:08.799 错误 21708 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : 在路径为[]的上下文中为servlet [dispatcherServlet] 提供服务时抛出异常 [请求处理失败;嵌套异常是org.springframework.dao.DataIntegrityViolationException:无法执行语句;SQL [n/a];约束["PRIMARY KEY ON PUBLIC.STUDENT(STUDENT_ID) [0, NULL, NULL];SQL语句:插入到student中 (class_id, student_name, student_id) values (?, ?, ?) [23505-200]];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句],根本原因是
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: 唯一索引或主键违反:"PRIMARY KEY ON PUBLIC.STUDENT(STUDENT_ID) [0, NULL, NULL];SQL 语句:插入到 student 中 (class_id, student_name, student_id) values (?, ?, ?) [23505-200]
Application.properties:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:testdb
hibernate.check_nullability=false
spring.jpa.hibernate.ddl-auto=create
Student.class:
@Entity(name = "STUDENT")
public class Student {
@Id
@Column(name = "Student_Id")
private int studentID;
@Column(name = "Student_Name")
private String studentName;
@ManyToOne(cascade = javax.persistence.CascadeType.ALL)
@JoinColumn(name = "class_id", nullable = true)
private Classs classs;
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Classs getClasss() {
return classs;
}
public void setClasss(Classs studentClass) {
this.classs = studentClass;
}
}
Classs:
@Entity(name = "CLASSS")
public class Classs {
@Id
@Column(name = "Classs_Id")
private int classsId;
@Column(name = "Classs_Name")
private String classsName;
@OneToMany(fetch = FetchType.LAZY)
private List<Student> students;
public int getClasssId() {
return classsId;
}
public void setClasssId(int classsId) {
this.classsId = classsId;
}
public String getClasssName() {
return classsName;
}
public void setClasssName(String classsName) {
this.classsName = classsName;
}
}
StudentService.class:
@Service
public class StudentService {
@Autowired
private StudentRepostry studentRepository;
public List<Student> getAllStudents() {
return studentRepository.findAll();
}
public Optional<Student> findById(Integer id) {
return studentRepository.findById(id);
}
public Student insertStudent(Student student) {
return studentRepository.save(student);
}
public Student updatingStudent(Student student) {
return studentRepository.save(student);
}
public void deleteStudentById(int id) {
studentRepository.deleteById(id);
}
}
ClasssService.class:
@Service
public class ClasssService {
@Autowired
private ClasssRepository classsRepository;
public Classs insertClass(Classs classs) {
return classsRepository.save(classs);
}
}
UniversityRegController.class:
@RestController
@RequestMapping("/university")
public class UniversityRegController {
@Autowired
private StudentService studentService;
@Autowired
private ClasssService classsService;
@GetMapping("/students")
public List<Student> getAllStudentsList() {
return studentService.getAllStudents();
}
@GetMapping("/student/{id}")
public Optional<Student> getStudentById(@PathVariable int id) {
return studentService.findById(id);
}
@PostMapping("/registerStudent")
public Student registerStudent(@RequestBody Student student) {
return studentService.insertStudent(student);
}
@PostMapping("/registerClass")
public Classs registerClass(@RequestBody Classs classs) {
return classsService.insertClass(classs);
}
@PutMapping("/updateStudent")
public Student updateStudent(@RequestBody Student student) {
return studentService.updatingStudent(student);
}
@DeleteMapping("/deleteStudent/{id}")
public void deleteStudentById(@PathVariable int id) {
studentService.deleteStudentById(id);
}
}
URI:http://localhost:8080/university/registerStudent
REST POST方法:
{
"Student": {
"Classs": {
"classsId": 108,
"classsName": "8th Class"
},
"studentID": 11,
"studentName": "Jasdfngid"
}
}
英文:
Getting Below error when trying to insert Student object through REST Post method
2020-10-08 18:50:08.799 ERROR 21708 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["PRIMARY KEY ON PUBLIC.STUDENT(STUDENT_ID) [0, NULL, NULL]"; SQL statement:
insert into student (class_id, student_name, student_id) values (?, ?, ?) [23505-200]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.STUDENT(STUDENT_ID) [0, NULL, NULL]"; SQL statement:
insert into student (class_id, student_name, student_id) values (?, ?, ?) [23505-200]
Application.properties:-
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:testdb
hibernate.check_nullability=false
spring.jpa.hibernate.ddl-auto=create
Student.class
@Entity(name = "STUDENT")
public class Student {
@Id
@Column(name = "Student_Id")
private int studentID;
@Column(name = "Student_Name")
private String studentName;
@ManyToOne(cascade = javax.persistence.CascadeType.ALL)
@JoinColumn(name = "class_id", nullable = true)
private Classs classs;
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Classs getClasss() {
return classs;
}
public void setClasss(Classs studentClass) {
this.classs = studentClass;
}
}
Classs
@Entity(name = "CLASSS")
public class Classs {
@Id
@Column(name = "Classs_Id")
private int classsId;
@Column(name = "Classs_Name")
private String classsName;
@OneToMany(fetch = FetchType.LAZY)
private List<Student> students;
public int getClasssId() {
return classsId;
}
public void setClasssId(int classsId) {
this.classsId = classsId;
}
public String getClasssName() {
return classsName;
}
public void setClasssName(String classsName) {
this.classsName = classsName;
}
}
StudentService.class
@Service
public class StudentService {
@Autowired
private StudentRepostry studentRepository;
public List<Student> getAllStudents() {
return studentRepository.findAll();
}
public Optional<Student> findById(Integer id) {
return studentRepository.findById(id);
}
public Student insertStudent(Student student) {
return studentRepository.save(student);
}
public Student updatingStudent(Student student) {
return studentRepository.save(student);
}
public void deleteStudentById(int id) {
studentRepository.deleteById(id);
}
}
ClasssService.class
@Service
public class ClasssService {
@Autowired
private ClasssRepository classsRepository;
public Classs insertClass(Classs classs) {
return classsRepository.save(classs);
}
}
UniversityRegController.class
@RestController
@RequestMapping("/university")
public class UniversityRegController {
@Autowired
private StudentService studentService;
@Autowired
private ClasssService classsService;
@GetMapping("/students")
public List<Student> getAllStudentsList() {
return studentService.getAllStudents();
}
@GetMapping("/student/{id}")
public Optional<Student> getStudentById(@PathVariable int id) {
return studentService.findById(id);
}
@PostMapping("/registerStudent")
public Student registerStudent(@RequestBody Student student) {
return studentService.insertStudent(student);
}
@PostMapping("/registerClass")
public Classs registerClass(@RequestBody Classs classs) {
return classsService.insertClass(classs);
}
@PutMapping("/updateStudent")
public Student updateStudent(@RequestBody Student student) {
return studentService.updatingStudent(student);
}
@DeleteMapping("/deleteStudent/{id}")
public void deleteStudentById(@PathVariable int id) {
studentService.deleteStudentById(id);
}
}
URI : http://localhost:8080/university/registerStudent
REST POST method:
{
"Student":
{ "Classs":
{
"classsId":108,
"classsName":"8th Class"
},
"studentID": 11,
"studentName": "Jasdfngid"
}
}
答案1
得分: 1
你试图创建具有相同 STUDENT_ID 的两行,而 STUDENT_ID 是一个主键(这意味着在两行上不能相同且不能为空)。
英文:
You tried to create two rows with the same STUDENT_ID that is a PRIMARY KEY (that means it can't be the same on two rows and can't be NULL)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论