Issue
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 :
@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 :
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.
This is my 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.
I am trying spring boot for the first time. :)
Solution
Use deleteById
method instead of the delete
method.
@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" );
}
}
}
Answered By - Annamalai Palanikumar
Answer Checked By - Mildred Charles (JavaFixing Admin)