Eclipse quickly EJB - 5. CMR of a couple of two-way relationships

xiaoxiao2021-03-06  14

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 a pair of two-way contacts, which is designed here, and UserInfobean (note that the IBM website is in connection with Userbean, here I think it is more appropriate to build contacts with UserInfobean). The focus is on how to build a pair of two-way contacts using XDoclet, and the client call is relatively simple.

This article is ""

Eclipse quickly picks EJB - 2. Design an entity bean ","

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

Eclipse rapid up hand EJB - 4. Multi-to-many one-way relationship CMR (1) "extends. Therefore, there must be the foundation of these three articles.

Regarding the environmental configuration, use skills to refer to my previous article, here as much as possible.

First, design entity bean: groupbean

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): Group

· EJB TYPE: Select Container Managed Entity EJB

Click Next.

● Schema name: Groupschema

Table Name: GroupTable

● Add a NAME column:

· Field: Name

· Field Type: java.lang.string

· Database column: Name

· SQL TYPE: VARCHAR

And make it a primary key.

● Similarly, add a DESCRIPTION column:

· Field: Description

· Field Type: java.lang.string

· Database column: Industry 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:

4. Add a SELECT METHOD, you can use JBoss-IDE to get, here is the code (there is a complete source code):

// ################# SELECT METHOD ####################### ########

/ *** SELECT METHOD * @ ejb.select * query = "SELECT USER.EMAIL From Groupschema AS G, IN (G.USERS) AS USER WHERE G.NAME =? 1" * / public abstract java.util.collection ejbSelectUserIDs (String groupName) throws javax.ejb.FinderException; / *** Home method * @throws FinderException * @ ejb.home-method view-type = "local" * / public java.util.ArrayList ejbHomeGetUserIDs (String groupName) throws Javax.ejb.findeRexception {return (java.util.arraylist) EJBSELECTUSERIDS (Groupname);

5. Add GroupBean to the CMPEJB module, then lomboz -> Generate EJB CLASSES

6. Key: Establish a pair of two-way contacts with USERINFOBEAN:

● Right-click GroupBean -> J2EE -> Add CMR Relationship:

The generated code refers to the complete source code given below.

● Switch to

UserInfobean.java:

Right-click UserInfobean -> J2EE -> Add CMR Relationship:

The code generated in UserInfobean.java (need to be added manually

@ jboss.relation):

/ *** getter for cmr reviewship ** @ Ejb.interface-method view-type = "local" * @ Ejb.relation name = "groupshaveuserinfos" * role-name = "userinfosingroup" * target-multiple = "yes" * @ jboss.relation * fk-column = "Group" * Related-pk-field = "name" * / public abstract javamxj.ejb.cmp.grouplocal getGroup (); / *** setter for cmr reason @EJB .interface-method view-type = "local" * / public abstract void setgroup (javamxj.ejb.cmp.grouplocal value);

OK, again generate ejb classes, if everything is normal, proceed to the next step.

● The full source code for GroupBean.java is given below:

GroupBean.java

/ *

* Create date 2005-1-26

*

* Author: javamxj (share happiness java)

* /

Package javamxj.ejb.cmp;

/ **

*

* you can insert your documentation for ' Groupbean '. *

Group

Group

javamxj.ejb.cmp.groupBean

Container

java.lang.string

2.x

Groupschema

Name

Name

java.lang.string

Name

VARCHAR

VARCHAR

False

True

Description

java.lang.string

Industry Description

VARCHAR

VARCHAR false

False

GroupTable

*

*

* @ ejb.bean name = "group"

* JNDI-NAME = "group"

* Type = "cmp"

* Primkey-Field = "Name"

* Schema = "groupschema"

* Cmp-Version = "2.x"

* View-type = "local"

* Data-Source = ""

*

* @ Ejb.Persistence

* Table-name = "grouptable"

*

*

*

* @ ejb.finder

* query = "SELECT Object (a) from groupschema as a"

* Signature = "java.util.collection Findall ()"

*

* @ ejb.pk class = "java.lang.string"

*

* @generated

** /

Public Abstract Class GroupBeaN 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 = "Name"

* JDBC-TYPE = "varchar"

* SQL-TYPE = "VARCHAR (24)"

* 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 = "Industry Description"

* JDBC-TYPE = "varchar"

* SQL-TYPE = "VARCHAR (24)"

* 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);

