JAVA-7680 Support deletions for Owners

This commit is contained in:
Justin McCartney 2024-06-28 14:01:20 +01:00
parent cae097bf6f
commit 9686a165bc
5 changed files with 33 additions and 6 deletions

View file

@ -6,6 +6,9 @@
## Running petclinic locally
Petclinic is a [Spring Boot](https://spring.io/guides/gs/spring-boot) application built using [Maven](https://spring.io/guides/gs/maven/). You can build a jar file and run it from the command line (it should work just as well with Java 8, 11 or 17):
## Changes from original repo
+ For Spring Boot 2.6.3 version, added the ability to delete Owners and cascade deletions to other tables
```
git clone https://github.com/spring-projects/spring-petclinic.git

View file

@ -59,7 +59,7 @@ public class Owner extends Person {
@Digits(fraction = 0, integer = 10)
private String telephone;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "owner_id")
@OrderBy("name")
private List<Pet> pets = new ArrayList<>();

View file

@ -15,11 +15,6 @@
*/
package org.springframework.samples.petclinic.owner;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
@ -27,6 +22,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
@ -34,6 +30,10 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
/**
* @author Juergen Hoeller
* @author Ken Krebs
@ -160,4 +160,16 @@ class OwnerController {
return mav;
}
/**
* Delete an owner and related entities by id
* @param ownerId the id to delete
* @return a ModelMap showing the owners list
*/
@DeleteMapping("/owners/{ownerId}")
public ModelAndView deleteOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/ownersList");
this.owners.deleteById(ownerId);
return mav;
}
}

View file

@ -19,6 +19,7 @@ import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
@ -79,4 +80,12 @@ public interface OwnerRepository extends Repository<Owner, Integer> {
@Transactional(readOnly = true)
Page<Owner> findAll(Pageable pageable);
/**
* Delete an owner by id
* @param id id to delete
*/
@Modifying
@Transactional
void deleteById(Integer id);
}

View file

@ -23,3 +23,6 @@ logging.level.org.springframework=INFO
# Maximum time static resources should be cached
spring.resources.cache.cachecontrol.max-age=12h
#spring.h2.console.enabled=true
#spring.h2.console.path=/h2-console