From 3c3fd7ae0b980cb63235a6c7dca304b2b771b25c Mon Sep 17 00:00:00 2001 From: Vitaliy Fedoriv Date: Sat, 12 Nov 2016 22:37:13 +0200 Subject: [PATCH] override delete() method --- .../PetTypeRepositoryExtOverride.java | 30 +++++++++ .../SpecialtyRepositoryExtOverride.java | 30 +++++++++ .../SpringDataPetTypeRepositoryExt.java | 23 ++++++- .../SpringDataPetTypeRepositoryExtImpl.java | 61 +++++++++++++++++++ .../SpringDataSpecialtyRepositoryExt.java | 24 +++++++- .../SpringDataSpecialtyRepositoryExtImpl.java | 46 ++++++++++++++ 6 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/PetTypeRepositoryExtOverride.java create mode 100644 src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpecialtyRepositoryExtOverride.java create mode 100644 src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetTypeRepositoryExtImpl.java create mode 100644 src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataSpecialtyRepositoryExtImpl.java diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/PetTypeRepositoryExtOverride.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/PetTypeRepositoryExtOverride.java new file mode 100644 index 000000000..8bdf224b2 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/PetTypeRepositoryExtOverride.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.samples.petclinic.repository.springdatajpa; + +import org.springframework.samples.petclinic.model.PetType; + +/** + * @author Vitaliy Fedoriv + * + */ + +public interface PetTypeRepositoryExtOverride { + + public void delete(PetType petType); + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpecialtyRepositoryExtOverride.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpecialtyRepositoryExtOverride.java new file mode 100644 index 000000000..6aed4fd50 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpecialtyRepositoryExtOverride.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.samples.petclinic.repository.springdatajpa; + +import org.springframework.samples.petclinic.model.Specialty; + +/** + * @author Vitaliy Fedoriv + * + */ + +public interface SpecialtyRepositoryExtOverride { + + public void delete(Specialty specialty); + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetTypeRepositoryExt.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetTypeRepositoryExt.java index 02fb6e409..206333c00 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetTypeRepositoryExt.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetTypeRepositoryExt.java @@ -1,3 +1,19 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.samples.petclinic.repository.springdatajpa; import org.springframework.beans.factory.annotation.Qualifier; @@ -5,7 +21,12 @@ import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.PetType; import org.springframework.samples.petclinic.repository.PetTypeRepositoryExt; +/** + * @author Vitaliy Fedoriv + * + */ + @Qualifier("PetTypeRepositoryExt") -public interface SpringDataPetTypeRepositoryExt extends PetTypeRepositoryExt, Repository { +public interface SpringDataPetTypeRepositoryExt extends PetTypeRepositoryExt, Repository, PetTypeRepositoryExtOverride { } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetTypeRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetTypeRepositoryExtImpl.java new file mode 100644 index 000000000..7fad3b12f --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataPetTypeRepositoryExtImpl.java @@ -0,0 +1,61 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.samples.petclinic.repository.springdatajpa; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.springframework.samples.petclinic.model.Pet; +import org.springframework.samples.petclinic.model.PetType; +import org.springframework.samples.petclinic.model.Visit; + +/** + * @author Vitaliy Fedoriv + * + */ + +public class SpringDataPetTypeRepositoryExtImpl implements PetTypeRepositoryExtOverride { + + @PersistenceContext + private EntityManager em; + + @SuppressWarnings("unchecked") + @Override + public void delete(PetType petType) { + String petTypeId = petType.getId().toString(); + + List pets = new ArrayList(); + pets = this.em.createQuery("SELECT pet FROM Pet pet WHERE type_id=" + petTypeId).getResultList(); + for (Pet pet : pets){ + List visits = new ArrayList(); + visits = pet.getVisits(); + for (Visit visit : visits){ + this.em.createQuery("DELETE FROM Visit visit WHERE id=" + visit.getId().toString()).executeUpdate(); + } + this.em.createQuery("DELETE FROM Pet pet WHERE id=" + pet.getId().toString()).executeUpdate(); + } + this.em.createQuery("DELETE FROM PetType pettype WHERE id=" + petTypeId).executeUpdate(); + if (em.contains(petType)) { + em.remove(petType); + } + + } + +} diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataSpecialtyRepositoryExt.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataSpecialtyRepositoryExt.java index 9361d1a4a..a123bee3e 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataSpecialtyRepositoryExt.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataSpecialtyRepositoryExt.java @@ -1,11 +1,33 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.samples.petclinic.repository.springdatajpa; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.repository.Repository; import org.springframework.samples.petclinic.model.Specialty; import org.springframework.samples.petclinic.repository.SpecialtyRepositoryExt; +import org.springframework.samples.petclinic.repository.springdatajpa.SpecialtyRepositoryExtOverride; + +/** + * @author Vitaliy Fedoriv + * + */ @Qualifier("SpecialtyRepositoryExt") -public interface SpringDataSpecialtyRepositoryExt extends SpecialtyRepositoryExt, Repository { +public interface SpringDataSpecialtyRepositoryExt extends SpecialtyRepositoryExt, Repository, SpecialtyRepositoryExtOverride { } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataSpecialtyRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataSpecialtyRepositoryExtImpl.java new file mode 100644 index 000000000..e33de30a2 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataSpecialtyRepositoryExtImpl.java @@ -0,0 +1,46 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.samples.petclinic.repository.springdatajpa; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.springframework.samples.petclinic.model.Specialty; +import org.springframework.samples.petclinic.repository.springdatajpa.SpecialtyRepositoryExtOverride; + +/** + * @author Vitaliy Fedoriv + * + */ + +public class SpringDataSpecialtyRepositoryExtImpl implements SpecialtyRepositoryExtOverride { + + @PersistenceContext + private EntityManager em; + + @Override + public void delete(Specialty specialty) { + String specId = specialty.getId().toString(); + this.em.createNativeQuery("DELETE FROM vet_specialties WHERE specialty_id=" + specId).executeUpdate(); + this.em.createQuery("DELETE FROM Specialty specialty WHERE id=" + specId).executeUpdate(); + if (em.contains(specialty)) { + em.remove(specialty); + } + + } + +}