Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

8.5. EntityManager

		
@Repository 
@Transactional(readOnly = true)  
class AccountServiceImpl implements AccountService {  
 
  @PersistenceContext 
  private EntityManager em;  
 
  @Override 
  @Transactional 
  public Account save(Account account) {  
 
    if (account.getId() == null) {  
      em.persist(account);  
      return account;  
    } else {  
      return em.merge(account);  
    }  
  }  
 
  @Override 
  public List<Account> findByCustomer(Customer customer) {  
 
    TypedQuery query = em.createQuery("select a from Account a where a.customer = ?1", Account.class);  
    query.setParameter(1, customer);  
 
    return query.getResultList();  
  }  
  
  @Override 
  public List<Customer> findAll(int page, int pageSize) {  
 
    TypedQuery query = em.createQuery("select c from Customer c", Customer.class);  
 
    query.setFirstResult(page * pageSize);  
    query.setMaxResults(pageSize);  
 
    return query.getResultList();  
  }  
   
}