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

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

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

    數(shù)據(jù)加載中……
    在j2ee環(huán)境中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 結(jié)合 閱讀(868) 評(píng)論(1)  編輯  收藏 所屬分類: ejb 3.0

    評(píng)論

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

    主站蜘蛛池模板: 免费国产在线观看老王影院| 国内精品久久久久影院亚洲| 日韩精品无码免费视频| 最近高清国语中文在线观看免费| 亚洲国产成人久久笫一页| 无码天堂va亚洲va在线va| 最近中文字幕完整免费视频ww| 日本无卡码免费一区二区三区| 亚洲gv白嫩小受在线观看| 亚洲Av永久无码精品一区二区| 2022久久国产精品免费热麻豆| 亚洲国产精品尤物yw在线| 国产免费高清69式视频在线观看 | 91制片厂制作传媒免费版樱花| 亚洲AV天天做在线观看| 57pao一国产成永久免费| 亚洲五月综合网色九月色| 久久九九全国免费| 亚洲A∨午夜成人片精品网站| 特黄特色的大片观看免费视频| 国产国拍精品亚洲AV片| 无码AV片在线观看免费| 亚洲啪啪综合AV一区| 91制片厂制作传媒免费版樱花| 亚洲一区二区三区乱码A| 亚洲成AV人片在WWW| 无遮免费网站在线入口| 亚洲AV女人18毛片水真多| 亚洲中文字幕无码日韩| 国产h视频在线观看网站免费| 亚洲欧美第一成人网站7777| 在线视频免费观看高清| 久久亚洲精品成人AV| 情侣视频精品免费的国产| 岛国精品一区免费视频在线观看| 亚洲国产精品综合一区在线| 免费人成在线观看网站视频 | 成年大片免费高清在线看黄| 亚洲电影一区二区| 性做久久久久免费看| 亚洲男同gay片|