mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-04-25 03:42:48 +00:00
replacing one Spring Data Repo with native JPA
see https://jira.springsource.org/browse/DATAJPA-292
This commit is contained in:
parent
8f531f366f
commit
5b8ef8ea10
4 changed files with 55 additions and 20 deletions
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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>
|
Loading…
Reference in a new issue