companyactivitystatusservice.java

来自「Struts2 + Spring JPA Hibernate demo.」· Java 代码 · 共 112 行

JAVA
112
字号
package com.vegeta.service.company.activity;

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.activity.CompanyActivityStatus;

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

	private EntityManagerFactory emf;

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

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

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

	public boolean create(CompanyActivityStatus companyActivitiesStatus) {
		log.debug("in the CompanyActivityStatusService create() method");
		boolean result = false;
		EntityManager entityMgr = emf.createEntityManager();
		EntityTransaction tx = null;
		try {
			tx = entityMgr.getTransaction();
			tx.begin();
			entityMgr.persist(companyActivitiesStatus);
			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 CompanyActivityStatusService remove() method");
		boolean result = false;
		EntityManager entityMgr = emf.createEntityManager();
		EntityTransaction tx = null;

		try {
			tx = entityMgr.getTransaction();
			tx.begin();
			CompanyActivityStatus companyActivitiesStatus = (CompanyActivityStatus) entityMgr.find(CompanyActivityStatus.class, id);
			entityMgr.remove(companyActivitiesStatus);
			tx.commit();
			result = true;
		} catch (Exception e) {
			if (tx != null && tx.isActive())
				tx.rollback();
			e.printStackTrace();
		}
		return result;

	}

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

	}

	public boolean save(CompanyActivityStatus companyActivitiesStatus) {
		boolean isCreated = false;
		boolean isUpdated = false;
		if (companyActivitiesStatus.getId() == null) {
			isCreated = create(companyActivitiesStatus);
		} else {
			isUpdated = update(companyActivitiesStatus);
		}
		return isCreated && isUpdated;
	}

}

⌨️ 快捷键说明

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