[jboss-user] [EJB 3.0] - Re: SOS

zhuhuapeng@gmail.com do-not-reply at jboss.com
Tue Aug 26 09:58:03 EDT 2008


CommonDBAction:

  | package jpa.common;
  | 
  | import java.util.List;
  | 
  | import javax.ejb.TransactionAttribute;
  | import javax.ejb.TransactionAttributeType;
  | import javax.persistence.EntityManager;
  | import javax.persistence.FlushModeType;
  | import javax.persistence.Query;
  | 
  | 
  | import jpa.common.client.CommonUtil;
  | import jpa.common.client.DataInfo;
  | import jpa.entity.cattle.SmallToad;
  | import jpa.exp.client.DuplicateObjExp;
  | 
  | /**
  |  * 该类中针对一些通用的数据库操作方式提供了封è£
.
  |  * 从而为service中的调用提供了支持,简化了代码.
  |  * @author howard.zhu
  |  *
  |  */
  | public class CommonDBAction {
  |  
  | 	/**
  | 	 * 删除一个对象,此时需要提供一个根据ID查询得到的对象.
  | 	 * @param <T>
  | 	 * @param id
  | 	 * @param em
  | 	 * @param eo
  | 	 * @return
  | 	 */
  | 	//@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
  | 	public static  <T extends SmallToad> int  deleteObjById(int id,EntityManager em ,T eo ) {
  | 		if(id < 0 )
  | 		{
  | 		   return CommonUtil.persistResult.INVALID_BEDELETEDOBJ;
  | 		}
  | 		
  | 		if(null == eo )
  | 		{
  | 		   return CommonUtil.persistResult.INVALID_BEDELETEDOBJ;
  | 		}
  | 		if(eo.getDataInfo().equals(DataInfo.BASIC))
  | 		{
  | 			//受保护的数据,不å
è®¸åˆ é™¤.
  | 		   return CommonUtil.persistResult.PROTECTED_OBJ;	
  | 		}else
  | 		{
  | 		   em.remove(eo);
  | 		}
  |            return CommonUtil.persistResult.SUCCESS ; 
  | 	}
  | 	
  | 	/**
  | 	 * 查询所有的该类对象.
  | 	 * @return
  | 	 */
  | 	@SuppressWarnings("unchecked")
  | 	public  static  <T> List<T> queryAllObjs(String namedQuery,EntityManager em,Class<? extends Object> T) {
  | 		Query query = em.createNamedQuery(namedQuery);
  | 		//不处理查询失败的æƒ
况,没有查询到值,则默认返回一个null; 
  | 		if(null == query)
  | 		{
  | 			return null; 
  | 		}
  | 		
  | 		return (List<T>)query.getResultList();
  | 	}
  | 	
  | 	
  | 	@SuppressWarnings("unchecked")
  | 	public  static <T> T queryByID(int id,EntityManager em,String namedQuery,String paramName ,Class<? extends Object> T) {
  | 		if(id < 0)
  | 		{
  | 			return null; 
  | 		}
  | 		Query query = em.createNamedQuery(namedQuery).setParameter(paramName, id);
  | 		
  | 	    if(null == query)
  | 	    {
  | 	    	return null; 
  | 	    }
  | 	     if(null == query.getResultList() || query.getResultList().size() <= 0)
  | 	     {
  | 	    	 return null; 
  | 	     }
  | 		return (T)query.getSingleResult();
  | 	}
  | 
  | 	
  | 	@SuppressWarnings("unchecked")
  | 	public static  <T> T queryByName(String name,EntityManager em,String namedQuery,String paramName ,Class<? extends Object> T) {
  | 		if(null == name || "".equals(name.trim()))
  | 		{
  | 			return null; 
  | 		}
  | 		Query query = em.createNamedQuery(namedQuery).setParameter(paramName, name);
  | 	    if(null == query)
  | 	    {
  | 	    	return null; 
  | 	    }
  | 	     if(null == query.getResultList() || query.getResultList().size() <= 0)
  | 	     {
  | 	    	 return null; 
  | 	     }
  | 		return (T)query.getSingleResult();
  | 	}
  | 
  | 	
  | 	
  | 	public static  <T extends SmallToad> int storeObj(T  eo,EntityManager em,List<T> mayMultyObjs) {
  | 		
  | 		if(!eo.checkInsertable())
  | 		{
  | 			//无效的对象
  | 			return CommonUtil.persistResult.INVALID_OBJECT;
  | 		}
  | 		int result = CommonUtil.persistResult.FAILURE;
  | 		if(null != mayMultyObjs && (mayMultyObjs.size() > 0) )
  | 		{
  | 		   for(T t : mayMultyObjs)
  | 		   {
  | 			   if(null != t)
  | 			   {
  | 				   return CommonUtil.persistResult.ALREADY_EXIST;
  | 			   }
  | 		   }
  | 		}
  | 	    em.persist(eo);
  | 	    result =  eo.getIdAfterInsert();
  | 		return result;
  | 	}
  | 	
  | 
  | 	public static  <T extends SmallToad> T updateObj(T eo,EntityManager em,List<T> mayMultyObjs,T existsObj)
  | 	throws DuplicateObjExp {
  | 
  | 		if(!eo.checkupdateble())
  | 		{
  | 			//无效的对象
  | 			return null ;
  | 		}
  | 		if(null != mayMultyObjs)
  | 		{
  | 			for(T t : mayMultyObjs)
  | 			{
  | 				if(null != t && (t.getIdAfterInsert() != eo.getIdAfterInsert()))
  | 				{
  | 					//对象已经存在
  | 					throw new DuplicateObjExp("multy stype in db! update failure !");
  | 				}
  | 			}
  | 		}
  | 		
  | 		if(null == existsObj)
  | 		{
  | 			return null; 
  | 		}else{
  | 			if(eo.getDataInfo().equals(DataInfo.BASIC))
  | 			{
  | 				//基础数据,不更新.
  | 				return existsObj; 
  | 			}else
  | 			{
  | 			    existsObj.copy(eo);
  | 			    return em.merge(existsObj);
  | 			}
  | 		}
  |     }
  | 	
  | }
  | 
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4172586#4172586

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4172586




More information about the jboss-user mailing list