在java代码中添加布尔值以检查表状态条件

时间:2016-02-25 22:14:21

标签: java hibernate

我正在尝试遍历java中sdid的记录列表。 sdid记录设置为具有相同sdid的最多8条记录,但需要检查每个sdid的状态列,以查看其PEND_ADDPEND_DELACTIVE

因此,对于第一个条件:状态为PEND_ADDPEND_DEL。然后删除记录。

如果状态仅为ACTIVE,则删除记录。

如果状态为ACTIVEPEND_ADD和/或PEND_DEL,则删除PEND_ADD和/或PEND_DEL并保留ACTIVE

        private void disconnectClassOfService(List<Integer> sdidList) throws Exception {

            if (sdidList == null || sdidList.isEmpty()){
                return;
            }

            logger.info("Start deleting sdid : " + sdidList);
            for (Integer sdid : sdidList ) {

                List<CosPolicy2Interface> cosPolicy2InterfaceBySdid = getCosPolicy2InterfaceDAO().getCosPolicy2InterfaceBySdid(sdid);
                if (cosPolicy2InterfaceBySdid != null && !cosPolicy2InterfaceBySdid.isEmpty()){
                    for (CosPolicy2Interface cosPolicySdid: cosPolicy2InterfaceBySdid){
                        if (cosPolicySdid.getStatus().equals("PEND_ADD") ){
                            getPersistMgr().delete(cosPolicySdid);
                        }
                        if (cosPolicySdid.getStatus().equals("PEND_DEL") ){
                            getPersistMgr().delete(cosPolicySdid);
                        }
                        if (cosPolicySdid.getStatus().equals("ACTIVE") ){
                            getPersistMgr().delete(cosPolicySdid);
                        }
                    }
                }

我的hibernate查询

            protected static final String queryBySdid = " SELECT * "
                + " FROM cos_policy2interface "
                + " WHERE  connection_type = 'SDID' AND connection_id = ?";

        @Override
        public List<CosPolicy2Interface> getCosPolicy2InterfaceBySdid(
                long sdid) throws Exception {
            EntityManager em = getEntityManager();
            CosPolicy2Interface CosPolicy2Interface = null;
            Query query = em.createNativeQuery(queryBySdid, CosPolicy2Interface.class);
            query.setParameter(1, sdid);

            @SuppressWarnings("unchecked")
            List<CosPolicy2Interface> cosPolicy2InterfaceList = query.getResultList();
            return cosPolicy2InterfaceList;
        }

表的例子:

CosPolicy2Interface_id,connection_type, connection_id, status
29, 'SDID', 13538806, ACTIVE
30, 'SDID', 13538806, PEND_DEL
31, 'SDID', 13538806, ACTIVE
32, 'SDID', 13538806, PEND_ADD
33, 'SDID', 13538806, PEND_ADD
34, 'SDID', 13538806, ACTIVE

My logic will erased all of it since it will iterate one record at a time. Could I use a boolean? some thing like this how can I implemented.

1) Pen Add -> dont care delete (But set PendingFlag to true)  
2)Pend Del --> dont care delete (Set Pending Flag to true)  
3) Active --> Store them in a list to check later  
     Come out of the for-loop and check if pendingFlag is false and activeCosList is not empty --> delete them

1 个答案:

答案 0 :(得分:1)

我想修改您的disconnectClassOfService方法。

private void disconnectClassOfService(List<Integer> sdidList) throws Exception {

    if (sdidList == null || sdidList.isEmpty()) {
        return;
    }

    logger.info("Start deleting sdid : " + sdidList);
    for (Integer sdid : sdidList) {

        List<CosPolicy2Interface> cosPolicy2InterfaceBySdid = getCosPolicy2InterfaceDAO().getCosPolicy2InterfaceBySdid(sdid);
        if (cosPolicy2InterfaceBySdid != null && !cosPolicy2InterfaceBySdid.isEmpty()) {

            List<CosPolicy2Interface> recordsACTIVE = new ArrayList<>();
            List<CosPolicy2Interface> recordsNonACTIVE = new ArrayList<>();

            for (CosPolicy2Interface cosPolicySdid : cosPolicy2InterfaceBySdid) {
                if (cosPolicySdid.getStatus().equals("PEND_ADD")) {
                    recordsNonACTIVE.add(cosPolicySdid);
                }
                if (cosPolicySdid.getStatus().equals("PEND_DEL")) {
                    recordsNonACTIVE.add(cosPolicySdid);
                }
                if (cosPolicySdid.getStatus().equals("ACTIVE")) {
                    recordsACTIVE.add(cosPolicySdid);
                }
            }
            if ((recordsACTIVE.size() == cosPolicy2InterfaceBySdid.size())) {
                getPersistMgr().deleteAll(cosPolicy2InterfaceBySdid);// Delete all records
            } else {
                // delete non active records
                getPersistMgr().deleteAll(recordsNonACTIVE);
            }
        }
    }
}

这样您就不必单独删除记录了。