JPA service里面的两个文件

package com.hunqing.user.service;

import com.hunqing.user.entity.User;

/**
 * 用户操作接口
 * 
 * @author
 * 
 * <p>Modification History:</p>
 * <p>Date       Author      Description</p>
 * <p>------------------------------------------------------------------</p>
 * <p>  </p>
 * <p>  </p>
 */
public interface IUserService {
	
	/**
	 * Save.
	 * 
	 * @param user the user
	 */
	public void save(User user);
	
	/**
	 * Delete.
	 * 
	 * @param String userName
	 */
	public void delete(String userName);
	
	/**
	 * Update.
	 * 
	 * @param user the user
	 */
	public void update(User user);
	
	/**
	 * Find.
	 * 
	 * @param user the user
	 */
	public User find(String userName);

}

 

 

 

package com.hunqing.user.service;

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

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hunqing.user.entity.User;

@Service @Transactional
public class UserService implements IUserService {
	
	@PersistenceContext
	EntityManager em;

	public void delete(String userName) {   //throws XXX  //抛出的例外叫用户例外,客户端捕获
											//运行期的异常叫运行期例外  spring会捕获运行期例外,自动回滚事物
											//客户例外不会回滚事物

		em.remove(em.getReference(User.class, userName));
	}

	public User find(String userName) {
		return em.find(User.class,userName);
	}

	public void save(User user) {

		em.persist(user);
	}

	public void update(User user) {
		em.merge(user);

	}

}
 

你可能感兴趣的:(service)