petclinic-uncached
Changes
src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/JpaOwnerRepositoryImpl.java 52(+52 -0)
src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java 20(+0 -20)
src/main/resources/db_readme.txt 0(+0 -0)
Details
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/JpaOwnerRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/JpaOwnerRepositoryImpl.java
new file mode 100644
index 0000000..6e88ea0
--- /dev/null
+++ b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/JpaOwnerRepositoryImpl.java
@@ -0,0 +1,52 @@
+package org.springframework.samples.petclinic.repository.springdatajpa;
+
+import java.util.Collection;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+
+import org.springframework.samples.petclinic.Owner;
+import org.springframework.samples.petclinic.repository.OwnerRepository;
+import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+
+/**
+ * Using native JPA here because of this query:
+ * "SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName"
+ * See https://jira.springsource.org/browse/DATAJPA-292 for more details.
+ *
+ * @author Michael Isvy
+ */
+@Repository
+@Transactional
+public class JpaOwnerRepositoryImpl implements OwnerRepository {
+
+ @PersistenceContext
+ private EntityManager em;
+
+
+ @SuppressWarnings("unchecked")
+ 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
+ Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName");
+ query.setParameter("lastName", lastName + "%");
+ return query.getResultList();
+ }
+
+ 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();
+ }
+
+
+ public void save(Owner owner) {
+ this.em.merge(owner);
+
+ }
+
+}
diff --git a/src/main/resources/spring/applicationContext-dao.xml b/src/main/resources/spring/applicationContext-dao.xml
index 89e79eb..f74d0cf 100644
--- a/src/main/resources/spring/applicationContext-dao.xml
+++ b/src/main/resources/spring/applicationContext-dao.xml
@@ -117,6 +117,9 @@
<beans profile="spring-data-jpa">
<jpa:repositories base-package="org.springframework.samples.petclinic.repository.springdatajpa"/>
+
+ <!-- Custom configuration for the custom implementation based on JPA -->
+ <bean id="owerRepository" class="org.springframework.samples.petclinic.repository.springdatajpa.JpaOwnerRepositoryImpl"/>
</beans>
</beans>
\ No newline at end of file