Eclipse quickly picks EJB - 3. Design a one-to-one two-way relationship CMR (1) This series of articles I am published in the IBM website based on rick hightower
"
EJB - CMP / CMR Introduction "Adapted, this is a series, with 4 articles. This article integrates the four articles, see this article, you must refer to it, so it is very convenient, but I have made some modifications, the principle is basically still constant. I don't say knowledge about CMP, I just introduce how to develop CMPs in Eclipse using Lomboz and JBoss-IDE.
This article mainly tells how to develop a one-to-one two-way relationship, which is designed with a UserInfobean, and the article.
Eclipse Quick Purchase EJB - 2. Designing the UserBean in an entity bean establishes relationship, which is also from which it will continue to extend. Therefore, there must be the foundation of the previous article.
Regarding the environmental configuration, use skills to refer to my previous article, here as much as possible.
The complete source code has been attached.
I. Design entity bean: Userinfobean
1. Establish a main part:
●
In the CMP_SAMPLE project, right click "SRC" -> New -> Lomboz EJB CREATION WIZARD:
· Package (K): javamxj.ejb.cmp
· Name (M): UserInfo
· EJB TYPE: Select Container Managed Entity EJB
Click Next.
● Schema name: userinfoschema
Table Name: Userinfotable
● Add an email column:
· Field: email
· Field Type: java.lang.string
· Database column: Email
· SQL TYPE: VARCHAR
And make it a primary key.
● Similarly, add another Name column:
· Field: Name
· Field Type: java.lang.string
· Database colorn: Name
· SQL TYPE: VARCHAR
● Similarly, add a DEPT column:
· Field: DEPT
· Field Type: java.lang.string
· Database column: Group
· SQL TYPE: VARCHAR
● Similarly, add a Workphone column:
Field: Workphone
· Field Type: java.lang.string
· Database column: Workload
· SQL TYPE: VARCHAR
● Similarly, add an AGE column:
· Field: AGE
· Field Type: java.lang.integer
· Database colorn: age
· SQL TYPE: INTEGER
● Similarly, add a SALARY bar:
· Field: Salary
· Field Type: java.lang.integer
· Database column: salary
· SQL TYPE: INTEGER
● Similarly, add an AGE column:
· Field: AGE
· Field Type: java.lang.integer
· Database colorn: age
· SQL TYPE: INTEGER ● Similarly, add a SALARY bar:
· Field: Salary
· Field Type: java.lang.integer
· Database column: salary
· SQL TYPE: INTEGER
The effect is as follows, and finally click to complete.
Note: As the above article, the SQL-Type = "varchar" in the generated code is changed to SQL-TYPE = "VARCHAR (XY)", XY is a suitable varchar bit number.
2. Complete EJBCREATE and EJBPOSTCREATE methods:
3. Add the following tags to the class tag.
4. EJB.SELECT method
· Right-click UserInfobean -> J2EE -> Add SELECT METHOD (JBoss-Ide: If you don't know how to call out, look at the article)
· Because the EJB.SELECT method needs to pass
The HOME interface can be used indirectly, so:
Right click UserInfobean -> J2EE -> Add
Home Method
· Because the EJB.SELECT method needs to pass
The HOME interface can be used indirectly, so:
Right click UserInfobean -> J2EE -> Add
Home Method
· As shown in the figure, click "Finish" to generate the code, you need to manually add a return statement.
5. Add UserInfobean to the CMPEJB module, then lomboz -> Generate EJB CLASSES
As shown below, 6 files are generated, where userinfovalue.java is the "@ Ejb.Value-Object" tag generated value object.
6. Add an abstract method to call the value object.
7. Key: Establish a one-to-one two-way relationship with UserInfobean and Userbean:
· Right click UserInfobean -> J2EE -> Add CMR Relationship:
· After clicking "Finish", generate the following code (the statement in the red box needs to be manually added):
· Switch to
Userbean.java:
Right-click Userbean -> J2EE -> Add CMR Relationship:
· After clicking "Finish", generate the following code (the statement in the red box needs to be manually added):
OK, again
Generate EJB Classes, if everything is normal, proceed to the next step.
(You can look at the code generated in ejb-jar.xml and jbosscmp-jdbc.xml files).
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.
About how to add, if you want yourself, you can take advantage of JBoss-Ide
Add Business Method (Look at the article);
Or Simple copying I have done good code.
These methods are very simple, and it is not easy to say, it is easy to read.
Ok, save,
Generate EJB CLASSES, start MySQL (after startup, first build the last article in the UserTable table in the CMP_SAMPLE library).),
Start JBoss server,
Deploy Module.
If everything is normal, two tables are automatically created in the CMP_SAMPLE library: useteable and userinfotable
Third, create a client
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); view the data sheet, it will find that "" "" Zhu@bbb.eee "data. Ok, this is over. I want to know how the matter is, and I listen to the decomposition. Complete source code Userbean.java / * * Created date 2005-1-14 * * Author: javamxj (share happiness java) * / Package javamxj.ejb.cmp; / ** * * you can insert your documentation for ' Userbean b> em>'. * XML Version = "1.0" encoding = "UTF-8"?> lomboz: Entityejb> lomboz: Fieldmappings> lomboz: FIELDMAPPINGS> lomboz: Entity> lomboz: EJB> * * * @ ejb.bean name = "user" * JNDI-Name = "user" * Type = "cmp" * Primkey-Field = "email" * Schema = "Userschema" * Cmp-Version = "2.x" * View-type = "local" * Data-Source = "" * * @ Ejb.Persistence * Table-name = "useertable" * * @ ejb.finder * query = "SELECT OBJECT (a) from userSchema as a" * Signature = "java.util.collection Findall ()" * * @ jboss.Persistence Create-Table = "True" * Remove-Table = "false" * * @ ejb.pk class = "java.lang.string" * * @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. STEMAIL (Email); SetPassword (Password); Return NULL; } / ** * The Container Invokes this Method Immedierately 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 = "password" * 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 * / Public Abstract Void SetPassword (Java.lang.String Password); / ** * Getter for CMR RelationShip * * @ ejb.interface-method view-type = "local" * @ ejb.relation name = "userhasuserinfo" * role-name = "UserhasAuserinfo" * 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.userInfolocal value); } Userinfobean.java / * * Create Date 2005-1-24 * * Author: javamxj (share happiness java) * / Package javamxj.ejb.cmp; Import javax.ejb.finderexception; / ** * * you can insert your documentation for ' userinfobean b> em>'. " * XML Version = "1.0" encoding = "UTF-8"?> lomboz: Entityejb> lomboz: FIELDMAPPINGS> lomboz: FIELDMAPPINGS> lomboz: FIELDMAPPINGS> lomboz: FIELDMAPPINGS> lomboz: FIELDMAPPINGS> lomboz: FIELDMAPPINGS> lomboz: Entity> lomboz: EJB> * * * @ ejb.bean name = "userinfo" * JNDI-Name = "Userinfo" * Type = "cmp" * Primkey-Field = "email" * Schema = "UserinfoSchema" * Cmp-Version = "2.x" * View-type = "local" * Data-Source = "" * * @ Ejb.Persistence * Table-name = "userinfotable" * * @ ejb.value-object * * @ ejb.finder * query = "SELECT Object (a) from userinfoschema as a" * Signature = "java.util.collection Findall ()" * * @ ejb.finder * query = "SELECT OBJECT (a) from userinfoschema as a where a.age>? 1" * Signature = "java.util.collection Finduserbyage (int Age)" * * @ ejb.finder * query = "SELECT OBJECT (G) from userinfoschema as g Where g.name like? 1" * signature = "java.util.collection FinduserbyName (java.lang.string name) * * @ jboss.Persistence * Create-Table = "True" * Remove-Table = "false" * * * @ ejb.pk class = "java.lang.string" * * @generated ** / Public Abstract Class UserInfobean Implements Javax.ejb.entityBean { / ** * @ ejb.create-method * / Public Java.lang.String Ejbcreate String email, String Name, String dept, String Workphone, Integer Age, Integer Salry) throws javax.ejb.createException { // EJB 2.0 Spec Says Return Null for CMP EJBCREATE METHODS. STEMAIL (email); SetName (Name); SetDept (dept); Setworkphone (Workphone); Setage (agn); Setsalary (SALARY); Return NULL; } / ** * The Container Invokes this Method Immedierately After It Calls Ejbcreate. * / Public void ejbpostcreate String Name, String email, String dept, String Workphone, Integer Age, Integer Salry) 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 = "varchar" * 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 de PEPT * @ 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.string workphone); / ** * CMP Field Age * @Return the agn * @ 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 age); / ** * CMP Field Salry * @return the salary * @ Ejb.Persistent-Field * @ Ejb.Persistence * Column-name = "Salary" * JDBC-TYPE = "varchar" * SQL-TYPE = "Integer" * read-only = "false" * * * @ ejb.interface-method * / Public Abstract Java.lang.integer getsalary (); / ** * @Param java.lang.integer the New Salry Value * @ ejb.interface-method * / Public Abstract Void Setsalary; Java.lang.integer Salary; / ** * @ 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 FingerException { 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 Relationship * * @ ejb.interface-method view-type = "local" * / Public Abstract Void Setuser (JavaMxj.ejb.cmp.userLocal Value); } UserManagementBean.java /* * Created date 2005-1-14 * * Author: javamxj (share happiness 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 * XML Version = "1.0" encoding = "UTF-8"?> lomboz: sessionejb> lomboz: session> lomboz: EJB> * * * @ 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 Boolean VerifyPassword (String Email, String Password) Throws javax.ejb.finderexception { UserLocal User = UserHome.FindByPrimaryKey (email); Return User.getPassword (). Equals (Password); } // ******************************************************** ********************* // One-to-one two-way relationship // ******************************************************** ********************* / ** * Create a user, 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 SALY) THROWS CREATEXCEPTION { UserLocal User = UserHome.create (Email, Password); UserInfolocal Info = InfoHome.create (email, name, dept, Workphone, New Integer (AGE), New Integer (Salary); User.setUserInfo (INFO); } / ** * Show 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 itrator = collection.iterator (); While (item.hasnext ()) { UserLocal User = (UserLocal) Iterator.next (); UserList.Add (user.getemail ()); } Return UserList; } / ** * Change the user's 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); } / ** * Use ejb.finder through age to check users * Business Method * @ ejb.interface-method view-type = "remote" * / Public ArrayList GetUserbyage1 (int Age) throws finderException { Arraylist age = new arraylist (30); Iterator it = infoHome.finduserbyage (agn) .Itemrator (); While (ore.hasnext ()) { UserInfolocal element = (userInfolocal) .next (); Age1.Add (Element.Getemail ()); } Return Age1; } / ** * Use ejb.select through age to check users * Business Method * @ ejb.interface-method view-type = "remote" * / Public ArrayList GetUserbyage2 (int Age) throws finderException { ArrayList age2 = new arraylist (30); Iterator it = infoHome.Queryuserbyage (age) .Itemrator (); While (ore.hasnext ()) { String element = (string) iTer.next (); Age2.Add (Element); } Return Age2; / ** * Get users with ejb.value-object * Business Method * @Throws FinderException * @ ejb.interface-method view-type = "remote" * / Public javamxj.ejb.cmp.userinfovalue [] getUsersinfo () Throws FingerException { ArrayList UserList = New ArrayList (50); Iterator it = UserHome.findall (). Iterator (); While (ore.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 ()]); } / ** * Use the ejb.finder through the name to check the user * Business Method * @Throws FinderException * @ ejb.interface-method view-type = "remote" * / Public ArrayList GetuserSbyName (String Name) throws FinderException { ArrayList Namelist = New ArrayList (30); Iterator it = infoHome.finduserbyname (name) .Itemrator (); While (ore.hasnext ()) { UserInfolocal element = (userInfolocal) .next (); Namelist.add (element.getemail ()); } Return namelist; } } CmpClient2.java / * * Create Date 2005-1-24 * * Author: javamxj (share happiness 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.user manufacture; 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 the template for this generated type comment, please turn to * Window - Preferences - Java - Code Style - 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 INIALCONTEXT; } Public void testbean () { Try { Javamxj.ejb.cmp.userManagement usermgmgmt = 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 ("Changing the name of User JavaMxj@yahoo.com.cn"); Usermgmt.changeename ("javamxj@yahoo.com.cn", "hello"); System.out.Println ("User with EJB.Finder outputs more than 30"); PrintList (usermgmt.getuserbyage1); System.out.println ("Use EJB.SELECT Output Ages for users"); PrintList (usermgmt.getuserbyage2); System.out.println ("Output User Information via ValueObject:"); Userinfovalue [] users = usermgmt.getusersInfo (); For (int index = 0; index Userinfovalue user = users [index]; System.out.println ("User" User.Getemail () "age:" User.getage ());} System.out.Println ("Output of User of the Weed"); PrintList (usermgmt.getusersbyname); System.out.println ("Output User in Name"); 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 (FINDEREXCEPTION E) { E.PrintStackTrace (); } catch (removeexcection e) { E.PrintStackTrace (); } } // Create a user Public Static Void CreateUsers (UserManagement Usermgmt) THROWS RemoteException, CreateException { System.out.println (Add data to 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@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", "Share Java Happy", "MXJ", "IT", "1788888", 26, 2900); } // Output 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) { CmpClient2 Test = New Cmpclient2 (); Test.TestBean (); } } · After clicking "Finish", generate the following code (the statement in the red box needs to be manually added): · Switch to Userbean.java: Right-click Userbean -> J2EE -> Add CMR Relationship: · After clicking "Finish", generate the following code (the statement in the red box needs to be manually added): OK, again Generate EJB Classes, if everything is normal, proceed to the next step. (You can look at the code generated in ejb-jar.xml and jbosscmp-jdbc.xml files). 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. About how to add, if you want yourself, you can take advantage of JBoss-Ide Add Business Method (Look at the article); Or Simple copying I have done good code. These methods are very simple, and it is not easy to say, it is easy to read. Ok, save, Generate EJB CLASSES, start MySQL (after startup, first build the last article in the CMP_SAMPLE library) The useertable table is removed. ), Start JBoss server, Deploy Module. If everything is normal, two tables are automatically created in the CMP_SAMPLE library: useteable and userinfotable Third, create a client 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 TESTBEAN () method to UsermgmT, back to table Add data and display data. 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", "Sharing Java Happy"); System.out.Println ("Password Verification:" login); System.out.println (" Change the name of the user javamxj@yahoo.com.cn "); Usermgmt.changename (" 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); View the data sheet, it will find that "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" Zhu@bbb.eee "data. Ok, this is over. I want to know how the matter is, and I listen to the decomposition. Complete source code Userbean.java / * * Created date 2005-1-14 * * Author: javamxj (share happiness java) * / Package javamxj.ejb.cmp; / ** * * you can insert your documentation for ' Userbean b> em>'. * XML Version = "1.0" encoding = "UTF-8"?> lomboz: Entityejb> lomboz: FIELDMAPPINGS> lomboz: FIELDMAPPINGS> lomboz: Entity> lomboz: EJB> * * * @ ejb.bean name = "user" * JNDI-Name = "user" * Type = "cmp" * Primkey-Field = "email" * Schema = "Userschema" * Cmp-Version = "2.x" * View-type = "local" * Data-Source = "" * * @ Ejb.Persistence * Table-name = "useertable" * * @ ejb.finder * query = "SELECT OBJECT (a) from userSchema as a" * Signature = "java.util.collection Findall ()" * * @ jboss.Persistence Create-Table = "True" * Remove-Table = "false" * * @ ejb.pk class = "java.lang.string" * * @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. STEMAIL (email); SetPassword (Password); Return NULL; } / ** * The Container Invokes this Method Immedierately 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 = "password" * 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 * / Public Abstract Void SetPassword (Java.lang.String Password); / ** * Getter for CMR RelationShip * * @ ejb.interface-method view-type = "local" * @ ejb.relation name = "userhasuserinfo" * role-name = "UserhasAuserinfo" * 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.userInfolocal value); } Userinfobean.java / * * Create Date 2005-1-24 * * Author: javamxj (share the joy of java) * / Package javamxj.ejb.cmp; Import javax.ejb.finderexception; / ** * * you can insert your documentation for ' userinfobean b> em>'. " * XML Version = "1.0" encoding = "UTF-8"?> lomboz: Entityejb> lomboz: FIELDMAPPINGS> lomboz: FIELDMAPPINGS> lomboz: FIELDMAPPINGS> lomboz: FIELDMAPPINGS> lomboz: Fieldmappings> lomboz: FIELDMAPPINGS> lomboz: Entity> lomboz: EJB> * * * @ ejb.bean name = "userinfo" * JNDI-Name = "Userinfo" * Type = "cmp" * Primkey-Field = "email" * Schema = "UserinfoSchema" * Cmp-Version = "2.x" * View-type = "local" * Data-Source = "" * * @ Ejb.Persistence * Table-name = "userinfotable" * * @ ejb.value-object * * @ ejb.finder * query = "SELECT Object (a) from userinfoschema as a" * Signature = "java.util.collection Findall ()" * * @ ejb.finder * query = "SELECT OBJECT (a) from userinfoschema as a where a.age>? 1" * Signature = "java.util.collection Finduserbyage (int Age)" * * @ ejb.finder * query = "SELECT OBJECT (G) from userinfoschema as g where g.name like? 1" * Signature = "java.util.collection finduserbyname (java.lang.string name)" * * @ jboss.Persistence * Create-Table = "True" * Remove-Table = "false" * * * @ ejb.pk class = "java.lang.string" * * @generated ** / Public Abstract Class UserInfobean Implements Javax.ejb.entityBean { / ** * @ ejb.create-method * / Public Java.lang.String Ejbcreate String email, String Name, String dept, String Workphone, Integer Age, Integer Salry) throws javax.ejb.createException { // EJB 2.0 Spec Says Return Null for CMP EJBCREATE METHODS. STEMAIL (email); SetName (Name); SetDept (dept); Setworkphone (Workphone); Setage (agn); Setsalary (SALARY); Return NULL; } / ** * The Container Invokes this Method Immedierately After It Calls Ejbcreate. * / Public void ejbpostcreate String Name, String email, String dept, String Workphone, Integer Age, Integer Salry) 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 = "varchar" * 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 de PEPT * @ 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.string workphone); / ** * CMP Field Age * @Return the agn * @ 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 agn); / ** * CMP Field Salry * @return the salary * @ Ejb.Persistent-Field * @ Ejb.Persistence * Column-name = "Salary" * JDBC-TYPE = "varchar" * SQL-TYPE = "Integer" * read-only = "false" * * * @ ejb.interface-method * / Public Abstract Java.lang.integer getsalary (); / ** * @Param java.lang.integer the New Salry Value * @ ejb.interface-method * / Public Abstract Void Setsalary; Java.lang.integer Salary; / ** * @ 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 FingerException { 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 Relationship * * @ ejb.interface-method view-type = "local" * / Public Abstract Void Setuser (JavaMxj.ejb.cmp.userLocal Value); } UserManagementBean.java / * * Created date 2005-1-14 * * Author: javamxj (share happiness 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 * XML Version = "1.0" encoding = "UTF-8"?> lomboz: sessionejb> lomboz: session> lomboz: EJB> * * * @ 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 Boolean VerifyPassword (String Email, String Password) Throws javax.ejb.finderexception { UserLocal User = UserHome.FindByPrimaryKey (email); Return User.getPassword (). Equals (Password); } // ******************************************************** ********************* // One-to-one two-way relationship // ******************************************************** ********************* / ** * Create a user, 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 SALY) THROWS CREATEXCEPTION { UserLocal User = UserHome.create (Email, Password); UserInfolocal Info = InfoHome.create (Email, Name, Dept, Workphone, New Integer (AGE), New Integer (Salary); User.setUserInfo (INFO); } / ** * Show 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 itrator = collection.iterator (); While (item.hasnext ()) { UserLocal User = (UserLocal) Iterator.next (); UserList.Add (user.getemail ()); } Return UserList; } / ** * Change the user's 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); } / ** * Use ejb.finder through age to check users * Business Method * @ ejb.interface-method view-type = "remote" * / Public ArrayList GetUserbyage1 (int Age) throws finderException { Arraylist age = new arraylist (30); Iterator it = infoHome.finduserbyage (agn) .Itemrator (); While (ore.hasnext ()) { UserInfolocal element = (userInfolocal) .next (); Age1.Add (Element.Getemail ()); } Return Age1; } / ** * Use ejb.select through age to check users * Business Method * @ ejb.interface-method view-type = "remote" * / Public ArrayList GetUserbyage2 (int Age) throws finderException { ArrayList age2 = new arraylist (30); Iterator it = infoHome.Queryuserbyage (age) .Itemrator (); While (ore.hasnext ()) { String element = (string) iTer.next (); Age2.Add (Element); } Return Age2; } / ** * Get users with ejb.value-object * Business Method * @Throws FinderException * @ ejb.interface-method view-type = "remote" * / Public JavaMxj.ejb.cmp.userinfovalue [] getusersinfo () throws finderException { ArrayList UserList = New ArrayList (50); Iterator it = UserHome.findall (). Iterator (); While (ore.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 ()]); } / ** * Use the ejb.finder through the name to check the user * Business Method * @Throws FinderException * @ ejb.interface-method view-type = "remote" * / Public ArrayList GetuserSbyName (String Name) throws FinderException { ArrayList Namelist = New ArrayList (30); Iterator it = infoHome.finduserbyname (name) .Itemrator (); While (ore.hasnext ()) { UserInfolocal element = (userInfolocal) .next (); Namelist.add (element.getemail ()); } Return namelist; } } CmpClient2.java / * * Create Date 2005-1-24 * * Author: javamxj (share happiness 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.user manufacture; 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 the template for this generated type comment, please turn to * Window - Preferences - Java - Code Style - 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 INIALCONTEXT; } Public void testbean () { Try { Javamxj.ejb.cmp.userManagement usermgmgmt = 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 ("Changing the name of User JavaMxj@yahoo.com.cn"); Usermgmt.changeename ("javamxj@yahoo.com.cn", "hello"); System.out.Println ("User with EJB.Finder outputs more than 30"); PrintList (usermgmt.getuserbyage1); System.out.println ("Use EJB.SELECT Output Ages for users"); PrintList (usermgmt.getuserbyage2); System.out.println ("Output User Information via ValueObject:"); Userinfovalue [] users = usermgmt.getusersInfo (); For (int index = 0; index Userinfovalue user = users [index]; System.out.println ("User" User.Getemail () ":" User.Getage ()); } System.out.Println ("Output of User of the Weed"); PrintList (usermgmt.getusersbyname); system.out.println ("Output user with kingman"); 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 (FINDEREXCEPTION E) { E.PrintStackTrace (); } catch (removeexcection e) { E.PrintStackTrace (); } } // Create a user Public Static Void CreateUsers (UserManagement Usermgmt) THROWS RemoteException, CreateException { System.out.println (Add data to 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@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", "Share Java Happy", "MXJ", "IT", "1788888", 26, 2900); } // Output 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) { CmpClient2 Test = New Cmpclient2 (); Test.TestBean (); } }