⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 entitymanagerclinic.java

📁 随书光盘:精通Sping 2.0 的随书源代码
💻 JAVA
字号:
package org.springframework.samples.petclinic.jpa;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;
import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.Owner;
import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.Vet;
import org.springframework.samples.petclinic.Visit;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * JPA implementation of the Clinic interface using EntityManager.
 *
 * <p>The mappings are defined in "orm.xml"
 * located in the META-INF dir.
 *
 * @author Mike Keith
 * @author Rod Johnson
 * @since 22.4.2006
 */
@Repository
@Transactional
public class EntityManagerClinic implements Clinic {
	
	@PersistenceContext
	private EntityManager em;

	public Collection<Vet> getVets() throws DataAccessException {
		return em.createQuery("SELECT vet FROM Vet vet ORDER BY vet.lastName, vet.firstName").getResultList();
	}

	public Collection<PetType> getPetTypes() throws DataAccessException {
		return em.createQuery("SELECT ptype FROM PetType ptype ORDER BY ptype.name").getResultList();
	}

	public Collection<Owner> findOwners(String lastName) throws DataAccessException {
		Query query = em.createQuery("SELECT owner FROM Owner owner WHERE owner.lastName LIKE :lastName");
		query.setParameter("lastName", lastName + "%");
		return query.getResultList();
	}

	public Owner loadOwner(int id) throws DataAccessException {
		return em.find(Owner.class, id);	
	}

	public Pet loadPet(int id) throws DataAccessException {
		return em.find(Pet.class, id);
	}

	public void storeOwner(Owner owner) throws DataAccessException {
		em.merge(owner);
	}

	public void storeOwnerSave(Owner owner) throws DataAccessException {
		Owner own = em.merge(owner);
		own.setCity("gz");
	}

	public void storePet(Pet pet) throws DataAccessException {
		em.merge(pet);
	}

	public void storeVisit(Visit visit) throws DataAccessException {
		em.merge(visit);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -