companycategoryservice.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.Company;

@Transactional
public class CompanyCategoryService implements ICompanyService {
	protected Logger log = LogManager.getLogger(this.getClass().getName());

	private EntityManagerFactory emf;

	public CompanyCategoryService() {
		emf = Persistence.createEntityManagerFactory("vegetaPU");
	}

	@SuppressWarnings("unchecked")
	public List<Company> findAll() {
		log.debug("in the CompanyService findAll() method");
		EntityManager entityMgr = emf.createEntityManager();
		Query query = entityMgr.createQuery("from Company");
		return query.getResultList();
	}

	public Company find(Integer id) {
		log.debug("in the CompanyService find() method");
		EntityManager entityMgr = emf.createEntityManager();
		return entityMgr.find(Company.class, id);
	}

	public boolean create(Company company) {
		log.debug("in the CompanyService create() method");
		boolean result = false;
		EntityManager entityMgr = emf.createEntityManager();
		EntityTransaction tx = null;
		try {
			tx = entityMgr.getTransaction();
			tx.begin();
			company.setCreateddate(new Date());
			entityMgr.persist(company);
			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();
			Company company = (Company) entityMgr.find(Company.class, id);
			entityMgr.remove(company);
			tx.commit();
			result = true;
		} catch (Exception e) {
			if (tx != null && tx.isActive())
				tx.rollback();
			e.printStackTrace();
		}
		return result;

	}

	public boolean update(Company company) {
		log.debug("in the CompanyService update() method");
		boolean result = false;
		EntityManager entityMgr = emf.createEntityManager();
		EntityTransaction tx = null;
		try {
			tx = entityMgr.getTransaction();
			tx.begin();
			company.setChangeddate(new Date());
			entityMgr.merge(company);
			tx.commit();
			result = true;
		} catch (Exception e) {
			if (tx != null && tx.isActive())
				tx.rollback();
		}
		return result;

	}

	public boolean save(Company company) {
		boolean isCreated = false;
		boolean isUpdated = false;
		if (company.getCompanyid() == null) {
			isCreated = create(company);
		} else {
			isUpdated = update(company);
		}
		return isCreated && isUpdated;
	}

}

⌨️ 快捷键说明

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