Rename entities in persistence

This commit is contained in:
Ismail Ismailov 2023-04-27 14:48:46 +02:00
parent 26c461a88d
commit 1b5c934825
14 changed files with 50 additions and 54 deletions

View file

@ -3,7 +3,7 @@ package org.springframework.samples.petclinic.application;
import java.util.Collection; import java.util.Collection;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.samples.petclinic.domain.VetRepository; 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; import org.springframework.stereotype.Service;
@Service @Service
@ -15,11 +15,11 @@ public class VetService {
this.vetRepository = vetRepository; 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); return this.vetRepository.getVetPage(page, pageSize);
} }
public Collection<Vet> getVets() { public Collection<VetEntity> getVets() {
return this.vetRepository.getVets(); return this.vetRepository.getVets();
} }

View file

@ -2,12 +2,12 @@ package org.springframework.samples.petclinic.domain;
import java.util.Collection; import java.util.Collection;
import org.springframework.data.domain.Page; 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 { public interface VetRepository {
Page<Vet> getVetPage(int page, int pageSize); Page<VetEntity> getVetPage(int page, int pageSize);
Collection<Vet> getVets(); Collection<VetEntity> getVets();
} }

View file

@ -15,12 +15,11 @@
*/ */
package org.springframework.samples.petclinic.infrastructure.controller; package org.springframework.samples.petclinic.infrastructure.controller;
import java.util.Collection;
import java.util.List; import java.util.List;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.samples.petclinic.application.VetService; 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.samples.petclinic.infrastructure.view.Vets;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; 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 // Here we are returning an object of type 'Vets' rather than a collection of Vet
// objects so it is simpler for Object-Xml mapping // objects so it is simpler for Object-Xml mapping
Vets vets = new Vets(); Vets vets = new Vets();
Page<Vet> paginated = findPaginated(page); Page<VetEntity> paginated = findPaginated(page);
vets.getVetList().addAll(paginated.toList()); vets.getVetList().addAll(paginated.toList());
return addPaginationModel(page, paginated, model); return addPaginationModel(page, paginated, model);
} }
private String addPaginationModel(int page, Page<Vet> paginated, Model model) { private String addPaginationModel(int page, Page<VetEntity> paginated, Model model) {
List<Vet> listVets = paginated.getContent(); List<VetEntity> listVets = paginated.getContent();
model.addAttribute("currentPage", page); model.addAttribute("currentPage", page);
model.addAttribute("totalPages", paginated.getTotalPages()); model.addAttribute("totalPages", paginated.getTotalPages());
model.addAttribute("totalItems", paginated.getTotalElements()); model.addAttribute("totalItems", paginated.getTotalElements());
@ -63,7 +62,7 @@ class VetController {
return "vets/vetList"; return "vets/vetList";
} }
private Page<Vet> findPaginated(int page) { private Page<VetEntity> findPaginated(int page) {
int pageSize = 5; int pageSize = 5;
return vetService.getVetPage(page, pageSize); return vetService.getVetPage(page, pageSize);
} }

View file

@ -25,7 +25,7 @@ import jakarta.validation.constraints.NotEmpty;
* @author Ken Krebs * @author Ken Krebs
*/ */
@MappedSuperclass @MappedSuperclass
public class Person extends BaseEntity { public class PersonEntity extends BaseEntity {
@Column(name = "first_name") @Column(name = "first_name")
@NotEmpty @NotEmpty

View file

@ -21,12 +21,12 @@ import jakarta.persistence.Entity;
import jakarta.persistence.Table; 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 * @author Juergen Hoeller
*/ */
@Entity @Entity
@Table(name = "specialties") @Table(name = "specialties")
public class Specialty extends NamedEntity { public class SpecialtyEntity extends NamedEntity {
} }

View file

@ -35,7 +35,7 @@ import java.util.Collection;
* @author Sam Brannen * @author Sam Brannen
* @author Michael Isvy * @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. * Retrieve all <code>Vet</code>s from the data store.
@ -43,7 +43,7 @@ public interface VetDataRepository extends Repository<Vet, Integer> {
*/ */
@Transactional(readOnly = true) @Transactional(readOnly = true)
@Cacheable("vets") @Cacheable("vets")
Collection<Vet> findAll() throws DataAccessException; Collection<VetEntity> findAll() throws DataAccessException;
/** /**
* Retrieve all <code>Vet</code>s from data store in Pages * 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) @Transactional(readOnly = true)
@Cacheable("vets") @Cacheable("vets")
Page<Vet> findAll(Pageable pageable) throws DataAccessException; Page<VetEntity> findAll(Pageable pageable) throws DataAccessException;
} }

View file

@ -23,7 +23,7 @@ import java.util.Set;
import org.springframework.beans.support.MutableSortDefinition; import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator; 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.Entity;
import jakarta.persistence.FetchType; import jakarta.persistence.FetchType;
@ -43,27 +43,27 @@ import jakarta.xml.bind.annotation.XmlElement;
*/ */
@Entity @Entity
@Table(name = "vets") @Table(name = "vets")
public class Vet extends Person { public class VetEntity extends PersonEntity {
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"), @JoinTable(name = "vet_specialties", joinColumns = @JoinColumn(name = "vet_id"),
inverseJoinColumns = @JoinColumn(name = "specialty_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) { if (this.specialties == null) {
this.specialties = new HashSet<>(); this.specialties = new HashSet<>();
} }
return this.specialties; return this.specialties;
} }
protected void setSpecialtiesInternal(Set<Specialty> specialties) { protected void setSpecialtiesInternal(Set<SpecialtyEntity> specialties) {
this.specialties = specialties; this.specialties = specialties;
} }
@XmlElement @XmlElement
public List<Specialty> getSpecialties() { public List<SpecialtyEntity> getSpecialties() {
List<Specialty> sortedSpecs = new ArrayList<>(getSpecialtiesInternal()); List<SpecialtyEntity> sortedSpecs = new ArrayList<>(getSpecialtiesInternal());
PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true)); PropertyComparator.sort(sortedSpecs, new MutableSortDefinition("name", true, true));
return Collections.unmodifiableList(sortedSpecs); return Collections.unmodifiableList(sortedSpecs);
} }
@ -72,7 +72,7 @@ public class Vet extends Person {
return getSpecialtiesInternal().size(); return getSpecialtiesInternal().size();
} }
public void addSpecialty(Specialty specialty) { public void addSpecialty(SpecialtyEntity specialty) {
getSpecialtiesInternal().add(specialty); getSpecialtiesInternal().add(specialty);
} }

