2017-08-24 42 views
1

我正在使用Spring Boot創建一個Web應用程序,該應用程序使用JPA保持FooBar實體。Spring MVC:使用複合鍵輸入和刪除

我有一些html頁面在我的控制器上執行AJAX請求。 的請求是這個實體:

@Entity 
@Table(name = "FOO_TABLE") 
public class FooBar { 

    @EmbeddedId 
    private FooBarId id; 

    @Column(name = "ADDRESS") 
    private String address; 

    @Column(name = "COLOR") 
    private String color; 
} 

它採用了複合鍵:

@Embeddable 
public class FooBarId { 

    @NotNull 
    @Column(name = "NAME") 
    private String name; 

    @NotNull 
    @Column(name = "TXT_ADR_MAIL") 
    private String email; 
} 

POST是OK:

@PostMapping 
public ResponseEntity<Void> postFoobar(FooBar fb){ 
    repo.save(fb) 
    return new ResponseEntity<>(HttpStatus.CREATED); 
} 

問:

我怎麼能執行一個PUT,GETDELETE?我看不到我該怎麼做,因爲我習慣於處理簡單的id。 那麼有可能使用複合鍵執行這些操作?

編輯1:

  • 我在表中沒有id列。我無法改變桌子。

  • 我到目前爲止的嘗試:對於DELETE,我將整個實體傳遞給控制器​​,然後根據密鑰搜索要刪除的實體。 對於PUTGET(單個獲得),我不知道從哪裏開始。

問候。

+0

你嘗試了什麼? – Nikolay

+0

@尼古拉請參考我的編輯。 – Marco

回答

0

假設您將DTO和實體分開。因此,在PUT/DELETE中,只需將正文放到您的請求中。對於GET,您可以嘗試POST來獲取數據,因爲您的ID與電子郵件和名稱相當複雜。所以把這個id放到Body中,然後用Post來查詢數據。這裏是我的嘗試:

--FooBarDTO

public class FooBarDTO { 
    private String name; 
    private String email; 
    private String address; 
    private String color; 

    public FooBarDTO(){} 
    /** 
    * @param name 
    * @param email 
    * @param id 
    * @param address 
    * @param color 
    */ 
    public FooBarDTO(String name, String email, String address, String color) { 
     this.name = name; 
     this.email = email; 
     this.address = address; 
     this.color = color; 
    } 
    /** 
    * @return the name 
    */ 
    public String getName() { 
     return name; 
    } 
    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 
    /** 
    * @return the email 
    */ 
    public String getEmail() { 
     return email; 
    } 
    /** 
    * @param email the email to set 
    */ 
    public void setEmail(String email) { 
     this.email = email; 
    } 
    /** 
    * @return the address 
    */ 
    public String getAddress() { 
     return address; 
    } 
    /** 
    * @param address the address to set 
    */ 
    public void setAddress(String address) { 
     this.address = address; 
    } 
    /** 
    * @return the color 
    */ 
    public String getColor() { 
     return color; 
    } 
    /** 
    * @param color the color to set 
    */ 
    public void setColor(String color) { 
     this.color = color; 
    } 


} 

--FooBarIdDTO

public class FooBarIdDTO { 
    private String name; 
    private String email; 
    /** 
    * @return the name 
    */ 
    public String getName() { 
     return name; 
    } 
    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 
    /** 
    * @return the email 
    */ 
    public String getEmail() { 
     return email; 
    } 
    /** 
    * @param email the email to set 
    */ 
    public void setEmail(String email) { 
     this.email = email; 
    } 

} 

---在控制器中添加這些方法:

@Autowired 
    private FooBarRepository repo; 

    // test FooBar 
    @RequestMapping(value = "/foo", method = RequestMethod.POST) 
    public ResponseEntity<?> postFoo(@RequestBody FooBarDTO body){ 
     FooBarId id = new FooBarId(); 
     id.setEmail(body.getEmail()); 
     id.setName(body.getName()); 

     FooBar fooBar = new FooBar(); 
     fooBar.setId(id); 
     fooBar.setAddress(body.getAddress()); 
     fooBar.setColor(body.getColor()); 
     repo.save(fooBar); 
     return new ResponseEntity<>(HttpStatus.OK); 
    } 

    //test PUT 
    @RequestMapping(value = "/foo", method = RequestMethod.PUT) 
    public ResponseEntity<?> putFoo(@RequestBody FooBarDTO body){ 
     FooBarId id = new FooBarId(); 
     id.setEmail(body.getEmail()); 
     id.setName(body.getName()); 

     FooBar fooBar = new FooBar(); 
     fooBar.setId(id); 
     fooBar.setAddress(body.getAddress()); 
     fooBar.setColor(body.getColor()); 
     repo.save(fooBar); 
     return new ResponseEntity<>(HttpStatus.OK); 
    } 

    //test Delete FooBar 
    @RequestMapping(value = "/foo", method = RequestMethod.DELETE) 
    public ResponseEntity<?> deleteFoo(@RequestBody FooBarIdDTO body){ 
     FooBarId id = new FooBarId(); 
     id.setEmail(body.getEmail()); 
     id.setName(body.getName()); 
     repo.delete(id); 
     return new ResponseEntity<>(HttpStatus.OK); 
    } 

    // test FooBar 
     @RequestMapping(value = "/getFoo", method = RequestMethod.POST) 
     public ResponseEntity<?> getFoo(@RequestBody FooBarIdDTO body){ 
      FooBarId id = new FooBarId(); 
      id.setEmail(body.getEmail()); 
      id.setName(body.getName()); 
      FooBar result = repo.findOne(id); 
      return ResponseEntity.ok(result); 
     } 

結果作爲圖像

enter image description here