cyclic dependency while fetching employees and settings into department

32 views Asked by At

I have "department" and "employee" tables , and I made class object for both .

 UserTable 
    userId userName departmentId
    1        abc      2 
    2        xyz      1


 Department table
  departmentId depName   depHeadId
   1           R&D        2  
   2           IT         1

    public List<User> getAllUsers(){
        List<User> mUserList= null;
        // mDbHelper=DBHelper.getDBHelperInstance(mContext);
        if(mDbHelper==null){
            // todo: handle this case
            return null;

        }
        mUserList=new ArrayList<>();
        Cursor mCursor=null;
        SQLiteDatabase mReadableDB= mDbHelper.getReadableDatabase();
        try{
            mCursor=mReadableDB.rawQuery("SELECT * FROM User",null);
            if(mCursor==null){
                return null;
            }
            if(mCursor.moveToFirst()) {
                do {
                    User user= new User();
                    user.setUserId(mCursor.getInt(mCursor.getColumnIndex("userId")));
                    user.setUserName(mCursor.getString(mCursor.getColumnIndex("userName")));

                    Department department= getDepartmentOfUser(mCursor.getInt(mCursor.getColumnIndex("departmentId")));
                    user.setDepartment(department);

                    if(department==null){
                        //Todo: handled this case
                        return null;
                    }
                    mUserList.add(user)

                } while (mCursor.moveToNext());
            }

        }catch (SQLiteException e){
            // ToDO: Add Exception to log file

        }finally {
            if(mCursor!=null){
                mCursor.close();
            }

        }

        return mUserList;
    }


   public Department getDepartment(int departmentId) {
      //  mDbHelper=DBHelper.getDBHelperInstance(mContext);
        Department department=null;
        if(mDbHelper==null){
            // need to log about the condition
            return null;

        }
        Cursor mCursor=null;
        SQLiteDatabase mReadableDB= mDbHelper.getReadableDatabase();
        try{
            mCursor=mReadableDB.rawQuery("SELECT * FROM DepartmentMaster where departmentId=?",new String[]{String.valueOf(departmentId)});
            if(mCursor==null){
                // need to log about the condition
                return null;
            }

            if(mCursor.moveToFirst()){
                department=new Department();
                department.setId(departmentId);
                department.setName(mCursor.getString(mCursor.getColumnIndex("depName")));
                department.setDepartMentHead(getUser(mCursor.getInt(mCursor.getColumnIndex("depHeadId"))));
            }


        }catch (SQLiteException e){
            // ToDO: Add Exception to log file

        }finally {
            if(mCursor!=null){
                mCursor.close();
            }

        }

        return department;
   }

I again need to call getUser(userId), Now i just stuck and its been cyclic dependency between depaartment and user . Can anyone help me to correct my class design or db design , to easily get my required data .

0

There are 0 answers