A Preliminary Study on the rules for writing BMP object beans

Source: Internet
Author: User

Now, let's start with the writing rules of the bank account Entity Bean: AccountBean.

AccountEJB consists of three files:

L Entity Bean class (AccountBean)

L Home interface (AccountHome)

L Remote interface (Account)

The procedure is as follows:

First, implement the EntityBean interface: public class AccountBean implements EntityBean {...}

I. a. Declare the context of the object environment: protected EntityContext ctx;

B. Declare Bean-manage entity state Fields

Private String accountID; // Note: The primary key does not have to have the same name as the primary key field in the database.

Private String ownerName;

Private double balance;

C. Create a constructor public AccountBean (){...}

2. compile business logic methods

Public void deposit (double amt) throwsAccountException {...} // deposit

Public void withdraw (double amt) throws AccountException {...} // withdrawal

3. Compile the getter/setter method of the object state field

Public double getBalance (){...}

Public void setOwnerName (String name ){...}

Public String getOwnerName (){...}

Public String getAccountID (){...}

Public void setAccountID (String id ){...}

4. Compile the business logic method of the main interface (the method starts with "ejbHome"), which is independent of any specific account instance.

Public double ejbHomeGetTotalBankValue () throws AccountException {...}

5. Implement Methods in the EntityBean interface (these methods are called by the container)

Public void ejbActivate (){...}

Public void ejbRemove () throws RemoveException {...}

Public void ejbPassivate (){...}

Public void ejbLoad (){...}

Public void ejbStore (){...}

Public void setEntityContext (EntityContext ctx ){...}

Public void unsetEntityContext (){...}

6. Compile and create (at least one pair of ejbCreate and ejbPostCreate methods are required) and search methods

Public void ejbPostCreate (String accountID, String ownerName ){...}

Public AccountPK ejbCreate (String accountID, String ownerName) throws CreateException {...}

Public AccountPK ejbFindByPrimaryKey (AccountPK key) throws FinderException {...}

Public Collection ejbFindByOwnerName (String name) throws FinderException {...}

7. Define methods for obtaining jdbc database connections from the connection pool

Public Connection getConnection () throwsException {...}

 

Certificate -------------------------------------------------------------------------------------------------------------------------------------

 

Now, write the remote business interface Account, which inherits the EJBObject interface: public interface Account extends EJBObject {...}

I. business logic interface in AccountBean above

Public void deposit (double amt) throws AccountException, RemoteException;
Public void withdraw (double amt) throws AccountException, RemoteException;

2. interface of the getter/setter method in AccountBean above

Public double getBalance () throws RemoteException;

Public String getOwnerName () throws RemoteException;
Public void setOwnerName (String name) throws RemoteException;

Public String getAccountID () throws RemoteException;
Public void setAccountID (String id) throws RemoteException;

 

 

Certificate -------------------------------------------------------------------------------------------------------------------------------------

 

Now let's write the main interface AccountHome, which inherits the EJBHome interface: public interface AccountHome extends EJBHome {...}

1. Compile the create () method and the ejbCreate () method in AccountBean. The return type is remote commercial interface Account (Account)

Public Account create (String accountID, String ownerName) throws CreateException, RemoteException;

2. Compile search methods

A. Find the Account using the primary key, which corresponds to the ejbFindByPrimaryKey () method in AccountBean:

Public Account findByPrimaryKey (AccountPK key) throws FinderException, RemoteException;

B. Use ownerName (account owner) to find the account, which corresponds to the ejbFindByOwnerName () method in AccountBean:

Public Collection findByOwnerName (String name) throws FinderException, RemoteException;

3. Compile an independent main interface business method. It is independent of any specific account and corresponds to the ejbHomeGetTotalBankValue () method in AccountBean:

Public double getTotalBankValue () throws AccountException, RemoteException;

 

Certificate -------------------------------------------------------------------------------------------------------------------------------------

 

Now write the Account primary key class AccountPK, which implements the Serializable interface: public class AccountPK implements java. io. Serializable {

The format of primary key classes is very simple and fixed, as shown below:
Public String accountID; // primary key

Public AccountPK (String id ){
This. accountID = id;
}

Public AccountPK (){
}

Public String toString (){
Return accountID;
}

Public int hashCode (){
Return accountID. hashCode ();
}
 
Public boolean equals (Object account ){
Return (AccountPK) account). accountID. equals (accountID );
}
}
Certificate -------------------------------------------------------------------------------------------------------------------------------------

 

Write the Exception class AccountException, which inherits Exception:

Public class AccountException extends Exception {
Public AccountException () {super ();}

Public AccountException (Exception e) {super (e. toString ());}

Public AccountException (String s) {super (s );}

}

 

Certificate -------------------------------------------------------------------------------------------------------------------------------------

Deployment description:

Ejb-jar.xml:

<? Xml version = "1.0"?>

<! DOCTYPE ejb-jar PUBLIC '-// Sun Microsystems, Inc. // DTD Enterprise JavaBeans 2.0 // en' 'HTTP: // java.sun.com/dtd/ejb-jar_2_0.dtd'>

<Ejb-jar>
<Enterprise-beans>
<Entity>
<Ejb-name> Account </ejb-name>
<Home> examples. AccountHome <Remote> examples. Account </remote>
<Ejb-class> examples. AccountBean </ejb-class>
<Persistence-type> Bean </persistence-type>
<Prim-key-class> examples. AccountPK </prim-key-class>
<Reentrant> False </reentrant>

</Entity>
</Enterprise-beans>
</Ejb-jar>

 

Jboss. xml:

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE jboss PUBLIC "-// JBoss // dtd jboss 3.0 // EN" "http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd">

<Jboss>

<Enterprise-beans>

<Entity>
<Ejb-name> Account </ejb-name>
<Jndi-name> AccountHome </jndi-name>
</Entity>
</Enterprise-beans>

</Jboss>


Persistence. xml:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Persistence xmlns = "http://java.sun.com/xml/ns/persistence" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version = "1.0">

<Persistence-unit name = "bbmatrix PPU" transaction-type = "JTA">
<Jta-data-source> java:/MySqlDS </jta-data-source>
</Persistence-unit>

</Persistence>

So far, the entire BMP Entity Bean writing process is over!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.