Skip to content

Commit

Permalink
Adding cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Dubois committed Mar 2, 2013
1 parent 6810267 commit 05a60b7
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 12 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Expand Up @@ -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>
Expand Down
Empty file.
5 changes: 4 additions & 1 deletion src/main/java/org/springframework/samples/petclinic/model/Owner.java 100644 → 100755
Expand Up @@ -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;
Expand All @@ -34,6 +35,7 @@
*/
@Entity
@Table(name = "owners")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Owner extends Person {
@Column(name = "address")
@NotEmpty
Expand All @@ -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;


Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/springframework/samples/petclinic/model/Pet.java 100644 → 100755
Expand Up @@ -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;
Expand All @@ -33,6 +34,7 @@
*/
@Entity
@Table(name = "pets")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Pet extends NamedEntity {

@Column(name = "birth_date")
Expand All @@ -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;


Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/springframework/samples/petclinic/model/PetType.java 100644 → 100755
Expand Up @@ -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;

Expand All @@ -23,6 +26,7 @@
*/
@Entity
@Table(name = "types")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class PetType extends NamedEntity {

}
2 changes: 2 additions & 0 deletions src/main/java/org/springframework/samples/petclinic/model/Visit.java 100644 → 100755
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@
*/
@Entity
@Table(name = "visits")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Visit extends BaseEntity {

/**
Expand Down
Expand Up @@ -23,6 +23,7 @@
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.
Expand All @@ -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
Expand All @@ -52,18 +54,13 @@ public Collection<Owner> findByLastName(String lastName) {

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

}
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -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();
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -51,9 +52,8 @@ public void save(Visit visit) {
@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();
}

}
Expand Up @@ -63,6 +63,7 @@ public String initNewVisitForm(@PathVariable("petId") int petId, Model model) {
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 {
Expand Down
32 changes: 32 additions & 0 deletions src/main/resources/ehcache.xml 100644 → 100755
Expand Up @@ -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>
7 changes: 7 additions & 0 deletions src/main/resources/spring/business-config.xml
Expand Up @@ -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) -->
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/spring/tools-config.xml
Expand Up @@ -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>


Expand Down

0 comments on commit 05a60b7

Please sign in to comment.