Spring Boot中JPA仓库的删除功能不起作用,原因是什么?

nkhmeac6  于 2022-10-04  发布在  Spring
关注(0)|答案(1)|浏览(479)

在之前的测试中,我看到虽然它能够毫无问题地删除,但现在它不能工作。

这是我的实体类:

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @Entity
  6. @Table(name="subcomments")
  7. public class SubComment {
  8. @Id
  9. @GeneratedValue(strategy = GenerationType.IDENTITY)
  10. public long id;
  11. @Column(name="uid")
  12. public String uid;
  13. @Column (name="name")
  14. public String name;
  15. @Column (name="above_uid")
  16. public String above_uid;
  17. @Column (name="url")
  18. public String url;
  19. @Column(name="coin")
  20. public String coin;
  21. @Column (name="comment")
  22. public String comment;
  23. @Column (name="top_date")
  24. public int top_date;
  25. @Column (name="top_time")
  26. public int top_time;
  27. }

这是我的JPA存储库:

  1. public interface SubCommentRepo extends JpaRepository<SubComment , Long>{
  2. // All CRUD database methods
  3. @Query( value="Select * from subcomments where uid=:uid and above_uid=:above_uid and coin=:coin and comment=:comment and top_date=:top_date and top_time=:top_time" , nativeQuery=true )
  4. public SubComment info(String uid, String above_uid, String coin, String comment,int top_date, int top_time);
  5. }

存储库中的INFO方法用于检查数据库中是否存在当前数据。

这是我的REST控制器:

  1. @RestController
  2. @RequestMapping(path="/api/v1/user")
  3. public class ServiceController {
  4. @Autowired
  5. SubCommentRepo subRepo ;
  6. // Delete Sub Comment***
  7. @DeleteMapping(path="/comment/sub/delete")
  8. public ResponseEntity<String> delete( @RequestBody SubComment smt ) {
  9. if ( subRepo.info(smt.uid, smt.above_uid, smt.coin, smt.comment, smt.top_date, smt.top_time)!= null ) {
  10. subRepo.delete(smt);
  11. return ResponseEntity.status( HttpStatus.OK ).body ( "Deleted" );
  12. } else {
  13. return ResponseEntity.status( HttpStatus.BAD_REQUEST ).body( "Not available already" );
  14. }
  15. }
  16. }

我正在使用MySQL作为数据库。

我是第一次试穿春靴。:)

myss37ts

myss37ts1#

使用deleteById方法而不是delete方法。

  1. @RestController
  2. @RequestMapping(path="/api/v1/user")
  3. public class ServiceController {
  4. @Autowired
  5. SubCommentRepo subRepo ;
  6. // Delete Sub Comment***
  7. @DeleteMapping(path="/comment/sub/delete")
  8. public ResponseEntity<String> delete( @RequestBody SubComment smt ) {
  9. if ( subRepo.info(smt.uid, smt.above_uid, smt.coin, smt.comment, smt.top_date, smt.top_time)!= null ) {
  10. subRepo.deleteById(smt.id);
  11. return ResponseEntity.status( HttpStatus.OK ).body ( "Deleted" );
  12. } else {
  13. return ResponseEntity.status( HttpStatus.BAD_REQUEST ).body( "Not available already" );
  14. }
  15. }
  16. }
展开查看全部

相关问题