唯一索引或主键冲突:”PUBLIC.STUDENT(STUDENT_ID)”;SQL语句:

huangapple go评论95阅读模式
英文:

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 = &quot;STUDENT&quot;)
public class Student {

	@Id
	@Column(name = &quot;Student_Id&quot;)
	private int studentID;

	@Column(name = &quot;Student_Name&quot;)
	private String studentName;

	@ManyToOne(cascade = javax.persistence.CascadeType.ALL)
	@JoinColumn(name = &quot;class_id&quot;, 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 = &quot;CLASSS&quot;)
public class Classs {

	@Id
	@Column(name = &quot;Classs_Id&quot;)
	private int classsId;
	
	@Column(name = &quot;Classs_Name&quot;)
	private String classsName;
	
	@OneToMany(fetch = FetchType.LAZY)
	private List&lt;Student&gt; 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&lt;Student&gt; getAllStudents() {
		return studentRepository.findAll();
	}

	public Optional&lt;Student&gt; 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(&quot;/university&quot;)
public class UniversityRegController {

	@Autowired
	private StudentService studentService;
	
	@Autowired
	private ClasssService classsService;

	@GetMapping(&quot;/students&quot;)
	public List&lt;Student&gt; getAllStudentsList() {
		return studentService.getAllStudents();
	}
	
	@GetMapping(&quot;/student/{id}&quot;)
	public Optional&lt;Student&gt; getStudentById(@PathVariable int id) {
		return studentService.findById(id);
	}

	@PostMapping(&quot;/registerStudent&quot;)
	public Student registerStudent(@RequestBody Student student) {
		return studentService.insertStudent(student);
	}
	
	@PostMapping(&quot;/registerClass&quot;)
	public Classs registerClass(@RequestBody Classs classs) {
		return classsService.insertClass(classs);
	}
	
	@PutMapping(&quot;/updateStudent&quot;)
	public Student updateStudent(@RequestBody Student student) {
		return studentService.updatingStudent(student);
	}
	
	@DeleteMapping(&quot;/deleteStudent/{id}&quot;)
	public void deleteStudentById(@PathVariable int id) {
		studentService.deleteStudentById(id);
	}
}

URI : http://localhost:8080/university/registerStudent
REST POST method:

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

答案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)

huangapple
  • 本文由 发表于 2020年10月8日 21:32:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64263621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定