【问题标题】:JPA Repository :Delete a row from database using native queryJPA 存储库:使用本机查询从数据库中删除一行
【发布时间】:2017-05-31 07:05:31
【问题描述】:

我想根据 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

【问题讨论】:

  • 显示你的控制器层代码。
  • 控制器层? @转发
  • 为什么是 404?我认为这不是关于 db,而是关于 url 映射。
  • 404 错误与 JPA 无关。这是来自您的网络服务器的错误,表明您尝试访问的 URL 未映射到任何资源。
  • 我也不知道为什么 404 这就是我问的原因

标签: java mysql sql spring


【解决方案1】:

请确保您的 url 映射如下-

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";
  }
}

详细了解如何创建 url 映射click here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-18
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多