当前页面: 开发资料首页 → Eclipse 专题 → Eclipse快速上手EJB -- 5. 一对多的双向关系的CMR(2)
摘要: Eclipse快速上手EJB -- 5. 一对多的双向关系的CMR(2)
UserManagementBean中有关 GroupBean 的部分(很简单)
</td></tr> <tr> <td style="BORDER-TOP-STYLE: none; BORDER-TOP-COLOR: rgb(236,233,216)" vAlign=top width="100%" bgColor=#edefd7>//********************************************************************** // 一对多的双向关系 //********************************************************************** /** * 创建组别 * @throws CreateException * @ejb.interface-method view-type="remote" */ public void createGroup(String groupName, String description) throws CreateException { groupHome.create(groupName, description); } /** * 删除组别 * @throws RemoveException * @throws EJBException * @ejb.interface-method view-type="remote" */ public void removeGroup(String groupName) throws EJBException, RemoveException { groupHome.remove(groupName); } /** * 显示所有组别 * @throws FinderException * @ejb.interface-method view-type="remote" */ public ArrayList getGroups() throws FinderException { ArrayList groupList = new ArrayList(30); Iterator iter = groupHome.findAll().iterator(); while (iter.hasNext()) { GroupLocal group = (GroupLocal) iter.next(); groupList.add(group.getName()); } return groupList; } /** * 将用户添加到组 * @throws FinderException * @ejb.interface-method view-type="remote" */ public void moveUserToGroup(String email, String groupName) throws FinderException { UserInfoLocal user = infoHome.findByPrimaryKey(email); GroupLocal group = groupHome.findByPrimaryKey(groupName); user.setGroup(group); } /** * 验证用户所在组 * @throws FinderException * @ejb.interface-method view-type="remote" */ public boolean inGroup(String email, String groupName) throws FinderException { UserInfoLocal user = infoHome.findByPrimaryKey(email); return user.getGroup().getName().equals(groupName); } /** * 给一组用户增加权限 * @throws FinderException * @ejb.interface-method view-type="remote" */ public void addRoleToUsers(String groupName, String roleName) throws FinderException { GroupLocal group = groupHome.findByPrimaryKey(groupName); RoleLocal role = roleHome.findByPrimaryKey(roleName); Iterator iter = group.getUsers().iterator(); while (iter.hasNext()) { UserInfoLocal user = (UserInfoLocal) iter.next(); user.getUser().getRoles().add(role); } } /** * 通过 ejb.finder 输出某组别的用户 * @throws FinderException * @ejb.interface-method view-type="remote" */ public ArrayList getUserIDsInGroup1(String groupName) throws FinderException { ArrayList userList = new ArrayList(30); GroupLocal group = groupHome.findByPrimaryKey(groupName); Iterator iter = group.getUsers().iterator(); while (iter.hasNext()) { UserInfoLocal element = (UserInfoLocal) iter.next(); userList.add(element.getEmail()); } return userList; } /** * 通过 ejb.select 输出某组别的用户 * Business method * @throws FinderException * @ejb.interface-method view-type = "remote" */ public ArrayList getUserIDsInGroup2(String groupName) throws FinderException { return groupHome.getUserIDs(groupName); }</td></tr></table>
CMPClient4.java
/* * 创建日期 2005-1-26 * * 作者:javamxj(分享java快乐) */ package javamxj.ejb.client; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; import javamxj.ejb.cmp.UserManagement; import javax.ejb.CreateException; import javax.ejb.FinderException; import javax.naming.InitialContext; import javax.naming.NamingException; /** * @author pc * * TODO 要更改此生成的类型注释的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */ public class CMPClient4 { private javamxj.ejb.cmp.UserManagementHome getHome() throws NamingException { return (javamxj.ejb.cmp.UserManagementHome) getContext().lookup( javamxj.ejb.cmp.UserManagementHome.JNDI_NAME); } private InitialContext getContext() throws NamingException { Hashtable props = new Hashtable(); props.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099"); // This establishes the security for authorization/authentication // props.put(InitialContext.SECURITY_PRINCIPAL,"username"); // props.put(InitialContext.SECURITY_CREDENTIALS,"password"); InitialContext initialContext = new InitialContext(props); return initialContext; } public void testBean() { try { javamxj.ejb.cmp.UserManagement userMgmt = getHome().create(); createUsers(userMgmt); System.out.println("输出group表中的组名:"); printList(userMgmt.getGroups()); System.out.println("改动前,通过 ejb.finder 输出组别为IT的用户"); printList(userMgmt.getUserIDsInGroup1("IT")); System.out.println("改动:将lisi@bbb.ccc添加到IT组"); userMgmt.moveUserToGroup("lisi@bbb.ccc", "IT"); System.out.println("改动后,通过 ejb.select 输出组别为IT的用户"); printList(userMgmt.getUserIDsInGroup2("IT")); System.out.println("通过用户组给用户增加权限"); userMgmt.addRoleToUsers("IT", "manager"); System.out.println("验证用户所在组"); if (userMgmt.inGroup("javamxj@yahoo.com.cn", "IT")) { System.out.println("javamxj is in the IT group"); } else if (userMgmt.inGroup("javamxj@yahoo.com.cn", "marketing")) { System.out.println("javamxj is now in the marketing group"); } } catch (RemoteException e) { e.printStackTrace(); } catch (CreateException e) { e.printStackTrace(); } catch (NamingException e) { e.printStackTrace(); } catch (FinderException e) { e.printStackTrace(); } } // 创建用户 public static void createUsers(UserManagement userMgmt) throws RemoteException, CreateException { System.out.println("向Group表中添加数据"); userMgmt.createGroup("Marketing", "市场组"); userMgmt.createGroup("Engineering", "工程组"); userMgmt.createGroup("Sales", "销售组"); userMgmt.createGroup("IT", "信息科技组"); } // 输出ArrayList private static void printList(ArrayList list) { Iterator i = list.iterator(); while (i.hasNext()) { Object details = (Object) i.next(); System.out.println(details.toString()); } System.out.println(""); } public static void main(String[] args) { CMPClient4 test = new CMPClient4(); test.testBean(); } }