[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