JPA存储库:使用本机查询从数据库中删除一行

时间:2017-05-31 07:05:31

标签: java mysql sql spring

我想删除基于userId的记录但是当我运行此代码并执行以下查询时,它在404上给出了一个错误 请帮助我如何删除数据?

PropertyReport.java

@Entity 
@Table(uniqueConstraints={
        @UniqueConstraint(columnNames = {"reportedProperty", "reporter"})
    })
public class PropertyReport implements Serializable {

private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToOne (cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, targetEntity = Property.class)
    @JoinColumn(name="reportedProperty")
    private Property property;

    @OneToOne
    @JoinColumn(name="reporter")
    private User user;

    @Column(length=1024)
    private String Report;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Property getProperty() {
        return property;
    }

    public void setProperty(Property property) {
        this.property = property;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getReport() {
        return Report;
    }

    public void setReport(String report) {
        Report = report;
    }
}

PropertyReportReppository.java

public interface PropertyReportRepository extends JpaRepository<PropertyReport, Long>{
@Modifying
    @PreAuthorize("hasAuthority('allRights')")
    @Query("delete from  PropertyReport pr where pr.user.id=:userId")
    int deleteTenantReview(@Param ("userId") Long userId); }

我打电话给的API

API: http://localhost:8555/api/propertyReports/search/removeByUserId/2

1 个答案:

答案 0 :(得分:0)

请确保您的网址maping如下 -

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class DemoController {

  @RequestMapping("/api/propertyReports/search/removeByUse‌​rId/{userId}")
  public String hello(@PathVariable(value = "userId") final Long userId){

//add method call here to delete your data that you want
    return "Hello World";
  }
}

有关如何创建网址映射click here

的更多详细信息