<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    數據加載中……
    在j2ee環境中ejb3.0的session bean操作entity bean的通用類 ManyActionSessionBean
    package com.vsc.study.ejb.sessionbean;

    import com.vsc.study.ejb.entitybean.Function;
    import com.vsc.study.ejb.remote.ManyActionSessionRemote;
    import com.vsc.study.ejb.util.LoggerInterceptor;
    import java.lang.reflect.Method;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import javax.ejb.Stateless;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.interceptor.Interceptors;

    @Stateless
    //@Interceptors({LoggerInterceptor.class})
    public class ManyActionSessionBean implements ManyActionSessionRemote {
        
        @PersistenceContext
        private EntityManager em;
       
        public Map getCurrentResults(int currentPage, int pageSize,
                int maxResultSet,String querySql) {
            if(maxResultSet==0){
                maxResultSet = em.createQuery(querySql).getResultList().size();
            }
            List list = em.createQuery(querySql).setFirstResult(
                    (currentPage - 1) * pageSize).setMaxResults(pageSize)
                    .getResultList();
            HashMap hashMap = new HashMap();
            hashMap.put(maxResultSet, list);
            return hashMap;
        }
       
        public Map getCurrentResults(int currentPage, int pageSize,
                int maxResultSet,Object object) {
            if(maxResultSet==0){
                maxResultSet = em.createQuery("from " + object.getClass().getSimpleName()).getResultList().size();
            }
            List list = em.createQuery("from " + object.getClass().getSimpleName())
            .setFirstResult((currentPage - 1) * pageSize).setMaxResults(
                    pageSize).getResultList();
            HashMap hashMap = new HashMap();
            hashMap.put(maxResultSet, list);
            return hashMap;
        }
       
        public int remove(Object object) {
            if (object == null)
                return -1;
            try {
                em.remove(this.search(object));
                return 0;
            } catch (Exception e) {
                return -2;
            }
        }
       
        public int[] removes(Object[] objects) {
            if(objects==null)
                throw new NullPointerException();
            int[] results = new int[objects.length];
            for (int i = 0; i < objects.length; i++) {
                Object obj = em.find(objects.getClass(), this.getId(objects[i]));
                if (obj != null) {
                    results[i] = this.remove(obj);
                }
            }
            return results;
        }
       
        public int update(Object object) {
            if (object == null || this.getId(object) == null)
                return -1;
            Object obj = this.search(object);
            if (obj == null)
                return -2;
            this.updateEntityBean(object, obj);
            try {
                em.merge(obj.getClass().cast(obj));
                return 0;
            } catch (Exception e) {
                return -1;
            }
        }
       
        public Object search(Object object) {
            if (object == null || this.getId(object) == null)
                return -1;
            try {
                return em.find(object.getClass(), this.getId(object));
            } catch (Exception e) {
                e.printStackTrace();
                return -1;
            }
        }
       
        public List  searchAll(Object object) {
            if (object == null)
                return null;
            try {
                return em.createQuery("from " + object.getClass().getSimpleName()).getResultList();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
       
        public Long insert(Object object) {
            try {
                em.persist(object.getClass().cast(object));
                return this.getId(object);
            } catch (Exception e) {
                e.printStackTrace();
                return -1l;
            }
        }
       
        public Long getId(Object object) {
            if (object == null)
                return -1l;
            try {
                return (Long) object.getClass().getDeclaredMethod("getId").invoke(
                        object);
            } catch (Exception e) {
                e.printStackTrace();
                return -5l;
            }
        }
       
        /**
         * obj is update Object
         *
         * @param object
         * @param obj
         */
        public void updateEntityBean(Object object, Object obj) {
            try {
                Method[] methods = object.getClass().getDeclaredMethods();
                Method method;
                Object objectMethodValue;
                Object objMethodValue;
                for (int i = 0; i < methods.length; i++) {
                    if (methods[i].getName().trim().startsWith("get")) {
                        if (methods[i].invoke(object) == null) {
                            System.out
                                    .println("----------this field is null--------------");
                            continue;
                        }
                        objectMethodValue = methods[i].invoke(object);
                        objMethodValue = methods[i].invoke(obj);
                        if (objectMethodValue.equals(objMethodValue)) {
                            System.out
                                    .println("----------field is equals--------------");
                            continue;
                        }
                        method = obj.getClass().getDeclaredMethod(
                                methods[i].getName().trim().replaceFirst("g", "s"),
                                methods[i].getReturnType());
                        method.invoke(obj, objectMethodValue);
                        System.out.println("==============="
                                + methods[i].invoke(obj));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    posted on 2008-02-25 15:23 java flex 結合 閱讀(867) 評論(1)  編輯  收藏 所屬分類: ejb 3.0

    評論

    # re: 在j2ee環境中ejb3.0的session bean操作entity bean的通用類 ManyActionSessionBean 2009-07-16 17:35 找個美女做老婆

      回復  更多評論    
    主站蜘蛛池模板: 亚洲AV日韩精品久久久久久| 自拍偷自拍亚洲精品播放| 亚洲一区免费观看| 亚洲综合色丁香婷婷六月图片| 亚洲爆乳AAA无码专区| 一个人看的hd免费视频| 免费人妻无码不卡中文字幕系| 成人免费看黄20分钟| 亚洲色欲一区二区三区在线观看| 亚洲精品日韩专区silk| 日韩大片免费观看视频播放| 91香蕉国产线观看免费全集| 狠狠久久永久免费观看| 亚洲成A∨人片在线观看不卡| 亚洲一线产区二线产区精华| 成年免费a级毛片| 很黄很黄的网站免费的| 亚洲精品高清在线| 亚洲一区在线视频| 免费看成人AA片无码视频吃奶| 毛片基地免费视频a| 国产aⅴ无码专区亚洲av| 亚洲风情亚Aⅴ在线发布| 4399影视免费观看高清直播| 亚洲成av人片在线观看天堂无码| 亚洲综合无码一区二区三区| 一级午夜免费视频| 毛片免费观看的视频| 亚洲一二成人精品区| 九九免费观看全部免费视频| AV免费网址在线观看| 亚洲成色在线综合网站| 美女被爆羞羞网站免费| 免费在线看v网址| 亚洲国产精品久久久久婷婷老年| 一区免费在线观看| 在线免费观看韩国a视频| 亚洲一级高清在线中文字幕| 久久久久久国产精品免费免费男同 | 成人免费在线视频| 亚洲福利视频网址|