EJB has two main types: BMP (bean managed persistence) and CMP (container managed persistence), which have their own advantages and disadvantages.
BMP is used to call database JDBC in bean. That is to say, in your entity bean, SQL statements such as "insert .. "or" select .. ", and use datasource to obtain a database resource and connection to directly add, delete, and modify the database.
CMP is automatically performed on the database by the EJB container, all you do is to rewrite the setxxx or getxxx method in the Entity Bean, and then define the CMP-field in the ejb-jar.xml.
Obviously, CMP writing is much simpler, and database operations are performed by EJB containers. However, CMP has a disadvantage that it is not flexible enough, if we want to complete the like Command similar to the SQL search statement, such as "select * from a where name like '% Banqiao'", CMP will not be able to help us automatically complete it, in this way, we need to write BMP by ourselves.
In practical application, we usually try to use CMP for efficiency considerations. But how to prepare for the possibility of using BMP in the future is to say that there is a foundation that can be extended to BMP. The support for CMP abstract classes in EJB 2.0 provides the basis for this implementation.
The general idea is to use an abstract class to complete CMP. If BMP is required, you can use extend as the abstract class and overwrite the original method (use your own special SQL statement operation to overwrite this method ).
Take the address Entity Bean: addressejb in Java pet store demo 1.3 as an example:
Public abstract class addressejb implements entitybean { Private entitycontext context = NULL; // Getters and setters for Po CMP Fields Public abstract string getfirstname (); Public abstract void setfirstname (string name ); Public abstract string getlastname (); Public abstract void setlastname (string name ); Public abstract string getstreet1 (); Public abstract void setstreet1 (string name ); Public abstract string getstreet2 (); Public abstract void setstreet2 (string name ); Public abstract string getcity (); Public abstract void setcity (string name ); Public abstract string getstate (); Public abstract void setstate (string name ); Public abstract string getcountry (); Public abstract void setcountry (string name ); Public abstract string getzip (); Public abstract void setzip (string name ); Public object ejbcreate (string fname, string lname, string S1, String S2, string cy, string St, String cnty, string pcode) Throws createexception { Setfirstname (fname ); Setlastname (lname ); Setstreet1 (S1 ); Setstreet2 (S2 ); Setcity (CY ); Setstate (ST ); Setcountry (cnty ); Setzip (pcode ); Return NULL; } Public void ejbpostcreate (string fname, string lname, string street1, String street2, string city, string state, String country, string zip) Throws createexception {} Public void setentitycontext (entitycontext c) {context = C ;} Public void unsetentitycontext (){} Public void ejbremove () throws removeexception {} Public void ejbactivate (){} Public void ejbpassivate (){} Public void ejbstore (){} Public void ejbload (){} } |
In the above addressejb, we can see that only setxxx or getxxx methods are available.
In the corresponding deployment description file ejb-jar.xml we see:
<Entity> <Display-Name> addressejb </display-Name> <EJB-Name> addressejb </EJB-Name> <Local-Home> com. Sun. J2EE. Blueprints. Address. EJB. addresslocalhome </Local-Home> <Local> com. Sun. J2EE. Blueprints. Address. EJB. addresslocal </local> <EJB-class> com. Sun. J2EE. Blueprints. Address. EJB. addressejb </EJB-class> <Persistence-type> container </persistence-type> <Prim-key-class> JAVA. Lang. Object </prim-key-class> <Reentrant> false </reentrant> <CMP-version> 2.x</CMP-version> <Abstract-schema-Name> address </abstract-schema-Name> <CMP-field> <Field-Name> firstname </field-Name> </CMP-field> <CMP-field> <Field-Name> lastname </field-Name> </CMP-field> <CMP-field> <Field-Name> street1 </field-Name> </CMP-field> <CMP-field> <Field-Name> street2 </field-Name> </CMP-field> <CMP-field> <Field-Name> city </field-Name> </CMP-field> <CMP-field> <Field-Name> state </field-Name> </CMP-field> <CMP-field> <Field-Name> country </field-Name> </CMP-field> <CMP-field> <Field-Name> zip </field-Name> </CMP-field> <Security-identity> <Description> </description> <Use-caller-identity> </use-caller-identity> </Security-identity> </Entity> |
In the above deployment file, the address database field is marked:
Firstname, lastname, street1, street2, city, state, country, zip
Once we use BMP, we only need to inherit the above CMP Bean:
Public class addressbeanbmp extends addressejb {
Use our own BMP method to overwrite the method in addressejb:
Ejbload () --> obtain data from the database (select)
Ejbstore () --> modify Database Data Update)
Ejbremove () --> delete database data)
Ejbcreate () --> insert)
Ejbfindbyprimarykey (primary key) --> ensure that the primary key exists.
Ejbfindallprimarykey () --> return the collectionxiam of all data records of a primary key.
The following uses ejbcreate () as an example:
Public object ejbcreate (string fname, string lname, string S1, String S2, string cy, string St, String cnty, string pcode) throws createexception { // Insert row into database This. fname = fname; This. lname = lname; This. S1 = S1; This. S2 = S2; This. Cy = Cy; This. St = sT; This. cnty = cnty; This. pcode = pcode; // Insert database record Try { Connection connection = getconnection (); Preparedstatement = connection. preparestatement ("Insert into address (firstname, lastname, street1, street2, city, state, country, zip) values (?, ?, ?,?,?,?) "); Statement. setstring (1, fname ); Statement. setstring (2, lname ); Statement. setstring (3, pcode ); Statement. setstring (4, S1 ); Statement. setstring (5, S2 ); Statement. setstring (6, St ); Statement. setstring (7, CY ); Statement. setstring (8, cnty ); If (statement.exe cuteupdate ()! = 1 ){ Statement. Close (); Connection. Close (); Throw new createexception ("cocould not create :"); } Statement. Close (); Connection. Close (); } Catch (sqlexception e ){ Throw new ejbexception ("cocould not create :"); } } |