唯一索引或主键冲突:"PRIMARY KEY ON PUBLIC.STUDENT(STUDENT_ID) [0, NULL, NULL]" ;SQL语句:



尝试通过REST Post方法插入Student对象时出现Getting Below错误

2020-10-08 18:50:08.799错误21708-[nio-8080-exec-7]o.a.c.c.[.[./].[dispatcherServlet]:路径为[]的上下文中Servlet[dispatcherServlet的Servlet.service((引发异常[请求处理失败;嵌套异常为org.springframework.dao.DataIntegrityViolationException:无法执行语句;SQL[n/a];constraint["PRIMARY KEY ON PUBLIC.STUDENT(STUDENT_ID([0,NULL,NULL]";;SQL语句:在student(class_id,student_name,student_id(中插入值(?,?,?([23505-200]];嵌套的异常是org.hubinate.exception.ConstraintViolationException:无法执行语句],根本原因是

org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException:唯一索引或主键冲突:"公钥上的主键STUDENT(STUDENT_ID([0,NULL,NULL]";;SQL语句:插入学生(class_id,student_name,student_id(值(?,?,?([23505-200]

应用程序属性:-

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

学生.等级

@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;
}
}

分类

@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);

}
}

ClasssServices.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/registerStudentREST POST方法:

{
"Student": 
{     "Classs":
{
"classsId":108,
"classsName":"8th Class"
},
"studentID": 11,
"studentName": "Jasdfngid"
}
}

您试图创建两个具有相同STUDENT_ID(PRIMARY KEY(的行(这意味着它在两行上不能相同,也不能为NULL(

最新更新