Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC : PUT and DELETE with a composite key

I'm using Spring Boot to create a web application that persist a FooBar entity with JPA.

I have some html pages that perform AJAX requests on my controllers. The requests are about this Entity :

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

  @EmbeddedId
  private FooBarId id;

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

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

It uses a composite key :

@Embeddable
public class FooBarId {

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

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

The POST is OK :

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

Question :

How can I perform a PUT, GET and DELETE ? I cannot see how can I do it because I'm used to deal with a simple id. So is it possible to perform those operations with a composite key ?

Edit 1:

  • I have no id column in my table. I cannot alter the table.

  • What I tried so far : for a DELETE, I passed the whole entity to the controller and then searched for the entity to delete based on the Key. For the PUT and the GET (single get), I don't know from where to start.

Regards.

like image 801
Marco Avatar asked Jan 31 '26 05:01

Marco


1 Answers

Assuming that you separate the DTO and the Entity. So, in the PUT/DELETE, just put the body to your requests. For the GET, you can try POST to get the data because your id is quite complex with the email and name. So put that id to Body and do the Post to query the data. Here is what I tried:

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

}

--- In Controller add these methods:

@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);
        }

Result as the image

enter image description here

like image 143
Kenny Tai Huynh Avatar answered Feb 01 '26 20:02

Kenny Tai Huynh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!