companybankservice.java
来自「Struts2 + Spring JPA Hibernate demo.」· Java 代码 · 共 115 行
JAVA
115 行
package com.vegeta.service.company;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import com.vegeta.model.company.CompanyBank;
@Transactional
public class CompanyBankService implements ICompanyBankService {
protected Logger log = LogManager.getLogger(this.getClass().getName());
private EntityManagerFactory emf;
public CompanyBankService() {
emf = Persistence.createEntityManagerFactory("vegetaPU");
}
@SuppressWarnings("unchecked")
public List<CompanyBank> findAll() {
log.debug("in the CompanyService findAll() method");
EntityManager entityMgr = emf.createEntityManager();
Query query = entityMgr.createQuery("from CompanyBank");
return query.getResultList();
}
public CompanyBank find(Integer id) {
log.debug("in the CompanyService find() method");
EntityManager entityMgr = emf.createEntityManager();
return entityMgr.find(CompanyBank.class, id);
}
public boolean create(CompanyBank companyBank) {
log.debug("in the CompanyService create() method");
boolean result = false;
EntityManager entityMgr = emf.createEntityManager();
EntityTransaction tx = null;
try {
tx = entityMgr.getTransaction();
tx.begin();
companyBank.setCreateddate(new Date());
entityMgr.persist(companyBank);
tx.commit();
result = true;
} catch (Exception e) {
if (tx != null && tx.isActive())
tx.rollback();
}
return result;
}
public boolean remove(Integer id) {
log.debug("in the CompanyService remove() method");
boolean result = false;
EntityManager entityMgr = emf.createEntityManager();
EntityTransaction tx = null;
try {
tx = entityMgr.getTransaction();
tx.begin();
CompanyBank companyBank = (CompanyBank) entityMgr.find(CompanyBank.class, id);
entityMgr.remove(companyBank);
tx.commit();
result = true;
} catch (Exception e) {
if (tx != null && tx.isActive())
tx.rollback();
e.printStackTrace();
}
return result;
}
public boolean update(CompanyBank companyBank) {
log.debug("in the CompanyService update() method");
boolean result = false;
EntityManager entityMgr = emf.createEntityManager();
EntityTransaction tx = null;
try {
tx = entityMgr.getTransaction();
tx.begin();
companyBank.setChangeddate(new Date());
entityMgr.merge(companyBank);
tx.commit();
result = true;
} catch (Exception e) {
if (tx != null && tx.isActive())
tx.rollback();
}
return result;
}
public boolean save(CompanyBank companyBank) {
boolean isCreated = false;
boolean isUpdated = false;
if (companyBank.getBankid() == null) {
isCreated = create(companyBank);
} else {
isUpdated = update(companyBank);
}
return isCreated && isUpdated;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?