diff --git a/readme.md b/readme.md index a68cf5061..73e8a4104 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java index f3ecdb174..bfc75a254 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java @@ -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 pets = new ArrayList<>(); diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java index 92f43e0a9..18bdd11a8 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java @@ -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; + } + } diff --git a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java index 062df4576..35ad94693 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java @@ -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 { @Transactional(readOnly = true) Page findAll(Pageable pageable); + /** + * Delete an owner by id + * @param id id to delete + */ + @Modifying + @Transactional + void deleteById(Integer id); + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4d4784e36..02588f6dc 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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