mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-25 01:05:48 +00:00
Rename entities in persistence
This commit is contained in:
parent
26c461a88d
commit
1b5c934825
14 changed files with 50 additions and 54 deletions
|
@ -3,7 +3,7 @@ package org.springframework.samples.petclinic.application;
|
|||
import java.util.Collection;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.samples.petclinic.domain.VetRepository;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
|
@ -15,11 +15,11 @@ public class VetService {
|
|||
this.vetRepository = vetRepository;
|
||||
}
|
||||
|
||||
public Page<Vet> getVetPage(int page, int pageSize) {
|
||||
public Page<VetEntity> getVetPage(int page, int pageSize) {
|
||||
return this.vetRepository.getVetPage(page, pageSize);
|
||||
}
|
||||
|
||||
public Collection<Vet> getVets() {
|
||||
public Collection<VetEntity> getVets() {
|
||||
return this.vetRepository.getVets();
|
||||
}
|
||||
|
||||
|
|
|
@ -2,12 +2,12 @@ package org.springframework.samples.petclinic.domain;
|
|||
|
||||
import java.util.Collection;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetEntity;
|
||||
|
||||
public interface VetRepository {
|
||||
|
||||
Page<Vet> getVetPage(int page, int pageSize);
|
||||
Page<VetEntity> getVetPage(int page, int pageSize);
|
||||
|
||||
Collection<Vet> getVets();
|
||||
Collection<VetEntity> getVets();
|
||||
|
||||
}
|
||||
|
|
|
@ -15,12 +15,11 @@
|
|||
*/
|
||||
package org.springframework.samples.petclinic.infrastructure.controller;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.samples.petclinic.application.VetService;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetEntity;
|
||||
import org.springframework.samples.petclinic.infrastructure.view.Vets;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
|
@ -48,14 +47,14 @@ class VetController {
|
|||
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
||||
// objects so it is simpler for Object-Xml mapping
|
||||
Vets vets = new Vets();
|
||||
Page<Vet> paginated = findPaginated(page);
|
||||
Page<VetEntity> paginated = findPaginated(page);
|
||||
vets.getVetList().addAll(paginated.toList());
|
||||
return addPaginationModel(page, paginated, model);
|
||||
|
||||
}
|
||||
|
||||
private String addPaginationModel(int page, Page<Vet> paginated, Model model) {
|
||||
List<Vet> listVets = paginated.getContent();
|
||||
private String addPaginationModel(int page, Page<VetEntity> paginated, Model model) {
|
||||
List<VetEntity> listVets = paginated.getContent();
|
||||
model.addAttribute("currentPage", page);
|
||||
model.addAttribute("totalPages", paginated.getTotalPages());
|
||||
model.addAttribute("totalItems", paginated.getTotalElements());
|
||||
|
@ -63,7 +62,7 @@ class VetController {
|
|||
return "vets/vetList";
|
||||
}
|
||||
|
||||
private Page<Vet> findPaginated(int page) {
|
||||
private Page<VetEntity> findPaginated(int page) {
|
||||
int pageSize = 5;
|
||||
return vetService.getVetPage(page, pageSize);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ import jakarta.validation.constraints.NotEmpty;
|
|||
* @author Ken Krebs
|
||||
*/
|
||||
@MappedSuperclass
|
||||
public class Person extends BaseEntity {
|
||||
public class PersonEntity extends BaseEntity {
|
||||
|
||||
@Column(name = "first_name")
|
||||
@NotEmpty
|
|
@ -21,12 +21,12 @@ import jakarta.persistence.Entity;
|
|||
import jakarta.persistence.Table;
|
||||
|
||||
/**
|
||||
* Models a {@link Vet Vet's} specialty (for example, dentistry).
|
||||
* Models a {@link VetEntity Vet's} specialty (for example, dentistry).
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "specialties")
|
||||
public class Specialty extends NamedEntity {
|
||||
public class SpecialtyEntity extends NamedEntity {
|
||||
|
||||
}
|
|
@ -35,7 +35,7 @@ import java.util.Collection;
|
|||
* @author Sam Brannen
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
public interface VetDataRepository extends Repository<Vet, Integer> {
|
||||
public interface VetDataRepository extends Repository<VetEntity, Integer> {
|
||||
|
||||
/**
|
||||
* Retrieve all <code>Vet</code>s from the data store.
|
||||
|
@ -43,7 +43,7 @@ public interface VetDataRepository extends Repository<Vet, Integer> {
|
|||
*/
|
||||
@Transactional(readOnly = true)
|
||||
@Cacheable("vets")
|
||||
Collection<Vet> findAll() throws DataAccessException;
|
||||
Collection<VetEntity> findAll() throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Retrieve all <code>Vet</code>s from data store in Pages
|
||||
|
@ -53,6 +53,6 @@ public interface VetDataRepository extends Repository<Vet, Integer> {
|
|||
*/
|
||||
@Transactional(readOnly = true)
|
||||
@Cacheable("vets")
|
||||
Page<Vet> findAll(Pageable pageable) throws DataAccessException;
|
||||
Page<VetEntity> findAll(Pageable pageable) throws DataAccessException;
|
||||
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ import java.util.Set;
|
|||
|
||||
import org.springframework.beans.support.MutableSortDefinition;
|
||||
import org.springframework.beans.support.PropertyComparator;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.model.Person;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.model.PersonEntity;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
|
@ -43,27 +43,27 @@ import jakarta.xml.bind.annotation.XmlElement;
|
|||
*/
|
||||
@Entity
|
||||
@Table(name = "vets")
|
||||
public class Vet extends Person {
|
||||
public class VetEntity extends PersonEntity {
|
||||
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "specialty_id"))
|
||||
private Set<Specialty> specialties;
|
||||
private Set<SpecialtyEntity> specialties;
|
||||
|
||||
protected Set<Specialty> getSpecialtiesInternal() {
|
||||
protected Set<SpecialtyEntity> getSpecialtiesInternal() {
|
||||
if (this.specialties == null) {
|
||||
this.specialties = new HashSet<>();
|
||||
}
|
||||
return this.specialties;
|
||||
}
|
||||
|
||||
protected void setSpecialtiesInternal(Set<Specialty> specialties) {
|
||||
protected void setSpecialtiesInternal(Set<SpecialtyEntity> specialties) {
|
||||
this.specialties = specialties;
|
||||
}
|
||||
|
||||
@XmlElement
|
||||
public List<Specialty> getSpecialties() {
|
||||
List<Specialty> sortedSpecs = new ArrayList<>(getSpecialtiesInternal());
|
||||
public List<SpecialtyEntity> getSpecialties() {
|
||||
List<SpecialtyEntity> sortedSpecs = new ArrayList<>(getSpecialtiesInternal());
|
||||
PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true));
|
||||
return Collections.unmodifiableList(sortedSpecs);
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ public class Vet extends Person {
|
|||
return getSpecialtiesInternal().size();
|
||||
}
|
||||
|
||||
public void addSpecialty(Specialty specialty) {
|
||||
public void addSpecialty(SpecialtyEntity specialty) {
|
||||
getSpecialtiesInternal().add(specialty);
|
||||
}
|
||||
|
|
@ -17,13 +17,13 @@ public class VetRepositoryImpl implements VetRepository {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Page<Vet> getVetPage(int page, int pageSize) {
|
||||
public Page<VetEntity> getVetPage(int page, int pageSize) {
|
||||
Pageable pageable = PageRequest.of(page - 1, pageSize);
|
||||
return this.vetDataRepository.findAll(pageable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Vet> getVets() {
|
||||
public Collection<VetEntity> getVets() {
|
||||
return this.vetDataRepository.findAll();
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ import java.util.List;
|
|||
|
||||
import jakarta.xml.bind.annotation.XmlElement;
|
||||
import jakarta.xml.bind.annotation.XmlRootElement;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetEntity;
|
||||
|
||||
/**
|
||||
* Simple domain object representing a list of veterinarians. Mostly here to be used for
|
||||
|
@ -31,10 +31,10 @@ import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
|
|||
@XmlRootElement
|
||||
public class Vets {
|
||||
|
||||
private List<Vet> vets;
|
||||
private List<VetEntity> vets;
|
||||
|
||||
@XmlElement
|
||||
public List<Vet> getVetList() {
|
||||
public List<VetEntity> getVetList() {
|
||||
if (vets == null) {
|
||||
vets = new ArrayList<>();
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
import org.springframework.core.style.ToStringCreator;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.model.Person;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.model.PersonEntity;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
|
@ -44,7 +44,7 @@ import jakarta.validation.constraints.NotEmpty;
|
|||
*/
|
||||
@Entity
|
||||
@Table(name = "owners")
|
||||
public class Owner extends Person {
|
||||
public class Owner extends PersonEntity {
|
||||
|
||||
@Column(name = "address")
|
||||
@NotEmpty
|
||||
|
|
|
@ -24,11 +24,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.samples.petclinic.application.VetService;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Specialty;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.SpecialtyEntity;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetEntity;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
@ -51,20 +50,20 @@ class VetControllerTests {
|
|||
@MockBean
|
||||
private VetService vetService;
|
||||
|
||||
private Vet james() {
|
||||
Vet james = new Vet();
|
||||
private VetEntity james() {
|
||||
VetEntity james = new VetEntity();
|
||||
james.setFirstName("James");
|
||||
james.setLastName("Carter");
|
||||
james.setId(1);
|
||||
return james;
|
||||
}
|
||||
|
||||
private Vet helen() {
|
||||
Vet helen = new Vet();
|
||||
private VetEntity helen() {
|
||||
VetEntity helen = new VetEntity();
|
||||
helen.setFirstName("Helen");
|
||||
helen.setLastName("Leary");
|
||||
helen.setId(2);
|
||||
Specialty radiology = new Specialty();
|
||||
SpecialtyEntity radiology = new SpecialtyEntity();
|
||||
radiology.setId(1);
|
||||
radiology.setName("radiology");
|
||||
helen.addSpecialty(radiology);
|
||||
|
@ -73,7 +72,7 @@ class VetControllerTests {
|
|||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
ArrayList<Vet> vetsList = Lists.newArrayList(james(), helen());
|
||||
ArrayList<VetEntity> vetsList = Lists.newArrayList(james(), helen());
|
||||
|
||||
given(this.vetService.getVets()).willReturn(vetsList);
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ import java.util.Set;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.model.Person;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.model.PersonEntity;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
|
@ -45,15 +45,15 @@ class ValidatorTests {
|
|||
void shouldNotValidateWhenFirstNameEmpty() {
|
||||
|
||||
LocaleContextHolder.setLocale(Locale.ENGLISH);
|
||||
Person person = new Person();
|
||||
PersonEntity person = new PersonEntity();
|
||||
person.setFirstName("");
|
||||
person.setLastName("smith");
|
||||
|
||||
Validator validator = createValidator();
|
||||
Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person);
|
||||
Set<ConstraintViolation<PersonEntity>> constraintViolations = validator.validate(person);
|
||||
|
||||
assertThat(constraintViolations).hasSize(1);
|
||||
ConstraintViolation<Person> violation = constraintViolations.iterator().next();
|
||||
ConstraintViolation<PersonEntity> violation = constraintViolations.iterator().next();
|
||||
assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName");
|
||||
assertThat(violation.getMessage()).isEqualTo("must not be empty");
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.samples.petclinic.owner.Owner;
|
||||
|
@ -34,9 +33,8 @@ import org.springframework.samples.petclinic.owner.OwnerRepository;
|
|||
import org.springframework.samples.petclinic.owner.Pet;
|
||||
import org.springframework.samples.petclinic.owner.PetType;
|
||||
import org.springframework.samples.petclinic.owner.Visit;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetEntity;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetDataRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
|
@ -184,9 +182,9 @@ class ClinicServiceTests {
|
|||
|
||||
@Test
|
||||
void shouldFindVets() {
|
||||
Collection<Vet> vets = this.vets.findAll();
|
||||
Collection<VetEntity> vets = this.vets.findAll();
|
||||
|
||||
Vet vet = EntityUtils.getById(vets, Vet.class, 3);
|
||||
VetEntity vet = EntityUtils.getById(vets, VetEntity.class, 3);
|
||||
assertThat(vet.getLastName()).isEqualTo("Douglas");
|
||||
assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
|
||||
assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
package org.springframework.samples.petclinic.vet;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet;
|
||||
import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetEntity;
|
||||
import org.springframework.util.SerializationUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
@ -28,11 +28,11 @@ class VetTests {
|
|||
|
||||
@Test
|
||||
void testSerialization() {
|
||||
Vet vet = new Vet();
|
||||
VetEntity vet = new VetEntity();
|
||||
vet.setFirstName("Zaphod");
|
||||
vet.setLastName("Beeblebrox");
|
||||
vet.setId(123);
|
||||
Vet other = (Vet) SerializationUtils.deserialize(SerializationUtils.serialize(vet));
|
||||
VetEntity other = (VetEntity) SerializationUtils.deserialize(SerializationUtils.serialize(vet));
|
||||
assertThat(other.getFirstName()).isEqualTo(vet.getFirstName());
|
||||
assertThat(other.getLastName()).isEqualTo(vet.getLastName());
|
||||
assertThat(other.getId()).isEqualTo(vet.getId());
|
||||
|
|
Loading…
Reference in a new issue