Eclipse quickly EJB - 4. Multi-to-many one-way relationship CMR (2)

xiaoxiao2021-03-06  40

Take it up:

Eclipse quickly EJB - 4. Multi-to-many one-way relationship CMR (1)

Second, add business methods in UserManagementBean

1. Add a USERINFOLOCALHOME variable UserHome and put it in the EJBCREATE and EJBPASSIVATE methods.

2. Add the following business methods in turn.

Part of Rolebean in UserManagementBean (very simple) // ****************************************************************************************************************************************************************************************************************************************** ******************************** Multi-to-many one-way relationships // ******* *********************************************************** ************ / ** * Create permissions * Business method * @throws createException * @ Ejb.interface-method view-type = "remote" * / public void Createrole (String Rolename, String description) throws CreateException {roleHome.create (roleName, description);} / ** * display all users * @throws FinderException * @ ejb.interface-method view-type = "remote" * / public ArrayList getRoles () throws FinderException { ArrayList RoleList = New ArrayList (30); Iterator iter = rolehome.findall (). Iterator (); while (ore.hasnext ()) {ROLELOCAL ROLE = (ROLELOCAL) iter.next (); rolelist.add (role.getname ());} return roleList;} / ** * delete permissions * @throws RemoveException * @throws EJBException * @ ejb.interface-method view-type = "remote" * / public void removeRole (String name) throws EJBExcepti on, RemoveException {roleHome.remove (name);} / ** * to increase user permissions * @throws FinderException * @ ejb.interface-method view-type = "remote" * / public void addRole (String email, String roleName) throws FinderException {UserLocal user = userHome.findByPrimaryKey (email); Collection roles = user.getRoles (); RoleLocal role = roleHome.findByPrimaryKey (roleName); roles.add (role);} / ** * owned by a user to give Permissions * @Throws FinderException * @ ejb.interface-method view-type = "remote" * / public arraylist getUserrole (String email) throws FingeRexception {ArrayList RoleList = New ArrayList (30);

UserLocal User = UserHome.FindByPrimaryKey (Email); item ore = user.getroles (). Iterator (); while (ore.hasnext ()) {ROLELOCAL ROLE = (ROLELOCAL) ney.next (); rolelist.add (role. GetName ());} return rolelist;} 3. Ok, save,

Generate EJB CLASSES, start MySQL,

Start JBoss server,

Deploy Module.

If everything is normal, two tables are automatically created in the CMP_SAMPLE library: Roletable and Userrole.

Role_fk and email_fk are defined in the getRoles in the UserBean.

Third, create a client

1. Right-click the SRC folder -> New-> Lomboz EJB Test Client Wizard:

Create a cmpclient3.java file (the steps are the same as the first two articles).

2. Modify the generated CMPCLIENT3.JAVA to call the method in the userManagementBean.

Here is a complete source code:

CmpClient3.java

/ * * Create date 2005-1-25 * * Author: javamxj (share the joy of 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 To change this generated type annotation template, go to * window - Preference - Java - code style - Code Template * / public class cmpclient3 {private javamxj.ejb.cmp.usement javamxj.ejb.cmp.usetionementHome 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 / authe ntication // 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 usermgmgmt = gethome (). Create (); createUsers (usermgmt); system.out.println ("Show All Permissions"); PrintList (usermgmt.getroles ()); system.out. Println ("Give User WANGYI@aaa.com Add Permissions"); Usermgmt.addrole ("wangyi@aaa.com", "manager"); usermgmt.addrole ("wangyi@aaa.com", "user");

System.out.println ("Give User JavaMxJ Add Permissions"); Usermgmt.Addrole ("JavaMxj@yahoo.com.cn", "admin"); Usermgmt.addrole ("javamxj@yahoo.com.cn", "User "); System.out.Println (" Show JavaMxj's permissions "); PrintList (Usermgmt.GetUserrole (" javamxj@yahoo.com.cn)));} catch (remoteExcection E) {E.PrintStackTrace ();} catch (CreateException e) {e.printStackTrace ();} catch (NamingException e) {e.printStackTrace ();} catch (FinderException e) {e.printStackTrace ();}} // create a user public static void createUsers (UserManagement Usermgmt) throws remoteException, createException {system.out.println (Add data to the role table "); usermgmt.createrole (" admin "," system administrator "); usermgmt.createrole (" manager "," content administrator "); usermgmt.createrole (" User "," ordinary users "); usermgmt.createrole (" guest "," guest user ");} // Output 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) {cmpclient3 test = new cmpclient3 (); test.testbean ();}} 3. Run client

· Console output:

· Changes in the database in the database:

转载请注明原文地址:https://www.9cbs.com/read-64216.html

New Post(0)