companyactivitiesdocsservice.java

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

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

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

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

	private EntityManagerFactory emf;

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

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

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

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

		try {
			tx = entityMgr.getTransaction();
			tx.begin();
			CompanyActivitiesDocs company = (CompanyActivitiesDocs) entityMgr.find(CompanyActivitiesDocs.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(CompanyActivitiesDocs companyActivitiesDocs) {
		log.debug("in the CompanyActivitiesDocsService update() method");
		boolean result = false;
		EntityManager entityMgr = emf.createEntityManager();
		EntityTransaction tx = null;
		try {
			tx = entityMgr.getTransaction();
			tx.begin();
			entityMgr.merge(companyActivitiesDocs);
			tx.commit();
			result = true;
		} catch (Exception e) {
			if (tx != null && tx.isActive())
				tx.rollback();
		}
		return result;

	}

	public boolean save(CompanyActivitiesDocs companyActivitiesDocs) {
		boolean isCreated = false;
		boolean isUpdated = false;
		if (companyActivitiesDocs.getActivityid() == null) {
			isCreated = create(companyActivitiesDocs);
		} else {
			isUpdated = update(companyActivitiesDocs);
		}
		return isCreated && isUpdated;
	}

}

⌨️ 快捷键说明

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