Adding cache

This commit is contained in:
Julien Dubois 2013-03-02 01:31:55 +01:00
parent 681026758d
commit 05a60b7740
13 changed files with 68 additions and 12 deletions

View file

@ -148,6 +148,10 @@
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>

View file

@ -15,6 +15,7 @@
*/
package org.springframework.samples.petclinic.model;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
@ -34,6 +35,7 @@ import java.util.*;
*/
@Entity
@Table(name = "owners")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Owner extends Person {
@Column(name = "address")
@NotEmpty
@ -48,7 +50,8 @@ public class Owner extends Person {
@Digits(fraction = 0, integer = 10)
private String telephone;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
@OneToMany(mappedBy = "owner")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set<Pet> pets;

View file

@ -15,6 +15,7 @@
*/
package org.springframework.samples.petclinic.model;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import org.springframework.beans.support.MutableSortDefinition;
@ -33,6 +34,7 @@ import java.util.*;
*/
@Entity
@Table(name = "pets")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Pet extends NamedEntity {
@Column(name = "birth_date")
@ -48,7 +50,8 @@ public class Pet extends NamedEntity {
@JoinColumn(name = "owner_id")
private Owner owner;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pet")
@OneToMany(mappedBy = "pet")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set<Visit> visits;

View file

@ -15,6 +15,9 @@
*/
package org.springframework.samples.petclinic.model;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.Table;
@ -23,6 +26,7 @@ import javax.persistence.Table;
*/
@Entity
@Table(name = "types")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class PetType extends NamedEntity {
}

View file

@ -15,6 +15,7 @@
*/
package org.springframework.samples.petclinic.model;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.hibernate.validator.constraints.NotEmpty;
import org.joda.time.DateTime;
@ -29,6 +30,7 @@ import javax.persistence.*;
*/
@Entity
@Table(name = "visits")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Visit extends BaseEntity {
/**

View file

@ -23,6 +23,7 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Collection;
import org.springframework.cache.annotation.Cacheable;
/**
* JPA implementation of the {@link OwnerRepository} interface.
@ -42,6 +43,7 @@ public class JpaOwnerRepositoryImpl implements OwnerRepository {
@Override
@SuppressWarnings("unchecked")
@Cacheable(value = "ownersByLastName")
public Collection<Owner> findByLastName(String lastName) {
// using 'join fetch' because a single query should load both owners and pets
// using 'left join fetch' because it might happen that an owner does not have pets yet
@ -52,18 +54,13 @@ public class JpaOwnerRepositoryImpl implements OwnerRepository {
@Override
public Owner findById(int id) {
// using 'join fetch' because a single query should load both owners and pets
// using 'left join fetch' because it might happen that an owner does not have pets yet
Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id");
query.setParameter("id", id);
return (Owner) query.getSingleResult();
return em.find(Owner.class, id);
}
@Override
public void save(Owner owner) {
this.em.merge(owner);
em.merge(owner);
}
}

View file

@ -19,6 +19,7 @@ import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.repository.PetRepository;
import org.springframework.stereotype.Repository;
import org.springframework.cache.annotation.Cacheable;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@ -41,6 +42,7 @@ public class JpaPetRepositoryImpl implements PetRepository {
@Override
@SuppressWarnings("unchecked")
@Cacheable(value = "pettype")
public List<PetType> findPetTypes() {
return this.em.createQuery("SELECT ptype FROM PetType ptype ORDER BY ptype.name").getResultList();
}

View file

@ -15,6 +15,7 @@
*/
package org.springframework.samples.petclinic.repository.jpa;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.repository.VisitRepository;
import org.springframework.stereotype.Repository;
@ -51,9 +52,8 @@ public class JpaVisitRepositoryImpl implements VisitRepository {
@Override
@SuppressWarnings("unchecked")
public List<Visit> findByPetId(Integer petId) {
Query query = this.em.createQuery("SELECT visit FROM Visit v where v.pets.id= :id");
query.setParameter("id", petId);
return query.getResultList();
Pet pet = em.find(Pet.class, petId);
return pet.getVisits();
}
}

View file

@ -63,6 +63,7 @@ public class VisitController {
public String processNewVisitForm(@PathVariable("petId") int petId, @Valid Visit visit, BindingResult result) {
Pet pet = this.clinicService.findPetById(petId);
visit.setPet(pet);
pet.addVisit(visit);
if (result.hasErrors()) {
return "pets/createOrUpdateVisitForm";
} else {

32
src/main/resources/ehcache.xml Normal file → Executable file
View file

@ -13,5 +13,37 @@
diskPersistent="false"
diskExpiryThreadIntervalSeconds="1"
memoryStoreEvictionPolicy="LRU"/>
<cache name="ownersByLastName"
timeToLiveSeconds="60"
maxElementsInMemory="10000"/>
<cache name="pettype"
timeToLiveSeconds="600"
maxElementsInMemory="10000"/>
<cache name="org.springframework.samples.petclinic.model.Pet"
timeToLiveSeconds="600"
maxElementsInMemory="10000"/>
<cache name="org.springframework.samples.petclinic.model.Pet.visits"
timeToLiveSeconds="600"
maxElementsInMemory="100000"/>
<cache name="org.springframework.samples.petclinic.model.Visit"
timeToLiveSeconds="600"
maxElementsInMemory="100000"/>
<cache name="org.springframework.samples.petclinic.model.Owner"
timeToLiveSeconds="600"
maxElementsInMemory="10000"/>
<cache name="org.springframework.samples.petclinic.model.Owner.pets"
timeToLiveSeconds="600"
maxElementsInMemory="100000"/>
<cache name="org.springframework.samples.petclinic.model.PetType"
timeToLiveSeconds="600"
maxElementsInMemory="100"/>
</ehcache>

View file

@ -58,6 +58,13 @@
<!-- gDickens: BOTH Persistence Unit and Packages to Scan are NOT compatible, persistenceUnit will win -->
<property name="persistenceUnitName" value="petclinic"/>
<property name="packagesToScan" value="org.springframework.samples.petclinic"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->

View file

@ -43,6 +43,7 @@
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"/>
</bean>