override delete() method

This commit is contained in:
Vitaliy Fedoriv 2016-11-12 22:37:13 +02:00
parent bbae5a66f9
commit 3c3fd7ae0b
6 changed files with 212 additions and 2 deletions

View file

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

View file

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

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.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<PetType, Integer> {
public interface SpringDataPetTypeRepositoryExt extends PetTypeRepositoryExt, Repository<PetType, Integer>, PetTypeRepositoryExtOverride {
}

View file

@ -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<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,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<Specialty, Integer> {
public interface SpringDataSpecialtyRepositoryExt extends SpecialtyRepositoryExt, Repository<Specialty, Integer>, SpecialtyRepositoryExtOverride {
}

View file

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