Eclipse quickly picks EJB - 3. One-to-one two-way relationship CMR (3)

xiaoxiao2021-03-06  25

Third, create a client

Eclipse quickly picks EJB - 3. Design a one-to-one two-way relationship CMR (1)

Eclipse quickly EJB - 3. One-to-one two-way relationship CMR (2)

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

2. Modify the generated cmpclient2.java, call the method in the userManagementBean.

2. Two static methods are established in vitro in TestBean () methods, simplifying code calls.

// create a user public static void createUsers (UserManagement userMgmt) throws RemoteException, CreateException {System.out.println ( "UserInfo added to the data in the table"); userMgmt.addUser ( "wangyi@aaa.com", "password 1", "Wang Yi", "Engineering", "111-1212", 22, 2000); usermgmt.adduser ("wanger@bbb.net", "password 2", "Wang 2", "marketing", "222-1213 ", 40, 4000); usermgmt.adduser (" zhangsan@bbb.ccc "," password 3 "," Zhang San "," IT "," 1688888 ", 32, 2800); usermgmt.adduser (" Lisi @ BBB .ccc "," password 4 "," Li Si "," Sales "," 1288888 ", 28, 2700); usermgmt.adduser (" zhu@bbb.eee "," password 5 "," Zhu Wang 5 ", "Sales", "1588888", 38, 4500); usermgmt.adduser ("javamxj@yahoo.com.cn", "Sharing Java Happy", "MXJ", "IT", "1788888", 26, 2900); } // Output ArrayList Private Static Void PrintList (ArrayList List) {ipiTerrator I = List.ITerator (); while (I.hasNext ()) {Object Details = (Object) i.next (); System.out.Println Details.toString ());} system.out.println ("");

3. Then, change the MyBean in the TestBean () method to the usermgmt, add data and display data to the table. As described below, the slope is the change portion.

public

