This content has been marked as final.
Show 2 replies
-
1. Re: query from multiple beans
jaseem Sep 25, 2008 11:49 AM (in response to jaseem)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
swd847 Sep 25, 2008 1:12 PM (in response to jaseem)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")); }