// ################# SELECT METHOD ####################### ########

/ **

* SELECT METHOD

* @ ejb.select

* query = "SELECT USER.EMAIL From Groupschema AS G, IN (G.USERS) AS User Where g.name =? 1"

*

* /

Public Abstract Java.util.Collection EJBSELECTUSERIDS (String Groupname)

Throws javax.ejb.finderexception;

/ **

* Home Method

* @Throws FinderException

* @ ejb.home-method view-type = "local"

* /

Public Java.util.ArrayList EjBhomegetUserIDS (String Groupname)

Throws javax.ejb.finderexception {

Return (java.util.ArrayList) EJBSELECTUSERIDS (Groupname);

}

// ################# CMR Relationship ###################### ########

/ **

* Getter for CMR RelationShip

*

* @ ejb.interface-method view-type = "local"

* @ ejb.relation name = "groupshaveuserinfos"

* role-name = "grouphasuserinfos" * target-multiple = "no"

* /

Public abstract java.util.collection getusers ();

/ **

* Setter for CMR Relationship

*

* @ ejb.interface-method view-type = "local"

* /

Public Abstract void setUsers (java.util.collection value);

}

Second, add business methods in UserManagementBean

1. Add a GroupLocalHome's variable GroupHome and put it in the EJBCREATE and EJBPassivate methods.

2. Add the following business methods in turn.

Part of GroupBean in UserManagementBean (very simple) // ************************************************************************************************************************************ ***************************************

// a pair of two-way relationship

// ******************************************************** *********************

/ **

* Create a group

* @Throws CreateException

* @ ejb.interface-method view-type = "remote"

* /

Public Void CreateGroup (String Groupname, String Description)

Throws createException {

Grouphome.create (Groupname, Description);

}

/ **

* Delete group

* @Throws RemoveException

* @Throws EJBEXCEPTION

* @ ejb.interface-method view-type = "remote"

* /

Public void transovegroup (String Groupname)

Throws EJBEXCEPTION,

REMOVEEXCEPTION {

Grouphome.Remove (Groupname);

}

/ **

* Show all groups

* @Throws FinderException

* @ ejb.interface-method view-type = "remote"

* /

Public arraylist getgroups () throws FinderException {

ArrayList GroupList = New ArrayList (30);

Iterator it = grouphome.findall (). Iterator ();

While (ore.hasnext ()) {

Grouplocal group = (grouplocal) iTer.next ();

Grouplist.add (group.getname ());

}

Return GroupList;

}

/ **

* Add users to groups

* @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);

}

/ **

* Verify that the user is located

* @Throws FinderException

* @ ejb.interface-method view-type = "remote"

* /

Public Boolean Ingroup (String email, string groupname)

Throws FingerException {

UserInfolocal User = InfoHome.FindByPrimaryKey (email);

Return User.getGroup (). getname (). Equals (groupname);

}

/ **

* Add permissions to a group of users

* @Throws FinderException

* @ ejb.interface-method view-type = "remote"

* /

Public void addroleTousers (String Groupname, String Rolename)

Throws FingerException {

Grouplocal group = grouphome.findbyprimaryKey (Groupname);

RoLELOCAL ROLE = RoleHome.FindByPrimaryKey (ROLENAME);

Iterator it = group.getusers (). Itrator ();

While (ore.hasnext ()) {

UserInfolocal User = (UserInfolocal) iTer.next ();

User.getuser (). getroles (). Add (role);

}

}

/ **

* Output a group of users via ejb.finder

* @Throws FinderException

* @ ejb.interface-method view-type = "remote"

* /

Public ArrayList GetUseridSingroup1 (String Groupname)

