This series of articles I published in IBM website according to 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 multi-to-many one-way contacts, which is designed here, and UserBean is designed to control the rolebean by UserBean. The focus is on how to build multiple one-way contacts using XDoclets, and the client call is relatively simple.
This article is ""
Eclipse quickly picks EJB - 2. Design an entity bean ","
Eclipse quickly applied EJB - 3. Design a pair of CMR (1) "extends. Therefore, there must be the foundation of these two articles.
Regarding the environmental configuration, use skills to refer to my previous article, here as much as possible.
First, design entity bean: rolebean
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): Role
· EJB TYPE: Select Container Managed Entity EJB
Click Next.
● Schema Name: Roleschema
Table name: Roletable
● Add a NAME column:
· Field: Name
· Field Type: java.lang.string
· Database column: Permissions Name
· SQL TYPE: VARCHAR
And make it a primary key.
● Similarly, add a DESCRIPTION column:
· Field: Description
· Field Type: java.lang.string
· Database column: permission description
· SQL TYPE: VARCHAR
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:
● The complete source code for RoleBean.java is given below:
Rolebean.java
/ *
* Create a date 2005-1-25
*
* Author: javamxj (share happiness java)
* /
Package javamxj.ejb.cmp;
/ **
*
* you can insert your documentation for ' Rolebean b> em>'. *
XML Version = "1.0" encoding = "UTF-8"?>
lomboz: Entityejb>
lomboz: FIELDMAPPINGS>
lomboz: FIELDMAPPINGS>
lomboz: Entity>
lomboz: EJB>
*
*
* @ ejb.bean name = "role"
* JNDI-Name = "role"
* Type = "cmp"
* Primkey-Field = "Name"
* Schema = "Roleschema"
* Cmp-Version = "2.x"
* View-type = "local"
* Data-Source = ""
*
* @ Ejb.Persistence
* Table-name = "Roletable"
*
* @ jboss.Persistence
* Create-Table = "True"
* Remove-Table = "false"
*
* @ ejb.finder
* query = "SELECT OBJECT (a) from Roleschema As A"
* Signature = "java.util.collection Findall ()"
*
* @ ejb.pk class = "java.lang.string"
*
* @generated
** /
Public Abstract Class Rolebean Implements Javax.ejb.entityBean {
/ **
* @ ejb.create-method
* /
Public Java.lang.String Ejbcreate (String Name, String Description) throws javax.ejb.createException {
// EJB 2.0 Spec Says Return Null for CMP EJBCREATE METHODS.
SetName (Name);
SetDescription (Description);
Return NULL;
}
/ **
* The Container Invokes this Method Immedierately After It Calls Ejbcreate.
* /
Public void ejbpostcreate (String name, string description) throws javax.ejb.createException {
}
/ **
* CMP Field Name
* @Return the name
* @ Ejb.Persistent-Field
* @ Ejb.Persistence
* Column-name = "Permissions Name"
* JDBC-TYPE = "varchar"
* SQL-TYPE = "VARCHAR (12)"
* read-only = "false"
* @ ejb.pk-field *
* @ 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 Description
* @Return the description
* @ Ejb.Persistent-Field
* @ Ejb.Persistence
* Column-name = "Permission Description"
* JDBC-TYPE = "varchar"
* SQL-TYPE = "VARCHAR (30)"
* read-only = "false"
*
*
* @ ejb.interface-method
* /
Public Abstract Java.lang.String getDescription ();
/ **
* @Param java.lang.string the New Description Value
* @ ejb.interface-method
* /
Public Abstract void setdescription (java.lang.string description);
}
Ok, save, join rolebean into the CMPEJB module, then lomboz ->
Generate EJB Classes.
4. Focus: Establish Userbean's multiple-to-many one-way contacts for Rolebean:
· Since this design is Userbean's one-way link to RoleBean, you only need to add a method tag in the UserBean.
Switch to
Userbean.java, right-click Userbean -> J2EE -> Add
CMR RELATIONSHIP:
· The resulting marker is as follows, but also needs to add some @Jboss tags:
· Save,
Generate EJB CLASSES
Eclipse quickly applied EJB - 4. Multi-to-many one-way relationship CMR (2) connected:
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 relationship
// ******************************************************** *********************
/ **
* Create permission
* 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 permissions
* @Throws FinderException
* @ ejb.interface-method view-type = "remote"
* /
Public ArrayList Gtroles () throws FinderException {
ArrayList RoleList = New ArrayList (30);
ITerator iter = rolehome.findall (). Iterator ();
While (ore.hasnext ()) {
ROLELOCAL ROLE = (ROLELOCAL) ITER.NEXT ();
RoleList.Add (roule.getname ());
}
Return RoleList;
}
/ **
* Delete permissions
* @Throws RemoveException
* @Throws EJBEXCEPTION
* @ ejb.interface-method view-type = "remote"
* /
Public void removerole (String name) throws ejbexception, removeexception {
Rolehome.Remove (Name);
}
/ **
* Add permissions to users
* @Throws FinderException
* @ ejb.interface-method view-type = "remote"
* /
Public void addrole (String email, string rolename) throws findexception {
UserLocal User = UserHome.FindByPrimaryKey (email);
Collection Roles = User.getroles ();
RoLELOCAL ROLE = RoleHome.FindByPrimaryKey (ROLENAME);
Roles.Add (Role);
}
/ **
* Get permissions owned by a user
* @Throws FinderException
* @ ejb.interface-method view-type = "remote"
* /
Public ArrayList GetUserRole (String email) throws FinderException {
ArrayList RoleList = New ArrayList (30);
UserLocal User = UserHome.FindByPrimaryKey (email);
ITerator it = user.getroles (). Itrator ();
While (ore.hasnext ()) {
ROLELOCAL ROLE = (ROLELOCAL) ITER.NEXT ();
RoleList.Add (roule.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 a date 2005-1-25
*
* 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.user manufacture;
Import javax.ejb.createException;
Import javax.ejb.finderexception;
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 CMPCLIENT3 {
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 usermgmt = gethome (). Create (); createUsers (usermgmt);
System.out.println ("Show All Permissions");
PrintList (usermgmt.getroles ());
System.out.println ("Add Permissions to User Wangyi@aaa.com);
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 permissions of JavaMxj");
PrintList (Usermgmt.getuserrole ("javamxj@yahoo.com));
} catch (remoteException 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", "General User");
Usermgmt.createrole ("Guest", "guest user");
}
// 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) {
CmpClient3 test = new cmpclient3 ();
Test.TestBean ();
}
}
3. Run the client
· Console output:
· Changes in the database in the database:
3. Add the following tags to the class tag:
● The complete source code for RoleBean.java is given below:
Rolebean.java
/ *
* Create a date 2005-1-25 *
* Author: javamxj (share happiness java)
* /
Package javamxj.ejb.cmp;
/ **
*
* you can insert your documentation for ' Rolebean b> em>'. *
XML Version = "1.0" encoding = "UTF-8"?>
lomboz: Entityejb>
lomboz: FIELDMAPPINGS>
lomboz: FIELDMAPPINGS>
lomboz: Entity>
lomboz: EJB>
*
*
* @ ejb.bean name = "role"
* JNDI-Name = "role"
* Type = "cmp"
* Primkey-Field = "Name"
* Schema = "Roleschema"
* Cmp-Version = "2.x"
* View-type = "local"
* Data-Source = ""
*
* @ Ejb.Persistence
* Table-name = "Roletable"
*
* @ jboss.Persistence
* Create-Table = "True"
* Remove-Table = "false"
*
* @ ejb.finder
* query = "SELECT OBJECT (a) from Roleschema As A"
* Signature = "java.util.collection Findall ()"
*
* @ ejb.pk class = "java.lang.string"
*
* @generated
** /
Public Abstract Class Rolebean Implements Javax.ejb.entityBean {
/ **
* @ ejb.create-method
* /
Public Java.lang.String Ejbcreate (String Name, String Description) throws javax.ejb.createException {
// EJB 2.0 Spec Says Return Null for CMP EJBCREATE METHODS.
SetName (Name);
SetDescription (Description);
Return NULL;
}
/ **
* The Container Invokes this Method Immedierately After It Calls Ejbcreate.
* /
Public void ejbpostcreate (string name, string description) throws javax.ejb.createException {}
/ **
* CMP Field Name
* @Return the name
* @ Ejb.Persistent-Field
* @ Ejb.Persistence
* Column-name = "Permissions Name"
* JDBC-TYPE = "varchar"
* SQL-TYPE = "VARCHAR (12)"
* read-only = "false"
* @ ejb.pk-field
*
* @ 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 Description
* @Return the description
* @ Ejb.Persistent-Field
* @ Ejb.Persistence
* Column-name = "Permission Description"
* JDBC-TYPE = "varchar"
* SQL-TYPE = "VARCHAR (30)"
* read-only = "false"
*
*
* @ ejb.interface-method
* /
Public Abstract Java.lang.String getDescription ();
/ **
* @Param java.lang.string the New Description Value
* @ ejb.interface-method
* /
Public Abstract void setdescription (java.lang.string description);
}
Ok, save, join rolebean into the CMPEJB module, then lomboz ->
Generate EJB Classes.
4. Focus: Establish Userbean's multiple-to-many one-way contacts for Rolebean:
· Since this design is Userbean's one-way link to RoleBean, you only need to add a method tag in the UserBean.
Switch to
Userbean.java, right-click Userbean -> J2EE -> Add
CMR RELATIONSHIP:
· The resulting marker is as follows, but also needs to add some @Jboss tags:
· Save,
Generate EJB CLASSES
Eclipse quickly applied EJB - 4. Multi-to-many one-way relationship CMR (2) connected:
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 permission
* 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 permissions
* @Throws FinderException
* @ ejb.interface-method view-type = "remote"
* /
Public ArrayList Gtroles () throws FinderException {
ArrayList RoleList = New ArrayList (30);
ITerator iter = rolehome.findall (). Iterator ();
While (ore.hasnext ()) {
ROLELOCAL ROLE = (ROLELOCAL) ITER.NEXT ();
RoleList.Add (roule.getname ());
}
Return RoleList;
}
/ **
* Delete permissions
* @Throws RemoveException
* @Throws EJBEXCEPTION
* @ ejb.interface-method view-type = "remote"
* /
Public void removerole (String name) throws ejbexception, removeexception {
Rolehome.Remove (Name);
}
/ **
* Add permissions to users
* @Throws FinderException
* @ ejb.interface-method view-type = "remote"
* /
Public void addrole (String email, string rolename) throws findexception {
UserLocal User = UserHome.FindByPrimaryKey (email);
Collection Roles = User.getroles ();
RoLELOCAL ROLE = RoleHome.FindByPrimaryKey (ROLENAME);
Roles.Add (Role);
}
/ **
* Get permissions owned by a user
* @Throws FinderException
* @ ejb.interface-method view-type = "remote"
* /
Public ArrayList GetUserRole (String email) throws FinderException {
ArrayList RoleList = New ArrayList (30);
UserLocal User = UserHome.FindByPrimaryKey (email); iterator iter = user.getroles (). Iterator ();
While (ore.hasnext ()) {
ROLELOCAL ROLE = (ROLELOCAL) ITER.NEXT ();
RoleList.Add (roule.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 a date 2005-1-25
*
* 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.user manufacture;
Import javax.ejb.createException;
Import javax.ejb.finderexception;
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 CMPCLIENT3 {
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 / automation // 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 Permissions");
PrintList (usermgmt.getroles ());
System.out.println ("Add Permissions to User Wangyi@aaa.com);
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 permissions of JavaMxj");
PrintList (Usermgmt.getuserrole ("javamxj@yahoo.com));
} catch (remoteException 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", "General User");
Usermgmt.createrole ("Guest", "guest user");
}
// 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) {
CmpClient3 test = new cmpclient3 ();
Test.TestBean ();
}
}
3. Run the client
· Console output:
· Changes in the database in the database: