diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryExtImpl.java index dcc21fcf2..14576c373 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryExtImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryExtImpl.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.jdbc; import java.util.Collection; @@ -6,6 +22,7 @@ import java.util.List; import java.util.Map; import javax.sql.DataSource; +import javax.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -13,9 +30,16 @@ import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.samples.petclinic.model.Owner; +import org.springframework.samples.petclinic.model.Pet; +import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.OwnerRepositoryExt; import org.springframework.stereotype.Repository; +/** + * @author Vitaliy Fedoriv + * + */ + @Repository @Qualifier("OwnerRepositoryExt") public class JdbcOwnerRepositoryExtImpl extends JdbcOwnerRepositoryImpl implements OwnerRepositoryExt { @@ -41,10 +65,25 @@ public class JdbcOwnerRepositoryExtImpl extends JdbcOwnerRepositoryImpl implemen } @Override + @Transactional public void delete(Owner owner) throws DataAccessException { - Map params = new HashMap<>(); - params.put("id", owner.getId()); - this.namedParameterJdbcTemplate.update("DELETE FROM owners WHERE id=:id", params); + Map owner_params = new HashMap<>(); + owner_params.put("id", owner.getId()); + List pets = owner.getPets(); + // cascade delete pets + for (Pet pet : pets){ + Map pet_params = new HashMap<>(); + pet_params.put("id", pet.getId()); + // cascade delete visits + List visits = pet.getVisits(); + for (Visit visit : visits){ + Map visit_params = new HashMap<>(); + visit_params.put("id", visit.getId()); + this.namedParameterJdbcTemplate.update("DELETE FROM visits WHERE id=:id", visit_params); + } + this.namedParameterJdbcTemplate.update("DELETE FROM pets WHERE id=:id", pet_params); + } + this.namedParameterJdbcTemplate.update("DELETE FROM owners WHERE id=:id", owner_params); } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryExtImpl.java index 2fdd4749c..57f74d809 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryExtImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryExtImpl.java @@ -1,8 +1,25 @@ +/* + * 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.jdbc; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.sql.DataSource; @@ -15,12 +32,18 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; +import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.OwnerRepositoryExt; import org.springframework.samples.petclinic.repository.PetRepositoryExt; import org.springframework.samples.petclinic.repository.VisitRepositoryExt; import org.springframework.samples.petclinic.util.EntityUtils; import org.springframework.stereotype.Repository; +/** + * @author Vitaliy Fedoriv + * + */ + @Repository @Qualifier("PetRepositoryExt") public class JdbcPetRepositoryExtImpl extends JdbcPetRepositoryImpl implements PetRepositoryExt { @@ -60,9 +83,16 @@ public class JdbcPetRepositoryExtImpl extends JdbcPetRepositoryImpl implements P @Override public void delete(Pet pet) throws DataAccessException { - Map params = new HashMap<>(); - params.put("id", pet.getId()); - this.namedParameterJdbcTemplate.update("DELETE FROM pets WHERE id=:id", params); + Map pet_params = new HashMap<>(); + pet_params.put("id", pet.getId()); + List visits = pet.getVisits(); + // cascade delete visits + for (Visit visit : visits){ + Map visit_params = new HashMap<>(); + visit_params.put("id", visit.getId()); + this.namedParameterJdbcTemplate.update("DELETE FROM visits WHERE id=:id", visit_params); + } + this.namedParameterJdbcTemplate.update("DELETE FROM pets WHERE id=:id", pet_params); } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetTypeRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetTypeRepositoryExtImpl.java index e3be84f1d..9286ea7fd 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetTypeRepositoryExtImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetTypeRepositoryExtImpl.java @@ -1,7 +1,25 @@ +/* + * 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.jdbc; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.sql.DataSource; @@ -15,10 +33,17 @@ import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.orm.ObjectRetrievalFailureException; +import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; +import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.PetTypeRepositoryExt; import org.springframework.stereotype.Repository; +/** + * @author Vitaliy Fedoriv + * + */ + @Repository @Qualifier("PetTypeRepositoryExt") public class JdbcPetTypeRepositoryExtImpl implements PetTypeRepositoryExt { @@ -74,9 +99,31 @@ public class JdbcPetTypeRepositoryExtImpl implements PetTypeRepositoryExt { @Override public void delete(PetType petType) throws DataAccessException { - Map params = new HashMap<>(); - params.put("id", petType.getId()); - this.namedParameterJdbcTemplate.update("DELETE FROM types WHERE id=:id", params); + Map pettype_params = new HashMap<>(); + pettype_params.put("id", petType.getId()); + List pets = new ArrayList(); + pets = this.namedParameterJdbcTemplate. + query("SELECT pets.id, name, birth_date, type_id, owner_id FROM pets WHERE type_id=:id", + pettype_params, + BeanPropertyRowMapper.newInstance(Pet.class)); + // cascade delete pets + for (Pet pet : pets){ + Map pet_params = new HashMap<>(); + pet_params.put("id", pet.getId()); + List visits = new ArrayList(); + visits = this.namedParameterJdbcTemplate.query( + "SELECT id, pet_id, visit_date, description FROM visits WHERE pet_id = :id", + pet_params, + BeanPropertyRowMapper.newInstance(Visit.class)); + // cascade delete visits + for (Visit visit : visits){ + Map visit_params = new HashMap<>(); + visit_params.put("id", visit.getId()); + this.namedParameterJdbcTemplate.update("DELETE FROM visits WHERE id=:id", visit_params); + } + this.namedParameterJdbcTemplate.update("DELETE FROM pets WHERE id=:id", pet_params); + } + this.namedParameterJdbcTemplate.update("DELETE FROM types WHERE id=:id", pettype_params); } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryExtImpl.java index 70fc1cb7b..872efbf80 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryExtImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetRepositoryExtImpl.java @@ -1,19 +1,29 @@ -/** - * +/* + * 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.jpa; import java.util.Collection; -import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.dao.DataAccessException; -import org.springframework.samples.petclinic.model.Owner; import org.springframework.samples.petclinic.model.Pet; -import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.PetRepositoryExt; import org.springframework.stereotype.Repository; @@ -36,9 +46,13 @@ public class JpaPetRepositoryExtImpl extends JpaPetRepositoryImpl implements Pet @Override public void delete(Pet pet) throws DataAccessException { + //this.em.remove(this.em.contains(pet) ? pet : this.em.merge(pet)); String petId = pet.getId().toString(); this.em.createQuery("DELETE FROM Visit visit WHERE pet_id=" + petId).executeUpdate(); this.em.createQuery("DELETE FROM Pet pet WHERE id=" + petId).executeUpdate(); + if (em.contains(pet)) { + em.remove(pet); + } } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetTypeRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetTypeRepositoryExtImpl.java index 74507b46a..b20e3e0c5 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetTypeRepositoryExtImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaPetTypeRepositoryExtImpl.java @@ -1,16 +1,41 @@ +/* + * 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.jpa; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.dao.DataAccessException; +import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.PetType; +import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.PetTypeRepositoryExt; import org.springframework.stereotype.Repository; +/** + * @author Vitaliy Fedoriv + * + */ + @Repository @Qualifier("PetTypeRepositoryExt") public class JpaPetTypeRepositoryExtImpl implements PetTypeRepositoryExt { @@ -39,9 +64,26 @@ public class JpaPetTypeRepositoryExtImpl implements PetTypeRepositoryExt { } + @SuppressWarnings("unchecked") @Override public void delete(PetType petType) throws DataAccessException { - this.em.remove(this.em.contains(petType) ? petType : this.em.merge(petType)); + //this.em.remove(this.em.contains(petType) ? petType : this.em.merge(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/jpa/JpaSpecialtyRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaSpecialtyRepositoryExtImpl.java index 9b0773bd8..55d1b851a 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaSpecialtyRepositoryExtImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaSpecialtyRepositoryExtImpl.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.jpa; import java.util.Collection; @@ -11,6 +27,11 @@ import org.springframework.samples.petclinic.model.Specialty; import org.springframework.samples.petclinic.repository.SpecialtyRepositoryExt; import org.springframework.stereotype.Repository; +/** + * @author Vitaliy Fedoriv + * + */ + @Repository @Qualifier("SpecialtyRepositoryExt") public class JpaSpecialtyRepositoryExtImpl implements SpecialtyRepositoryExt { @@ -40,7 +61,13 @@ public class JpaSpecialtyRepositoryExtImpl implements SpecialtyRepositoryExt { @Override public void delete(Specialty specialty) throws DataAccessException { - this.em.remove(this.em.contains(specialty) ? specialty : this.em.merge(specialty)); + //this.em.remove(this.em.contains(specialty) ? specialty : this.em.merge(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); + } } } diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryExtImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryExtImpl.java index 4679a1c68..a3697616c 100644 --- a/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryExtImpl.java +++ b/src/main/java/org/springframework/samples/petclinic/repository/jpa/JpaVisitRepositoryExtImpl.java @@ -1,10 +1,25 @@ +/* + * 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.jpa; import java.util.Collection; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; -import javax.persistence.Query; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.dao.DataAccessException; @@ -12,6 +27,11 @@ import org.springframework.samples.petclinic.model.Visit; import org.springframework.samples.petclinic.repository.VisitRepositoryExt; import org.springframework.stereotype.Repository; +/** + * @author Vitaliy Fedoriv + * + */ + @Repository @Qualifier("VisitRepositoryExt") public class JpaVisitRepositoryExtImpl extends JpaVisitRepositoryImpl implements VisitRepositoryExt { @@ -34,6 +54,9 @@ public class JpaVisitRepositoryExtImpl extends JpaVisitRepositoryImpl implements public void delete(Visit visit) throws DataAccessException { String visitId = visit.getId().toString(); this.em.createQuery("DELETE FROM Visit visit WHERE id=" + visitId).executeUpdate(); + if (em.contains(visit)) { + em.remove(visit); + } } }