[英]Delete function of JPArepository in Spring boot does not work , What is the reason?
In the previous tests, I saw that while it was able to delete without any problems, it does not work now.在之前的测试中,我看到虽然它能够毫无问题地删除,但现在不起作用。
This is my entity class:这是我的实体 class:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="subcomments")
public class SubComment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long id;
@Column(name="uid")
public String uid;
@Column (name="name")
public String name;
@Column (name="above_uid")
public String above_uid;
@Column (name="url")
public String url;
@Column(name="coin")
public String coin;
@Column (name="comment")
public String comment;
@Column (name="top_date")
public int top_date;
@Column (name="top_time")
public int top_time;
}
This is my jpa repository:这是我的 jpa 存储库:
public interface SubCommentRepo extends JpaRepository<SubComment , Long>{
// All CRUD database methods
@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 )
public SubComment info(String uid, String above_uid, String coin, String comment,int top_date, int top_time);
}
The info method in the repository is designed to check the existence of the current data in the database.存储库中的 info 方法旨在检查数据库中是否存在当前数据。
This is my rest controller:这是我的 rest controller:
@RestController
@RequestMapping(path="/api/v1/user")
public class ServiceController {
@Autowired
SubCommentRepo subRepo ;
// Delete Sub Comment ***
@DeleteMapping(path="/comment/sub/delete")
public ResponseEntity<String> delete( @RequestBody SubComment smt ) {
if ( subRepo.info(smt.uid, smt.above_uid, smt.coin, smt.comment, smt.top_date, smt.top_time)!= null ) {
subRepo.delete(smt);
return ResponseEntity.status( HttpStatus.OK ).body ( "Deleted" );
} else {
return ResponseEntity.status( HttpStatus.BAD_REQUEST ).body( "Not available already" );
}
}
}
I am using mysql as database.我正在使用 mysql 作为数据库。
I am trying spring boot for the first time.我第一次尝试 spring 引导。 :)
:)
Use deleteById
method instead of the delete
method.使用
deleteById
方法而不是delete
方法。
@RestController
@RequestMapping(path="/api/v1/user")
public class ServiceController {
@Autowired
SubCommentRepo subRepo ;
// Delete Sub Comment ***
@DeleteMapping(path="/comment/sub/delete")
public ResponseEntity<String> delete( @RequestBody SubComment smt ) {
if ( subRepo.info(smt.uid, smt.above_uid, smt.coin, smt.comment, smt.top_date, smt.top_time)!= null ) {
subRepo.deleteById(smt.id);
return ResponseEntity.status( HttpStatus.OK ).body ( "Deleted" );
} else {
return ResponseEntity.status( HttpStatus.BAD_REQUEST ).body( "Not available already" );
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.