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 0000000000000000000000000000000000000000..6e88ea019d980a1f7a95a206d28e36ec724ede4c --- /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/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java b/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java deleted file mode 100644 index 655c8e7435eaf764a9200c572955d06a86b4cce7..0000000000000000000000000000000000000000 --- a/src/main/java/org/springframework/samples/petclinic/repository/springdatajpa/SpringDataOwnerRepository.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.springframework.samples.petclinic.repository.springdatajpa; - -import java.util.Collection; - -import org.springframework.dao.DataAccessException; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.Repository; -import org.springframework.samples.petclinic.Owner; -import org.springframework.samples.petclinic.repository.OwnerRepository; - -/** - * - * @author Michael Isvy - * @since 15.1.2013 - */ -public interface SpringDataOwnerRepository extends OwnerRepository, Repository<Owner, Integer> { - - @Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%") - Collection<Owner> findByLastName(String lastName) throws DataAccessException; -} diff --git a/src/main/resources/db/db_readme.txt b/src/main/resources/db_readme.txt similarity index 100% rename from src/main/resources/db/db_readme.txt rename to src/main/resources/db_readme.txt diff --git a/src/main/resources/spring/applicationContext-dao.xml b/src/main/resources/spring/applicationContext-dao.xml index 89e79eb6053a59be0347dde34ba79f6b0440efe4..f74d0cf4ca637b599d9696c473c6470c9f9342a1 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