2017-05-31 59 views
1

我想删除基于userId的记录但是,当我运行此代码并执行以下查询时,它给了我一个404错误 请帮助我如何删除数据?JPA存储库:使用本机查询从数据库中删除一行

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

显示控制器层的代码。 – Blank

+0

控制器层? @Forward – SFAH

+0

为什么404?我认为这不是关于数据库,它是关于URL映射。 – Blank

回答

0

请确保您的网址马平是如下─

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