companyaddressservice.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.CompanyAddress;
@Transactional
public class CompanyAddressService implements ICompanyAddressService {
protected Logger log = LogManager.getLogger(this.getClass().getName());
private EntityManagerFactory emf;
public CompanyAddressService() {
emf = Persistence.createEntityManagerFactory("vegetaPU");
}
@SuppressWarnings("unchecked")
public List<CompanyAddress> findAll() {
log.debug("in the CompanyAddressService findAll() method");
EntityManager entityMgr = emf.createEntityManager();
Query query = entityMgr.createQuery("from CompanyAddress");
return query.getResultList();
}
public CompanyAddress find(Integer id) {
log.debug("in the CompanyAddressService find() method");
EntityManager entityMgr = emf.createEntityManager();
return entityMgr.find(CompanyAddress.class, id);
}
public boolean create(CompanyAddress companyAddress) {
log.debug("in the CompanyAddressService create() method");
boolean result = false;
EntityManager entityMgr = emf.createEntityManager();
EntityTransaction tx = null;
try {
tx = entityMgr.getTransaction();
tx.begin();
companyAddress.setCreateddate(new Date());
entityMgr.persist(companyAddress);
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 CompanyAddressService remove() method");
boolean result = false;
EntityManager entityMgr = emf.createEntityManager();
EntityTransaction tx = null;
try {
tx = entityMgr.getTransaction();
tx.begin();
CompanyAddress companyAddress = (CompanyAddress) entityMgr.find(CompanyAddress.class, id);
entityMgr.remove(companyAddress);
tx.commit();
result = true;
} catch (Exception e) {
if (tx != null && tx.isActive())
tx.rollback();
e.printStackTrace();
}
return result;
}
public boolean update(CompanyAddress companyAddress) {
log.debug("in the CompanyAddressService update() method");
boolean result = false;
EntityManager entityMgr = emf.createEntityManager();
EntityTransaction tx = null;
try {
tx = entityMgr.getTransaction();
tx.begin();
companyAddress.setChangeddate(new Date());
entityMgr.merge(companyAddress);
tx.commit();
result = true;
} catch (Exception e) {
if (tx != null && tx.isActive())
tx.rollback();
}
return result;
}
public boolean save(CompanyAddress companyAddress) {
boolean isCreated = false;
boolean isUpdated = false;
if (companyAddress.getAddressid() == null) {
isCreated = create(companyAddress);
} else {
isUpdated = update(companyAddress);
}
return isCreated && isUpdated;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?