2 Replies Latest reply on Sep 25, 2008 1:12 PM by Stuart Douglas

    query from multiple beans

    Jaseem Ambalangadan Newbie
      I have three beans
      GroupBean(Columns:- groupId (@Id @GeneratedValue), GroupName) , MenuBean(Columns:- menuId (@Id @GeneratedValue), menuName) , GroupMenuBean(Columns:- groupId, menuId, permissions
                              unique key(groupId, menuId)

      Is there any annotation for composite key so that I can use in GroupMenuBean. Is it mandatory to use an @Id to be used in GroupMenuBean?

      Which bean should I use while querying for groupId, groupName, menuId, menuName, permissions

      ie select groups.groupId, groups.groupName, menu.menuId, menu.menuName, groupMenu.permissions from GroupBean groups, MenuBean menu, GroupMenuBean groupMenu where groups.groupId = groupMenu.groupId and menu.menuId = groupMenu.menuId
        • 1. Re: query from multiple beans
          Jaseem Ambalangadan Newbie
          I have three beans

          class MenuBean {
                  private Long menuId = null;     (Primary key)
               private String menuName = null;

                  ..........
                  getters and setters
          }

          class GroupBean {
                  private Long groupId = null;      (primary key)
                  private String groupName = null;  

                  ............
                  getters and setters
          }

          class GroupMenuBean {
                  private Long groupMenuId = null; (primary key)
               private MenuBean menuId;          (foreign key)
               private GroupBean userGroupId;    (foreign key)  
               private String objectPermissions = null;

                  ...........
                  getters and setters
          }

          My Query is given below
          List <GroupMenuBean> groupMenuBean = em.createQuery("select groupMenu.groupMenuId, groups.groupId, menu.menuId from GroupBean groups, MenuBean menu, GroupMenuBean groupMenu where
          groupMenu.groupId = groups.groupId and groupObjects.menuId = menu.menuId").getResultList();

          The query works fine. I need to display groupMenuId, groupId and menuId form groupMenuBean object. I tried using Iterator as follows
          Iterator itr = groupMenuBean.iterator;
          while(iterator.hasNext) {
                GroupMenuBean details = (GroupMenuBean) itr.next;
                //Exception occures here.
                System.out.println("Menu Id: " +details.getMenuId);
          }


          • 2. Re: query from multiple beans
            Stuart Douglas Master

            You query is not actually returning a GroupMenuBean, and because of type erasre in java generics it will not be picked up when it gets put into the list, and you will get a ClassCastExcaption when you try and iterate through it. I think what is actually being returned is a list of maps, but I have never really used this type of query and I am half pissed so I am not sure. try something like this:



            Iterator itr = groupMenuBean.iterator;
            while(iterator.hasNext) {
                  Map details = (Map) itr.next;
                  //Exception occures here.
                  System.out.println("Menu Id: " +details.get("getMenuId"));
            }