Skip to content
Snippets Groups Projects
Commit 9f8acc05 authored by Mic's avatar Mic
Browse files

migration to JPA annotations

- added JPA annotations to POJO classes
- merged JPA Repos with Hibernate repos so we only use JPA
- renamed .txt files to .sql
- moved Spring configuration files so it is easier to use them inside
JUnit tests
parent 099847cf
No related branches found
No related tags found
No related merge requests found
Showing
with 90 additions and 132 deletions
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.3.0.200912170948-RELEASE]]></pluginVersion>
<pluginVersion><![CDATA[3.2.0.201211290605-M1]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
......
package org.springframework.samples.petclinic;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
/**
* Simple JavaBean domain object with an id property.
* Used as a base class for objects needing this property.
......@@ -7,9 +10,10 @@ package org.springframework.samples.petclinic;
* @author Ken Krebs
* @author Juergen Hoeller
*/
@MappedSuperclass
public class BaseEntity {
private Integer id;
@Id
protected Integer id;
public void setId(Integer id) {
......
......@@ -43,7 +43,7 @@ public interface Clinic {
* @return the <code>Owner</code> if found
* @throws org.springframework.dao.DataRetrievalFailureException if not found
*/
Owner loadOwner(int id) throws DataAccessException;
Owner findOwner(int id) throws DataAccessException;
/**
* Retrieve a <code>Pet</code> from the data store by id.
......@@ -51,7 +51,7 @@ public interface Clinic {
* @return the <code>Pet</code> if found
* @throws org.springframework.dao.DataRetrievalFailureException if not found
*/
Pet loadPet(int id) throws DataAccessException;
Pet findPet(int id) throws DataAccessException;
/**
* Save an <code>Owner</code> to the data store, either inserting or updating it.
......
package org.springframework.samples.petclinic;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
/**
* Simple JavaBean domain object adds a name property to <code>BaseEntity</code>.
* Used as a base class for objects needing these properties.
......@@ -7,8 +10,10 @@ package org.springframework.samples.petclinic;
* @author Ken Krebs
* @author Juergen Hoeller
*/
@MappedSuperclass
public class NamedEntity extends BaseEntity {
@Column(name="name")
private String name;
......
......@@ -6,6 +6,12 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.core.style.ToStringCreator;
......@@ -17,14 +23,18 @@ import org.springframework.core.style.ToStringCreator;
* @author Juergen Hoeller
* @author Sam Brannen
*/
@Entity @Table(name="owners")
public class Owner extends Person {
@Column(name="address")
private String address;
@Column(name="city")
private String city;
@Column(name="telephone")
private String telephone;
@OneToMany(cascade=CascadeType.ALL, mappedBy="owner")
private Set<Pet> pets;
......
package org.springframework.samples.petclinic;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
/**
* Simple JavaBean domain object representing an person.
*
* @author Ken Krebs
*/
@MappedSuperclass
public class Person extends BaseEntity {
@Column(name="first_name")
protected String firstName;
private String firstName;
private String lastName;
@Column(name="last_name")
protected String lastName;
public String getFirstName() {
return this.firstName;
......
......@@ -7,6 +7,14 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
......@@ -17,14 +25,21 @@ import org.springframework.beans.support.PropertyComparator;
* @author Juergen Hoeller
* @author Sam Brannen
*/
@Entity @Table(name="pets")
public class Pet extends NamedEntity {
@Column(name="birth_date")
private Date birthDate;
@ManyToOne
@JoinColumn(name = "type_id")
private PetType type;
@ManyToOne
@JoinColumn(name = "owner_id")
private Owner owner;
@OneToMany(cascade=CascadeType.ALL, mappedBy="pet")
private Set<Visit> visits;
......
package org.springframework.samples.petclinic;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* @author Juergen Hoeller
*/
@Entity @Table(name="types")
public class PetType extends NamedEntity {
}
package org.springframework.samples.petclinic;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* Models a {@link Vet Vet's} specialty (for example, dentistry).
*
* @author Juergen Hoeller
*/
@Entity @Table(name="specialties")
public class Specialty extends NamedEntity {
}
......@@ -5,6 +5,10 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import org.springframework.beans.support.MutableSortDefinition;
......@@ -18,8 +22,10 @@ import org.springframework.beans.support.PropertyComparator;
* @author Sam Brannen
* @author Arjen Poutsma
*/
@Entity @Table(name="vets")
public class Vet extends Person {
@ManyToMany
private Set<Specialty> specialties;
......
......@@ -2,20 +2,31 @@ package org.springframework.samples.petclinic;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* Simple JavaBean domain object representing a visit.
*
* @author Ken Krebs
*/
@Entity @Table(name="visits")
public class Visit extends BaseEntity {
/** Holds value of property date. */
@Column(name="visit_date")
private Date date;
/** Holds value of property description. */
@Column(name="description")
private String description;
/** Holds value of property pet. */
@ManyToOne
@JoinColumn(name = "pet_id")
private Pet pet;
......
package org.springframework.samples.petclinic.hibernate;
import java.util.Collection;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.Owner;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.Vet;
import org.springframework.samples.petclinic.Visit;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
/**
* Hibernate implementation of the Clinic interface.
*
* <p>The mappings are defined in "petclinic.hbm.xml", located in the root of the
* class path.
*
* <p>Note that transactions are declared with annotations and that some methods
* contain "readOnly = true" which is an optimization that is particularly
* valuable when using Hibernate (to suppress unnecessary flush attempts for
* read-only operations).
*
* @author Juergen Hoeller
* @author Sam Brannen
* @author Mark Fisher
* @since 19.10.2003
*/
@Repository
@Transactional
public class HibernateClinic implements Clinic {
private SessionFactory sessionFactory;
@Autowired
public HibernateClinic(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public Collection<Vet> getVets() {
return sessionFactory.getCurrentSession().createQuery("from Vet vet order by vet.lastName, vet.firstName").list();
}
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public Collection<PetType> getPetTypes() {
return sessionFactory.getCurrentSession().createQuery("from PetType type order by type.name").list();
}
@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
public Collection<Owner> findOwners(String lastName) {
return sessionFactory.getCurrentSession().createQuery("from Owner owner where owner.lastName like :lastName")
.setString("lastName", lastName + "%").list();
}
@Transactional(readOnly = true)
public Owner loadOwner(int id) {
return (Owner) sessionFactory.getCurrentSession().load(Owner.class, id);
}
@Transactional(readOnly = true)
public Pet loadPet(int id) {
return (Pet) sessionFactory.getCurrentSession().load(Pet.class, id);
}
public void storeOwner(Owner owner) {
// Note: Hibernate3's merge operation does not reassociate the object
// with the current Hibernate Session. Instead, it will always copy the
// state over to a registered representation of the entity. In case of a
// new entity, it will register a copy as well, but will not update the
// id of the passed-in object. To still update the ids of the original
// objects too, we need to register Spring's
// IdTransferringMergeEventListener on our SessionFactory.
sessionFactory.getCurrentSession().merge(owner);
}
public void storePet(Pet pet) {
sessionFactory.getCurrentSession().merge(pet);
}
public void storeVisit(Visit visit) {
sessionFactory.getCurrentSession().merge(visit);
}
public void deletePet(int id) throws DataAccessException {
Pet pet = loadPet(id);
sessionFactory.getCurrentSession().delete(pet);
}
}
/**
*
* The classes in this package represent the Hibernate implementation
* of PetClinic's persistence layer.
*
*/
package org.springframework.samples.petclinic.hibernate;
......@@ -163,7 +163,7 @@ public class SimpleJdbcClinic implements Clinic, SimpleJdbcClinicMBean {
* owner, if not already loaded.
*/
@Transactional(readOnly = true)
public Owner loadOwner(int id) throws DataAccessException {
public Owner findOwner(int id) throws DataAccessException {
Owner owner;
try {
owner = this.simpleJdbcTemplate.queryForObject(
......@@ -179,7 +179,7 @@ public class SimpleJdbcClinic implements Clinic, SimpleJdbcClinicMBean {
}
@Transactional(readOnly = true)
public Pet loadPet(int id) throws DataAccessException {
public Pet findPet(int id) throws DataAccessException {
JdbcPet pet;
try {
pet = this.simpleJdbcTemplate.queryForObject(
......@@ -190,7 +190,7 @@ public class SimpleJdbcClinic implements Clinic, SimpleJdbcClinicMBean {
catch (EmptyResultDataAccessException ex) {
throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
}
Owner owner = loadOwner(pet.getOwnerId());
Owner owner = findOwner(pet.getOwnerId());
owner.addPet(pet);
pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
loadVisits(pet);
......
......@@ -55,12 +55,12 @@ public class EntityManagerClinic implements Clinic {
}
@Transactional(readOnly = true)
public Owner loadOwner(int id) {
public Owner findOwner(int id) {
return this.em.find(Owner.class, id);
}
@Transactional(readOnly = true)
public Pet loadPet(int id) {
public Pet findPet(int id) {
return this.em.find(Pet.class, id);
}
......@@ -89,7 +89,7 @@ public class EntityManagerClinic implements Clinic {
}
public void deletePet(int id) throws DataAccessException {
Pet pet = loadPet(id);
Pet pet = findPet(id);
this.em.remove(pet);
}
......
......@@ -54,7 +54,7 @@ public class AddPetController {
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@PathVariable("ownerId") int ownerId, Model model) {
Owner owner = this.clinic.loadOwner(ownerId);
Owner owner = this.clinic.findOwner(ownerId);
Pet pet = new Pet();
owner.addPet(pet);
model.addAttribute("pet", pet);
......
......@@ -46,7 +46,7 @@ public class AddVisitController {
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@PathVariable("petId") int petId, Model model) {
Pet pet = this.clinic.loadPet(petId);
Pet pet = this.clinic.findPet(petId);
Visit visit = new Visit();
pet.addVisit(visit);
model.addAttribute("visit", visit);
......
......@@ -71,7 +71,7 @@ public class ClinicController {
@RequestMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
ModelAndView mav = new ModelAndView("owners/show");
mav.addObject(this.clinic.loadOwner(ownerId));
mav.addObject(this.clinic.findOwner(ownerId));
return mav;
}
......@@ -84,7 +84,7 @@ public class ClinicController {
@RequestMapping(value="/owners/*/pets/{petId}/visits", method=RequestMethod.GET)
public ModelAndView visitsHandler(@PathVariable int petId) {
ModelAndView mav = new ModelAndView("visits");
mav.addObject("visits", this.clinic.loadPet(petId).getVisits());
mav.addObject("visits", this.clinic.findPet(petId).getVisits());
return mav;
}
......
......@@ -44,7 +44,7 @@ public class EditOwnerController {
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@PathVariable("ownerId") int ownerId, Model model) {
Owner owner = this.clinic.loadOwner(ownerId);
Owner owner = this.clinic.findOwner(ownerId);
model.addAttribute(owner);
return "owners/form";
}
......
......@@ -52,7 +52,7 @@ public class EditPetController {
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@PathVariable("petId") int petId, Model model) {
Pet pet = this.clinic.loadPet(petId);
Pet pet = this.clinic.findPet(petId);
model.addAttribute("pet", pet);
return "pets/form";
}
......@@ -72,7 +72,7 @@ public class EditPetController {
@RequestMapping(method = RequestMethod.DELETE)
public String deletePet(@PathVariable int petId) {
Pet pet = this.clinic.loadPet(petId);
Pet pet = this.clinic.findPet(petId);
this.clinic.deletePet(petId);
return "redirect:/owners/" + pet.getOwner().getId();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment