change delete() methods to cascade delete

This commit is contained in:
Vitaliy Fedoriv 2016-11-12 23:02:35 +02:00
parent d861ca635b
commit 7b310b1a6e
7 changed files with 239 additions and 17 deletions

View file

@ -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<String, Object> params = new HashMap<>();
params.put("id", owner.getId());
this.namedParameterJdbcTemplate.update("DELETE FROM owners WHERE id=:id", params);
Map<String, Object> owner_params = new HashMap<>();
owner_params.put("id", owner.getId());
List<Pet> pets = owner.getPets();
// cascade delete pets
for (Pet pet : pets){
Map<String, Object> pet_params = new HashMap<>();
pet_params.put("id", pet.getId());
// cascade delete visits
List<Visit> visits = pet.getVisits();
for (Visit visit : visits){
Map<String, Object> 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);
}
}

View file

@ -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<String, Object> params = new HashMap<>();
params.put("id", pet.getId());
this.namedParameterJdbcTemplate.update("DELETE FROM pets WHERE id=:id", params);
Map<String, Object> pet_params = new HashMap<>();
pet_params.put("id", pet.getId());
List<Visit> visits = pet.getVisits();
// cascade delete visits
for (Visit visit : visits){
Map<String, Object> 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);
}
}

View file

@ -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<String, Object> params = new HashMap<>();
params.put("id", petType.getId());
this.namedParameterJdbcTemplate.update("DELETE FROM types WHERE id=:id", params);
Map<String, Object> pettype_params = new HashMap<>();
pettype_params.put("id", petType.getId());
List<Pet> pets = new ArrayList<Pet>();
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<String, Object> pet_params = new HashMap<>();
pet_params.put("id", pet.getId());
List<Visit> visits = new ArrayList<Visit>();
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<String, Object> 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);
}
}

View file

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

View file

@ -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<Pet> pets = new ArrayList<Pet>();
pets = this.em.createQuery("SELECT pet FROM Pet pet WHERE type_id=" + petTypeId).getResultList();
for (Pet pet : pets){
List<Visit> visits = new ArrayList<Visit>();
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);
}
}
}

View file

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

View file

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