2017-05-31 60 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