J2EE design mode: CMP to BMP mode

xiaoxiao2021-03-06  41

EJB2.0 finally gave us a operable container management persistent (CMP) model. As long as it is possible, I use CMP Beans in my EJB project; however, because of this strange thing, I need to turn my entity into bean management (BMP). I use the following mode to make my cleanlore from the CMP model to the Bean management persistent model. In this article, we will discuss: 1.cmp 2.0: What happened? 2.INVENVENTORY EJB Application 3. Develop a CMP Bean 4. Transplant CMP bean to BMP CMP 2.0: What happened? When the EJB came out, CMP caused a lot of repercussions. When EJB2.0 came, the CMP model was revised, it gave us the characteristics of us: such as relationships and query language standardization. Compared with EJB1.1, when we write a CMP BEAN with EJB2.0, our writing is very different. We create an abstract class instead of creating a public domain that is managed by the container, we create an abstract property like JavaBean (Getters and setters). This allows specific vendors' persistence managers to implement data accessors (Accessors) in their own way. This will help them (vendors) proposed like this: ● Since they don't call any set methods, do not do any action in EJBStore () ● They only change a domain, so we only set the domain in the Update query statement ● We delay some data, so when we request data with the GET method, we read him by the way: Why do we have to create an abstract method in an abstract class? Why can't the Persistent Manager create a method in the derived class? A: We must access these methods in an abstract class. For example: In ejbcreate (), we set them through incoming parameters. Inventory EJB Application In order to illustrate the entity model, we will see a simple application to model the goods list (Inventory) system. Our application includes the following components: Inventory entity bean: This is our focus. He will map to Database Table Inventory, which records the name of the entry (main keyword), price and state of the number of entries in the warehouse. BEAN: This bean uses inventory entity bean to get the price of the entry, it uses local ( LOCAL) Interface to access the entity price Customer: This command line application runs this method on the session (SESSION) to test all work is running well, the key is when we transplant this Inventory entity from CMP to BMP implementation, Don't make a change, let's take a look at the use of CMP implementation inventory entity to develop a CMP bean inventory CMP To create an abstract class (in accordance with entity description), the XML description file tells the framework what should be smashed, the picture shows our entry looks. What is what?

Figure one

Abstract entity bean

The abstract entity has the following properties:

Implement the Javax.ejb.EntityBean interface

Abstract Public Class InventoryBean Implements EntityBean

Method for declaring in an entity interface

public void setEntityContext (EntityContext context) {ctx = context;} public void unsetEntityContext () {ctx = null;} public void ejbActivate () {} public void ejbPassivate () {} public void ejbRemove () throws RemoveException {} public void ejbStore () {} Public void ejbload () {} Implementation EJBCREATE () and EJBPOSTCREATE () Method The method corresponds to the Create () method in the Home interface (all parameters for the Abstract SET method)

Public String EJBCREATE (String Item, Float Price, Int stock "CreateException {setitem (item); setstock; Return null;}

Public void ejbpostcreate (String Item, Float Price, Int stock) throws createException {}

Implement abstract GET and SET methods

public abstract String getItem (); public abstract void setItem (String item); public abstract float getPrice (); public abstract void setPrice (float price); public abstract int getStock (); public abstract void setStock (int stock);

Implement a helper method to access the entity context (we will use it in the back BMPBean)

Public EntityContext () {returnText () {Return CTX;

EJB Deployment Descriptor We created a CMP entity class, which now creates a deployment descriptor. First we will create a standard "EJB-JAR.XML", then we need to configure information related to the vendor. We will show WebLogic 6.1 Deployment descriptor, we choose one. Standard EJB-JAR.XML basic entity configuration information: We will configure class names, use local variables, they are local variables inside the entity. We tell containers through a persistent type XML tag that this entity is CMP InventoryBean InventoryHome inventory> / local InventoryBean Container java.lang.string false then we will tell containers those domains in containers, The ITEM domain is the primary key class:

WebLogic-ejb-jar.xml of a particular vendor We first define a specific information of vendor in WebLogic-EJB-JAR.XML. Here we tell the server where to store local stubs (stub) and where to find CMP mapping. Persistent mapping information: WebLogic_cmp_rdbms 6.0 meta-inf / weblogic-cmp-rdbms-jar. XML Local JNDI Name InventoryHome WebLogic-cmp-rdbms-jar.xml data source (DataSource) name :( This data source is created when configuring) InventoryDB table (Table) Name: Inventory Domain (Field) Map Stock stock Item item price price Now we have everything we want to have. The container will use abstract classes and deployment information to make everything is very good. For some reason, we may make this CmpBean to BMP. The following is why we have to do this 1. Portability: Currently, it works only in WebLogic 6.1, and we don't want to learn other tools. This is very normal if we want to sell this database component - it can run on any server. 2. Performance: If we want to run SQL (combination with stored procedures, combination of tables). 3. Alternative data sources: If we want to access different data sources instead of RDBMS. Transplantation with BMP Beans We will discuss the transplant mode. We can transplant the CMP to BMP via the "Transferring Manager" mode. We can see that the lasting manager extends the abstract class we created and implemented an abstract class. The final design looks like Figure 2: Figure 2 BMP mode

BMP entity bean let's take a look at the bean class that is about to be BMPBean: Extended abstract cmpbean class: public class inventorybeanbmp extends InventoryBean {} Create a mapping area: public string item; public float price; public int Stock; rewrite EJB * () series method : In order to rewrite the EJB method, we need to remember our responsibility to each method in BMP.

Most items look like, so let's see one of the ways (you can see the rest of the code in the downloaded code) PUBLIC STRING EJBCREATE (String Item, Float Price, Int stock) throws createException {// INSERT ROW into database this.item = item; this.price = price; this.stock = stock; // insert database records try {Connection connection = getConnection (); PreparedStatement statement = connection.prepareStatement ( "iNSERT INTO inventory (item, price, VALUES (?,?,?) "); statement.setstring (1, item); statement.setfloat (2, price); statement.setint (3, stock); if (statement.executeUpdate ()! = 1 ) {Statement.close (); connection new create: item);} Statement.close (); connection.close (); Return Item;} catch (SQLException E) {Throw new ejbexception ("Could Not Create: Item, E);} Get and SET methods: public string getItem () {Return this.Item;} public void settem (String item) {this.Item = ITEM Public float getprice () {return this.price;} PUB {this.price = price;} public int GETSTOCK;} public void setstock;} {this.stock = stock;} Auxiliary function Get a JDBC connection: private Connection getConnection () throws SQLException {DataSource ds = null; try {Context ctx = new InitialContext (); ds = (DataSource) ctx.lookup ( "java: comp / env / jdbc / InventoryDB");} catch (NamingException exp) {Exp.PrintStackTrace ();} return (DS == NULL) NULL: DS.GetConnection ();} EJB Deployment Descriptor: Standard EJB-JAR.XML Change the deployment environment when converting from CMP into BMP: Class points to BMP The class is not an abstract class, we want the container to know our own management lasting:

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

New Post(0)