Void testbean () {

Try {

Javamxj.ejb.cmp.userManagement

Usermgmt = gethome (). CREATE ();

CreateUsers (usermgmt); system.out.println ("Show All User"); PrintList (usermgmt.getusers ()); Run the program, the console output is as follows:

4. Comment "CREATEUSERS (usermgmt);" to invoke password verification and change the user's name.

// CREATEUSERS (usermgmt);

System.out.println (

"Show all users");

PrintList (usermgmt.getusers ());

Boolean login = false; login = usermgmt.verifypassword ("javamxj@yahoo.com.cn", "Share Java Happy"); System.Out.println ("Password Verification:" login;

System.out.println ("Changing the name of the user javamxj@yahoo.com.cn"); usermgmt.changeename ("javamxj@yahoo.com.cn", "hello");

If you run the program, you will find that JavaMxj's name has been changed to hello:

5. Continue to call the method of using the user:

System.out.println ("User using EJB.Finder"); PrintList (usermgmt.getuserbyage1 (30)); system.out.println ("User uses EJB.SELECT output age greater than 30 users"); PrintList (Usermgmt.getuserbyage2); Output:

6. Continue to call the method of outputting the user via the valueObject:

System.out.println ("Output User Information via ValueObject:"); userinfovalue [] users = usermgmt.getusersInfo (); for (int index = 0; index

Output:

7. Continue to call the user's way to query the user:

SYSTEM

.out.println ("Output of the user"); PrintList (usermgmt.getusersbyname); system.out.println ("Output Name User User"); PrintList (usermgmt.getusersbyname ("% Wang%")); Output:

8. Delete a user, since the tagged tag has been set, although the method of call is the method in the userbean, but still deletes the data related to the userInfobean.

/ / Delete a user usermgmt.removeuser (zhu@bbb.eee);

Viewing the data sheet will find "zhu@bbb.eee" data from the use of "zhu@bb.eee" in use at the same, and userinfotable.

Ok, this is over. I want to know how the matter is, and I listen to the decomposition.

Complete source code

Userbean.java

/ * * Create date 2005-1-14 * * Author:; javamxj (share the joy of java) * / package javamxj.ejb.cmp / ** * * You can insert your Documentation for ' Userbean '. * * @generated ** / public abstract class UserBean implements javax.ejb.EntityBean {/ ** * @ ejb.create-method * / public java.lang.String ejbCreate (String email, String password) throws javax.ejb.CreateException {// EJB 2.0 spec says return null For cmp ejbcreate methods. setemword (Password); Return NULL;

} / ** * The container invokes this method immediately after it calls ejbCreate. * / Public void ejbPostCreate (String email, String password) throws javax.ejb.CreateException {} / ** * CMP Field email * @return the email * @ Ejb.Persistent-Field * @ Ejb.Persistence * Column-name = "Email" * JDBC-TYPE = "VARCHAR" * SQL-TYPE = "VARCHAR (64)" * read-only = "false" * @ejb. PK-Field * * @ ejb.interface-method * / public abstract java.lang.string getemail (); / ** * @Param java.lang.string the new email value * @ Ejb.interface-method * / public abstract Void setemail (java.lang.string email); / ** * CMP Field Password * @return the password * @ Ejb.Persistent-Field * @ Ejb.Persistence * Column-name = "* JDBC-TYPE =" VARCHAR "* SQL-TYPE =" VARCHAR (64) "* read-only =" false "* * @ ejb.interface-method * / public abstract java.lang.string getpassword (); / ** * @Param java.lang .String the new password value * @ Ejb.interface-method D * / public abstract void setPassword (java.lang.string password); / ** * getter for cmr reason * * @ Ejb.interface-method view-type = "local" * @ Ejb.relation name = "userhasuserinfo" * Role-name = "userhasause" * target-multiple = "no" * cascade-delete = "yes" * * @ jboss.relation fk-color = "email" * related-pk-field = "email"

* / Public abstract javamxj.ejb.cmp.UserInfoLocal getUserInfo (); / ** * Setter for CMR Relationship * * @ ejb.interface-method view-type = "local" * / public abstract void setUserInfo (javamxj.ejb.cmp .Urserinfolocal value); UserInfobean.java

/ * * Create date 2005-1-24 * * Author: javamxj (share the joy of java) * / package javamxj.ejb.cmp; import javax.ejb.FinderException; / ** * * <- begin-user-doc! -> You can insert your documentation for ' Userinfobean '. *

* Remove-table = "false" * * * @ Ejb.pk class = "java.lang.string" * * @generated ** / public abstract class userinfobe information javax. ejb.EntityBean {/ ** * @ ejb.create-method * / public java.lang.String ejbCreate (String email, String name, String dept, String workPhone, Integer age, Integer salary) throws javax.ejb.CreateException {/ / EJB 2.0 Spec Says Return Null for Cmp Ejbcreate Methods. SetEmail (email); setName (Name); setDept (dept); setage (age); setage; setsalary; return null;} / ** * The container invokes this method immediately after it calls ejbCreate. * / public void ejbPostCreate (String name, String email, String dept, String workPhone, Integer age, Integer salary) throws javax.ejb.CreateException {} / ** * CMP Field email * @Return the email * @ ejb.persistent-field * @ ejb.persistence * column-name = "email" * jdbc-type = "varchar" * sql-type = "varchar (32) "* read-only =" false "* @ ejb.pk-Field * * @ Ejb.interface-method * / public abstract java.lang.string getemail (); / ** * @Param java.lang. String the new email value * @ ejb.interface-method * / public abstract void setemail (java.lang.string email); / ** * cmp field name * @Return the name * @ ejb.persistent-field * @EJB. Persistence * column-name = "Name * JDBC-TYPE =" varcha "* SQL-TYPE =" VARCHAR (16) "* read-only =" false "* * @ Ejb.interface-method * / public Abstract Java .lang.string getname ();

/ ** * @Param java.lang.string the new name value * @ ejb.interface-method * / public abstract void setName (java.lang.string name); / ** * CMP Field Dept * @return the dept * @ Ejb.Persistent-Field * @ Ejb.Persistence * Column-name = "Group" * JDBC-TYPE = "VARCHAR" * SQL-TYPE = "VARCHAR (24)" * Read-only = "false" * * * @ ejb.interface-method * / public abstract java.lang.string getDept (); / ** * @Param java.lang.string the new dept value * @ ejb.interface-method * / public abstract void setDept (Java. Lang.String Dept); / ** * CMP Field Workphone * @Return the Workphone * @ Ejb.Persistent-Field * @ Ejb.Persistence * Column-name = "Work Phone" * JDBC-TYPE = "VARCHAR" * SQL- TYPE = "VARCHAR (12)" * read-only = "false" * * * @ Ejb.interface-method * / public abstract java.lang.string getWorkphone (); / ** * @Param java.lang.string the New workphone value * @ ejb.interface-method * / public abstract void setworkphone (java.lang.strin g workphone); / ** * CMP Field Age * @Return the Age * @ Ejb.Persistent-Field * @ Ejb.Persistence * Column-name = "Age" * JDBC-TYPE = "VARCHAR" * SQL-TYPE = " Integer "* read-only =" false "* * * @ ejb.interface-method * / public abstract java.lang.integer getage (); / ** * @Param java.lang.integer the new age value * @ejb .interface-method * / public abstract void setage (java.lang.integer agent; / ** * cmp field sales * @return the salary * @ ejb.persistent-inf * @

Ejb.persistence * column-name = "Salable" * JDBC-TYPE = "varcha" * SQL-TYPE = "Integer" * read-only = "false" * * @ Ejb.interface-method * / public Abstract Java .. Lang.integer getsalary (); / ** * @Param java.lang.integer the new sales value * @ Ejb.interface-method * / public abstract void setsalary; / ** * @ejb .interface-method * / public abstract userinfovalue getUserinfovalue (); // ########### SELECT METHOD ################################################################################################################################################################################################################### ##### / ** * select method * @ ejb.select query = "SELECT G.EMAIL from UserinfoSchema As g Where g.age>? 1" * / public abstract java.util.collection EJBSELECTUSERBYAGE (INT i) throws javax.ejb.FinderException; / ** * Home method * @throws FinderException * @ ejb.home-method view-type = "local" * / public java.util.Collection ejbHomeQueryUserByAge (int age) throws FinderException {return ejbSelectUserByAge (age );} // ############ CMR Relationship ########################### Getter for CMR Relationship * * @ ejb.interface-method view-type = "local" * @ ejb.relation * name = "UserHasUserInfo" * role-name = "UserInfoPartOfUser" * target-multiple = "no" * cascade-delete = "YES" * / public abstract javamxj.ejb.cmp.userlocal getuser (); / ** * setter for cmr reason * * @ Ejb.interface-method view-type = "local" * / public abstract void setuser (javamxj . Ejb.cmp.userlocal value); UserManagementBean.java

/ * * Create date 2005-1-14 * * Author: javamxj (share the joy of java) * / package javamxj.ejb.cmp; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; Import javax.ejb.createException; import javax.ejb.findERException; / ** * * a generated session bean * UserManagement UserManagement javamxj.ejb.cmp.useragementBean stateless Container * * * @ ejb.bean name = "usermanagement" * jndi- Name = "UserManagement" * type = "stateless" * Transaction-type = "container" * View-type = "remote" * * @ ejb.ejb-ref * ejb-name = "user" * view-type = "local "* ref-name =" ejb / userlocal "* * @ jboss.ejb-local-ref * ref-name =" userlocal "* jndi-name =" userlocal "* * @

generated * / public abstract class UserManagementBean implements javax.ejb.SessionBean {private UserLocalHome userHome = null; private UserInfoLocalHome infoHome = null; / ** * Create method * @ ejb.create-method view-type = "remote" * / public void ejbCreate () throws javax.ejb.CreateException, javax.naming.NamingException {userHome = UserUtil.getLocalHome (); infoHome = UserInfoUtil.getLocalHome ();} public void ejbPassivate () {userHome = null; infoHome = null;} / * * * Business method * @ ejb.interface-method view-type = "remote" * / public void addUser (String email, String password) throws javax.ejb.CreateException {UserLocal user = userHome.create (email, password);} / ** * Business method * @ ejb.interface-method view-type = "remote" * / public void removeUser (String email) throws javax.ejb.RemoveException {userHome.remove (email);} / ** * Business method * @ ejb.interface-method view-type = "remote" * / public boilean verifypassword (String email, s THROWS JAVAX.EJB.FINDEREXCEPTION {UserLocal User = UserHome.FindByPrimaryKey (email); return user.getpassword (). Equals (password);} // ************** *********************************************************** ***** // One-to-one two-way relationship // ******************************************* *************************** * Create users, create user and userinfo * this time @throws CreateException * @ ejb.interface-method view-type = "remote" * / public void addUser (String email, String password, String name, String dept, String workPhone, int age, int salary) throws CreateException {UserLocal user =

UserHome.create (Email, Password); UserInfolocal Info = InfoHome.Create (Email, Name, DEPT, WORKPHONE, NEW INTEGER (AGE), New Integer (Salary); User.SetUserInfo (INFO);} / ** * Display All users * @throws FinderException * @ ejb.interface-method view-type = "remote" * / public ArrayList getUsers () throws FinderException {ArrayList userList = new ArrayList (50); Collection collection = userHome.findAll (); Iterator iterator = Collection.iterator (); while (item (item.hasnext ()) {UserLocal User = (UserLocal) Iterator.Next (); userlist.add ());} Return UserList;} / ** * Change the user name * Business method * @ ejb.interface-method view-type = "remote" * / public void changeName (String email, String name) throws javax.ejb.FinderException {UserLocal user = userHome.findByPrimaryKey (email); UserInfoLocal info = User.getuserinfo (); info.setname (name);} / ** * Using ejb.finder through age to query users * Business method * @ Ejb.interface-method view-type = "remote" * / public arra yList getUserByAge1 (int age) throws FinderException {ArrayList age1 = new ArrayList (30); Iterator iter = infoHome.findUserByAge (age) .iterator (); while (iter.hasNext ()) {UserInfoLocal element = (UserInfoLocal) iter.next (); Age1.Add (Element.Getemail ());} Return Age1;} / ** * Using ejb.select through age to query users * Business method * @ ejb.interface-method view-type = "remote" * / public ArrayList getUserByAge2 (int age) throws FinderException {ArrayList age2 = new ArrayList (30); Iterator iter = infoHome.queryUserByAge (age) .iterator (); while (iter.hasNext ()) {String element = (String) iter .next ();

Age2.add (Element);} Return Age2;} / ** * Using Ejb.Value-Object to get user * business method * @throws FinderException * @ Ejb.interface-method view-type = "remote" * / public javamxj. ejb.cmp.UserInfoValue [] getUsersInfo () throws FinderException {ArrayList userList = new ArrayList (50); Iterator iter = userHome.findAll () iterator ();. while (iter.hasNext ()) {UserLocal user = (UserLocal) iter.next (); UserInfoLocal info = user.getUserInfo (); UserInfoValue userInfoValue = info.getUserInfoValue (); userList.add (userInfoValue);} return (UserInfoValue []) userList.toArray (new UserInfoValue [userList .size () ]);} / * ** ejb.finder by using the user name to query * Business method * @throws FinderException * @ ejb.interface-method view-type = "remote" * / public ArrayList getUsersByName (String name) throws FinderException { ArrayList Namelist = New ArrayList (30); item neyname (name) .ITerator (); while (ore) {userinfolocal element = (userInfolocal) Iter.next (); namelist.add (element.getemail ());}}} cmpclient2.java

/ * * Create date 2005-1-24 * * 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.UserInfoValue; import javamxj.ejb.cmp.UserManagement; import javax.ejb.CreateException; import javax.ejb.FinderException; import javax.ejb.RemoveException; import javax.naming .Initialcontext; import javax.naming.namingexception; / ** * @Author pc * * Todo To change this generated type annotation template, go to * Window - Preference - Java - Code Template * / Public Class CMPClient2 {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 ( "Show all users"); PrintList (usermgmt.getusers ()); boolean login = false; login = usermgmt.verifypassword ("javamxj@yahoo.com.cn", "Share Java Happy");

System.out.println ("Password Verification:" login; system.out.println ("Change User JavaMxj@yahoo.com.cn"); usermgmt.changename ("javamxj@yahoo.com.cn", "Hello"); system.out.println ("User using EJB.Finder"); PrintList (usermgmt.getuserbyage1 (30)); system.out.println ("using ejb.select output age greater than 30 User "); PrintList (usermgmt.getuserbyage2); system.out.println (" Output User Information via ValueObject: "); userinfovalue [] users = usermgmt.getusersInfo (); for (int index = 0; index < Users.length; index ) {userinfovalue user = users [index]; system.out.println ("User" User.Getemail () "age:" User.getage ());} system.out.println ("Output User"); PrintList (Usermgmt.getuserSbyName); System.out.Println ("Output User User"); PrintList (Usermgmt.getusersbyName) % ")); // Delete a user usermgmt.removeuser (" zhu@bbb.eee ");} catCh (RemoteException E) {E.PrintStackTrace ();} catch (createException E) {E.PrintStackTrace ();} Catch (namingexception e) {E.PrintStackTrace ();} catch (FinderExce) ption e) {e.printStackTrace ();} catch (RemoveException e) {e.printStackTrace ();}} // create a user public static void createUsers (UserManagement userMgmt) throws RemoteException, CreateException {System.out.println ( "the Add data in the Userinfo table "); usermgmt.adduser (" wangyi@aaa.com "," password 1 "," Wang Yi "," ENGINEERING "," 111-1212 ", 22, 2000); usermgmt.adduser Wanger@bbb.net "," Password 2 "," Wang 2 "," Marketing "," 222-1213 ", 40, 4000); Usermgmt.adduser (" zhangsan @

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

New Post(0)