Throws FingerException {

ArrayList UserList = New ArrayList (30);

Grouplocal group = grouphome.findbyprimaryKey (Groupname);

Iterator it = group.getusers (). Itrator ();

While (ore.hasnext ()) {

UserInfolocal element = (userInfolocal) .next ();

UserList.Add (Element.Getemail ());

}

Return UserList;

}

/ **

* Output a group of users via ejb.select

* Business Method

* @Throws FinderException

* @ ejb.interface-method view-type = "remote"

* /

Public ArrayList GetUseridSingRoup2 (String Groupname) throws FinderException {

Return Grouphome.getUserids (Groupname);

}

3. Ok, save, Generate EJB Classes, start mysql, start JBoss server, deploy module.

If everything is normal, a table is automatically created in the CMP_SAMPLE library: GroupTable.

Third, create a client

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

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

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

Here is a complete source code:

CmpClient4.java

/ *

* Create date 2005-1-26

*

* 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 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 usermgmgmt = gethome (). Create ();

CreateUsers (usermgmt);

System.out.println ("Output Group Name in Group Table:");

PrintList (usermgmt.getgroups ());

System.out.println ("Before the change, the EJB.FINDER output group is IT user");

PrintList (usermgmt.getuseridsingroup1 ("it"));

System.out.println ("Change: Add Lisi@bbb.ccc to IT");

Usermgmt.moveusertogroup ("Lisi@bbb.ccc", "it");

SYSTEM.OUT.Println ("After the change, the group is used by the EJB.SELECT Output Group");

PrintList (usermgmt.getuseridsingroup2 ("it"));

System.out.println ("Add Permissions through User Group");

Usermgmt.addroleTousers ("IT", "Manager");

System.out.Println ("Verify the user group");

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 ();

}

}

// Create a user

Public Static Void CreateUsers (UserManagement Usermgmt)

THROWS RemoteException, CreateException {

System.out.println (Add data to the Group ");

Usermgmt.creategroup ("Marketing", "Market Group");

Usermgmt.creategroup ("Engineering", "Engineering Group");

Usermgmt.creategroup ("Sales", "Sales Group");

Usermgmt.creategroup ("IT", "Information Technology Group");

}

// 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) {

CmpClient4 test = new cmpclient4 ();

Test.TestBean ();

}

}

3. Run the client

· Console output:

· Changes in the database in the database:

Third, create a client

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

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

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

Here is a complete source code:

CmpClient4.java

/ *

* Create date 2005-1-26

*

* 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 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 INIALCONTEXT;

}

Public void testbean () {

Try {

Javamxj.ejb.cmp.userManagement usermgmgmt = gethome (). Create ();

CreateUsers (usermgmt);

System.out.println ("Output Group Name in Group Table:");

PrintList (usermgmt.getgroups ());

System.out.println ("Before the change, the EJB.FINDER output group is IT user");

PrintList (usermgmt.getuseridsingroup1 ("it"));

System.out.println ("Change: Add Lisi@bbb.ccc to IT");

Usermgmt.moveusertogroup ("Lisi@bbb.ccc", "it");

SYSTEM.OUT.Println ("After the change, the group is used by the EJB.SELECT Output Group");

PrintList (usermgmt.getuseridsingroup2 ("it"));

System.out.println ("Add Permissions through User Group");

Usermgmt.addroleTousers ("IT", "Manager");

System.out.Println ("Verify the user group");

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 ();

}

}

// Create a user

Public Static Void CreateUsers (UserManagement Usermgmt)

THROWS RemoteException, CreateException {

System.out.println (Add data to the Group ");

Usermgmt.creategroup ("Marketing", "Market Group");

Usermgmt.creategroup ("Engineering", "Engineering Group"); Usermgmt.createGroup ("Sales", "Sales Group");

Usermgmt.creategroup ("IT", "Information Technology Group");

}

// 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) {

CmpClient4 test = new cmpclient4 ();

Test.TestBean ();

}

}

3. Run the client

· Console output:

· Changes in the database in the database:

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

New Post(0)