我试图更新一个现有的记录,但是它创建了一个新的记录,即使主键不是空值。而且,主题字段返回空值。我使用的是来自spring framework的CrudRepository。
我希望它在数据库中更新,而不是创建,主题字段应该返回Math、Science或英语。
下面是我的输入表单、控制器、实体、DTO和服务实现。
我做错了什么,因为我的其他用户输入表单使用了相同的代码
edit.html
<form th:method="PUT" th:action="@{~/teacher/edit}" th:object="${grades}">
<input type="hidden" name="id" th:field="*{id}">
<input type="hidden" name="studentID" th:field="*{studentID}">
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="quiz1" class="col-form-label">Quiz 1:</label></div>
<div class="col-xl-3"><input id="quiz1" type="text" th:field="*{quiz1}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="quiz2" class="col-form-label">Quiz 2:</label></div>
<div class="col-xl-3"><input id="quiz2" type="text" th:field="*{quiz2}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="quiz3" class="col-form-label">Quiz 3:</label></div>
<div class="col-xl-3"><input id="quiz3" type="text" th:field="*{quiz3}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="quiz4" class="col-form-label">Quiz 4:</label></div>
<div class="col-xl-3"><input id="quiz4" type="text" th:field="*{quiz4}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="quiz5" class="col-form-label">Quiz 5:</label></div>
<div class="col-xl-3"><input id="quiz5" type="text" th:field="*{quiz5}">
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="hw1" class="col-form-label">Homework 1:</label></div>
<div class="col-xl-3"><input id="hw1" type="text" th:field="*{hw1}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="hw2" class="col-form-label">Homework 2:</label></div>
<div class="col-xl-3"><input id="hw2" type="text" th:field="*{hw2}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="hw3" class="col-form-label">Homework 3:</label></div>
<div class="col-xl-3"><input id="hw3" type="text" th:field="*{hw3}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="hw4" class="col-form-label">Homework 4:</label></div>
<div class="col-xl-3"><input id="hw4" type="text" th:field="*{hw4}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="hw5" class="col-form-label">Homework 5:</label></div>
<div class="col-xl-3"><input id="hw5" type="text" th:field="*{hw5}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="hw6" class="col-form-label">Homework 6:</label></div>
<div class="col-xl-3"><input id="hw6" type="text" th:field="*{hw6}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="hw7" class="col-form-label">Homework 7:</label></div>
<div class="col-xl-3"><input id="hw7" type="text" th:field="*{hw7}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="hw8" class="col-form-label">Homework 8:</label></div>
<div class="col-xl-3"><input id="hw8" type="text" th:field="*{hw8}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="exam1" class="col-form-label">Midterms:</label></div>
<div class="col-xl-3"><input id="exam1" type="text" th:field="*{exam1}"></div>
</div>
<div class="row" style="padding: 10px;">
<div class="col-xl-3"></div>
<div class="col-xl-5"><label for="exam2" class="col-form-label">Finals:</label></div>
<div class="col-xl-3"><input id="exam2" type="text" th:field="*{exam2}"></div>
</div>
<div class="mb-3">
<label for="subject" class="form-label">Subject</label>
<select id="subject" th:field="*{subject}">
<option th:value="Math" th:text="Math"></option>
<option th:value="English" th:text="English"></option>
<option th:value="Science" th:text="Science"></option>
<option selected th:value=null th:text="None"></option>
</select>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</div>
</form>
教师控制器
@Controller
@RequestMapping("teacher")
public class TeacherController {
@Autowired
private AdminService adminService;
@Autowired
private GradesService gradesService;
@GetMapping
public String index(Model model){
model.addAttribute("users", adminService.list());
return "teacher/teacher";
}
@GetMapping("/section")
public String section(Model model){
model.addAttribute("users", adminService.list());
return "teacher/section";
}
@GetMapping("/edit")
public String edit(Model model){
model.addAttribute("users", adminService.list());
return "teacher/viewStudents";
}
@GetMapping("/{id}")
private String getUser(@PathVariable Long id, Model model) {
model.addAttribute("user", adminService.get(id));
model.addAttribute("grades", gradesService.get(id));
return "teacher/edit";
}
@PutMapping("/edit")
private String updateUser(GradesDTO gradesDTO, Model model) {
gradesService.update(gradesDTO);
return edit(model);
}
等级实体
@Entity
@Table(name="grades")
public class Grades {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
@ManyToOne
@JoinColumn(name="student_ID", nullable = false)
private User studentID;
@Column
private String subject;
@Column(columnDefinition = "BIGINT default '0'")
private Long quiz1 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long quiz2 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long quiz3 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long quiz4 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long quiz5 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long hw1 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long hw2 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long hw3 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long hw4 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long hw5 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long hw6 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long hw7 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long hw8 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long exam1 = 0L;
@Column(columnDefinition = "BIGINT default '0'")
private Long exam2 = 0L;
@Column(columnDefinition = "Double default '0.0'")
private Double weightQ = 0D;
@Column(columnDefinition = "Double default '0.0'")
private Double weightD = 0D;
@Column(columnDefinition = "Double default '0.0'")
private Double weightE = 0D;
public Grades(){
}
public Grades(Long id){
this.Id = id;
}
public Grades(GradesDTO gradesDTO){
this.studentID = new User(gradesDTO.getStudentID());
this.quiz1= gradesDTO.getQuiz1();
this.quiz2= gradesDTO.getQuiz2();
this.quiz3= gradesDTO.getQuiz3();
this.quiz4= gradesDTO.getQuiz4();
this.quiz5= gradesDTO.getQuiz5();
this.hw1=gradesDTO.getHw1();
this.hw2=gradesDTO.getHw2();
this.hw3=gradesDTO.getHw3();
this.hw4=gradesDTO.getHw4();
this.hw5=gradesDTO.getHw5();
this.hw6=gradesDTO.getHw6();
this.hw7=gradesDTO.getHw7();
this.hw8=gradesDTO.getHw8();
this.exam1=gradesDTO.getExam1();
this.exam2=gradesDTO.getExam2();
this.subject = gradesDTO.getSubject();
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Long getId() {
return Id;
}
public void setId(Long id) {
Id = id;
}
public User getStudentID() {
return studentID;
}
public void setStudentID(User studentID) {
this.studentID = studentID;
}
public Long getQuiz1() {
return quiz1;
}
public void setQuiz1(Long quiz1) {
this.quiz1 = quiz1;
}
public Long getQuiz2() {
return quiz2;
}
public void setQuiz2(Long quiz2) {
this.quiz2 = quiz2;
}
public Long getQuiz3() {
return quiz3;
}
public void setQuiz3(Long quiz3) {
this.quiz3 = quiz3;
}
public Long getQuiz4() {
return quiz4;
}
public void setQuiz4(Long quiz4) {
this.quiz4 = quiz4;
}
public Long getQuiz5() {
return quiz5;
}
public void setQuiz5(Long quiz5) {
this.quiz5 = quiz5;
}
public Long getHw1() {
return hw1;
}
public void setHw1(Long hw1) {
this.hw1 = hw1;
}
public Long getHw2() {
return hw2;
}
public void setHw2(Long hw2) {
this.hw2 = hw2;
}
public Long getHw3() {
return hw3;
}
public void setHw3(Long hw3) {
this.hw3 = hw3;
}
public Long getHw4() {
return hw4;
}
public void setHw4(Long hw4) {
this.hw4 = hw4;
}
public Long getHw5() {
return hw5;
}
public void setHw5(Long hw5) {
this.hw5 = hw5;
}
public Long getHw6() {
return hw6;
}
public void setHw6(Long hw6) {
this.hw6 = hw6;
}
public Long getHw7() {
return hw7;
}
public void setHw7(Long hw7) {
this.hw7 = hw7;
}
public Long getHw8() {
return hw8;
}
public void setHw8(Long hw8) {
this.hw8 = hw8;
}
public Long getExam1() {
return exam1;
}
public void setExam1(Long exam1) {
this.exam1 = exam1;
}
public Long getExam2() {
return exam2;
}
public void setExam2(Long exam2) {
this.exam2 = exam2;
}
public Double getWeightQ() {
return weightQ;
}
public void setWeightQ(Double weightQ) {
this.weightQ = weightQ;
}
public Double getWeightD() {
return weightD;
}
public void setWeightD(Double weightD) {
this.weightD = weightD;
}
public Double getWeightE() {
return weightE;
}
public void setWeightE(Double weightE) {
this.weightE = weightE;
}
}
级别DTO
public class GradesDTO {
private Long id;
private Long studentID;
private Long quiz1;
private Long quiz2;
private Long quiz3;
private Long quiz4;
private Long quiz5;
private Long hw1;
private Long hw2;
private Long hw3;
private Long hw4;
private Long hw5;
private Long hw6;
private Long hw7;
private Long hw8;
private Long exam1;
private Long exam2;
private Double weightQ;
private Double weightD;
private Double weightE;
private String subject;
public GradesDTO(){
}
public GradesDTO(Long Id, Long quiz1, Long quiz2, Long quiz3, Long quiz4, Long quiz5, Long hw1, Long hw2, Long hw3, Long hw4, Long hw5, Long hw6, Long hw7, Long hw8, Long exam1,
Long exam2, Double weightQ, Double weightD, Double weightE, String subject ){
this.id=Id;
this.quiz1=quiz1;
this.quiz2=quiz2;
this.quiz3=quiz3;
this.quiz4=quiz4;
this.quiz5=quiz5;
this.hw1=hw1;
this.hw2=hw2;
this.hw3=hw3;
this.hw4=hw4;
this.hw5=hw5;
this.hw6=hw6;
this.hw7=hw7;
this.hw8=hw8;
this.exam1=exam1;
this.exam2=exam2;
this.weightQ=weightQ;
this.weightD=weightD;
this.weightE=weightE;
this.subject=subject;
}
public GradesDTO(Grades grades){
this.id = grades.getId();
this.studentID = grades.getStudentID().getId();
this.quiz1 = grades.getQuiz1();
this.quiz2 = grades.getQuiz2();
this.quiz3 = grades.getQuiz3();
this.quiz4 = grades.getQuiz4();
this.quiz5 = grades.getQuiz5();
this.hw1 = grades.getHw1();
this.hw2 = grades.getHw2();
this.hw3 = grades.getHw3();
this.hw4 = grades.getHw4();
this.hw5 = grades.getHw5();
this.hw6 = grades.getHw6();
this.hw7 = grades.getHw7();
this.hw8 = grades.getHw8();
this.exam1 = grades.getExam1();
this.exam2 = grades.getExam2();
this.weightD = grades.getWeightD();
this.weightQ = grades.getWeightQ();
this.weightE = grades.getWeightE();
this.subject = grades.getSubject();
}
public Long getStudentID() {
return studentID;
}
public void setStudentID(Long studentID) {
this.studentID = studentID;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getQuiz1() {
return quiz1;
}
public void setQuiz1(Long quiz1) {
this.quiz1 = quiz1;
}
public Long getQuiz2() {
return quiz2;
}
public void setQuiz2(Long quiz2) {
this.quiz2 = quiz2;
}
public Long getQuiz3() {
return quiz3;
}
public void setQuiz3(Long quiz3) {
this.quiz3 = quiz3;
}
public Long getQuiz4() {
return quiz4;
}
public void setQuiz4(Long quiz4) {
this.quiz4 = quiz4;
}
public Long getQuiz5() {
return quiz5;
}
public void setQuiz5(Long quiz5) {
this.quiz5 = quiz5;
}
public Long getHw1() {
return hw1;
}
public void setHw1(Long hw1) {
this.hw1 = hw1;
}
public Long getHw2() {
return hw2;
}
public void setHw2(Long hw2) {
this.hw2 = hw2;
}
public Long getHw3() {
return hw3;
}
public void setHw3(Long hw3) {
this.hw3 = hw3;
}
public Long getHw4() {
return hw4;
}
public void setHw4(Long hw4) {
this.hw4 = hw4;
}
public Long getHw5() {
return hw5;
}
public void setHw5(Long hw5) {
this.hw5 = hw5;
}
public Long getHw6() {
return hw6;
}
public void setHw6(Long hw6) {
this.hw6 = hw6;
}
public Long getHw7() {
return hw7;
}
public void setHw7(Long hw7) {
this.hw7 = hw7;
}
public Long getHw8() {
return hw8;
}
public void setHw8(Long hw8) {
this.hw8 = hw8;
}
public Long getExam1() {
return exam1;
}
public void setExam1(Long exam1) {
this.exam1 = exam1;
}
public Long getExam2() {
return exam2;
}
public void setExam2(Long exam2) {
this.exam2 = exam2;
}
public Double getWeightQ() {
return weightQ;
}
public void setWeightQ(Double weightQ) {
this.weightQ = weightQ;
}
public Double getWeightD() {
return weightD;
}
public void setWeightD(Double weightD) {
this.weightD = weightD;
}
public Double getWeightE() {
return weightE;
}
public void setWeightE(Double weightE) {
this.weightE = weightE;
}
public String getSubject() {
return subject;
}
public void setSubjectId(String subject) {
this.subject = subject;
}
}
等级服务实施
@Service
public class GradesServiceImpl implements GradesService {
@Autowired
private GradesRepository gradesRepository;
@Autowired
AdminRepository adminRepository;
@Autowired
AdminServiceImpl adminServiceimpl;
@Override
public List<GradesDTO> list() {
return StreamSupport.stream(gradesRepository.findAll().spliterator(), false)
.map(GradesDTO::new)
.collect(Collectors.toList());
}
@Override
public void add(GradesDTO user) {
gradesRepository.save(new Grades(user));
}
@Override
public GradesDTO get(Long id) {
AdminDTO adminDTO = adminServiceimpl.get(id);
User user = new User(adminDTO);
return new GradesDTO(gradesRepository.findById(gradesRepository.findByStudentID(user).getId()).get());
}
@Override
public void update(GradesDTO updatedUser) {
Grades grades = new Grades(updatedUser);
gradesRepository.save(grades);
}
}
1条答案
按热度按时间wr98u20j1#
您没有正确更新数据库中的记录。根据您的代码
您只是尝试将新记录保存到表中,因为JPA不知道它是否已经存在。因此,每次您尝试保存新记录时。
要正确更新任何记录,您需要首先从数据库中查找/获取记录,然后对获取的实体对象进行修改。
现在,无论您是否可以对它调用
save method
,JPA都会自动保存它。