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

xiaoxiao2021-03-06  34

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 the joy of java) * / package javamxj.ejb.cmp / ** * * You can insert your Documentation for ' Groupbean '. * * @generated ** / public abstract class groupbean imports 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 immediately 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 ################################## Ejb.select * q uery = "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);

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

New Post(0)