View file

@ -17,13 +17,13 @@ public class VetRepositoryImpl implements VetRepository {
} }
@Override @Override
public Page<Vet> getVetPage(int page, int pageSize) { public Page<VetEntity> getVetPage(int page, int pageSize) {
Pageable pageable = PageRequest.of(page - 1, pageSize); Pageable pageable = PageRequest.of(page - 1, pageSize);
return this.vetDataRepository.findAll(pageable); return this.vetDataRepository.findAll(pageable);
} }
@Override @Override
public Collection<Vet> getVets() { public Collection<VetEntity> getVets() {
return this.vetDataRepository.findAll(); return this.vetDataRepository.findAll();
} }

View file

@ -20,7 +20,7 @@ import java.util.List;
import jakarta.xml.bind.annotation.XmlElement; import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement; 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 * 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 @XmlRootElement
public class Vets { public class Vets {
private List<Vet> vets; private List<VetEntity> vets;
@XmlElement @XmlElement
public List<Vet> getVetList() { public List<VetEntity> getVetList() {
if (vets == null) { if (vets == null) {
vets = new ArrayList<>(); vets = new ArrayList<>();
} }

View file

@ -19,7 +19,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.core.style.ToStringCreator; 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 org.springframework.util.Assert;
import jakarta.persistence.CascadeType; import jakarta.persistence.CascadeType;
@ -44,7 +44,7 @@ import jakarta.validation.constraints.NotEmpty;
*/ */
@Entity @Entity
@Table(name = "owners") @Table(name = "owners")
public class Owner extends Person { public class Owner extends PersonEntity {
@Column(name = "address") @Column(name = "address")
@NotEmpty @NotEmpty

View file

@ -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.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.samples.petclinic.application.VetService; import org.springframework.samples.petclinic.application.VetService;
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Specialty; import org.springframework.samples.petclinic.infrastructure.persistence.vet.SpecialtyEntity;
import org.springframework.samples.petclinic.infrastructure.persistence.vet.Vet; import org.springframework.samples.petclinic.infrastructure.persistence.vet.VetEntity;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@ -51,20 +50,20 @@ class VetControllerTests {
@MockBean @MockBean
private VetService vetService; private VetService vetService;
private Vet james() { private VetEntity james() {
Vet james = new Vet(); VetEntity james = new VetEntity();
james.setFirstName("James"); james.setFirstName("James");
james.setLastName("Carter"); james.setLastName("Carter");
james.setId(1); james.setId(1);
return james; return james;
} }
private Vet helen() { private VetEntity helen() {
Vet helen = new Vet(); VetEntity helen = new VetEntity();
helen.setFirstName("Helen"); helen.setFirstName("Helen");
helen.setLastName("Leary"); helen.setLastName("Leary");
helen.setId(2); helen.setId(2);
Specialty radiology = new Specialty(); SpecialtyEntity radiology = new SpecialtyEntity();
radiology.setId(1); radiology.setId(1);
radiology.setName("radiology"); radiology.setName("radiology");
helen.addSpecialty(radiology); helen.addSpecialty(radiology);
@ -73,7 +72,7 @@ class VetControllerTests {
@BeforeEach @BeforeEach
void setup() { void setup() {
ArrayList<Vet> vetsList = Lists.newArrayList(james(), helen()); ArrayList<VetEntity> vetsList = Lists.newArrayList(james(), helen());
given(this.vetService.getVets()).willReturn(vetsList); given(this.vetService.getVets()).willReturn(vetsList);

View file

@ -23,7 +23,7 @@ import java.util.Set;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.context.i18n.LocaleContextHolder; 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 org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolation;
@ -45,15 +45,15 @@ class ValidatorTests {
void shouldNotValidateWhenFirstNameEmpty() { void shouldNotValidateWhenFirstNameEmpty() {
LocaleContextHolder.setLocale(Locale.ENGLISH); LocaleContextHolder.setLocale(Locale.ENGLISH);
Person person = new Person(); PersonEntity person = new PersonEntity();
person.setFirstName(""); person.setFirstName("");
person.setLastName("smith"); person.setLastName("smith");
Validator validator = createValidator(); Validator validator = createValidator();
Set<ConstraintViolation<Person>> constraintViolations = validator.validate(person); Set<ConstraintViolation<PersonEntity>> constraintViolations = validator.validate(person);
assertThat(constraintViolations).hasSize(1); assertThat(constraintViolations).hasSize(1);
ConstraintViolation<Person> violation = constraintViolations.iterator().next(); ConstraintViolation<PersonEntity> violation = constraintViolations.iterator().next();
assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName"); assertThat(violation.getPropertyPath().toString()).isEqualTo("firstName");
assertThat(violation.getMessage()).isEqualTo("must not be empty"); assertThat(violation.getMessage()).isEqualTo("must not be empty");
} }

View file

@ -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;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; 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.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.samples.petclinic.owner.Owner; 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.Pet;
import org.springframework.samples.petclinic.owner.PetType; import org.springframework.samples.petclinic.owner.PetType;
import org.springframework.samples.petclinic.owner.Visit; 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.samples.petclinic.infrastructure.persistence.vet.VetDataRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
/** /**
@ -184,9 +182,9 @@ class ClinicServiceTests {
@Test @Test
void shouldFindVets() { 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.getLastName()).isEqualTo("Douglas");
assertThat(vet.getNrOfSpecialties()).isEqualTo(2); assertThat(vet.getNrOfSpecialties()).isEqualTo(2);
assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry"); assertThat(vet.getSpecialties().get(0).getName()).isEqualTo("dentistry");

View file

@ -16,7 +16,7 @@
package org.springframework.samples.petclinic.vet; package org.springframework.samples.petclinic.vet;
import org.junit.jupiter.api.Test; 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 org.springframework.util.SerializationUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -28,11 +28,11 @@ class VetTests {
@Test @Test
void testSerialization() { void testSerialization() {
Vet vet = new Vet(); VetEntity vet = new VetEntity();
vet.setFirstName("Zaphod"); vet.setFirstName("Zaphod");
vet.setLastName("Beeblebrox"); vet.setLastName("Beeblebrox");
vet.setId(123); 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.getFirstName()).isEqualTo(vet.getFirstName());
assertThat(other.getLastName()).isEqualTo(vet.getLastName()); assertThat(other.getLastName()).isEqualTo(vet.getLastName());
assertThat(other.getId()).isEqualTo(vet.getId()); assertThat(other.getId()).isEqualTo(vet.getId());