Now you are ready for advanced techniques using JDBC. The persistence of stored data for J2EE applications is provided through JDBC DataSource connections communicating with a relational database server. If the persistence management for an entity EJB is bean-managed, you must write JDBC code in your entity bean. The ejbCreate() method inserts the attributes of the bean as a new row in the database table. The ejbFindByPrimaryKey() method queries the database to find a row that matches the primary key. The attributes of the bean object are updated with the results returned from the SELECT operation. The BookEntityBean is an entity EJB that is implemented using bean-managed persistence. The primary purpose for this example is to show how and where JBDC is used in an Enterprise JavaBean. Note Refer to Chapter 21 for further information on entity EJBs. The BookEntityBean contains three attributes: book_id , author_id , and book_name . The ejbCreate() method for this bean inserts a new row into the books table of the database containing these three fields. A BookEntityPK object is then created using the book_id as the primary key for this entity EJB. The ejbFindByPrimaryKey() method performs a query on the database to select the row with the matching book_id . The columns from the matching row are used to initialize the corresponding attributes of the BookEntityBean object. This example was created using Together ControlCenter version 6.0. An Entity EJB was added to the class diagram using the Entity EJB tool. The Properties Inspector includes an EJB Entity tab, which allows you to set the Persistence Management to Bean managed. The ejbCreate() and ejbFindByPrimaryKey() methods in the generated code must be modified to include your JDBC statements. The code is commented with //Write your code here to indicate where your code modifications should be entered. Figure 13.11 illustrates the class diagram for the BookEntityBean example, while Listing 13.8 details the Java source code for the BookEntityBean example. Figure 13.11. The BookEntityBean class diagram. Listing 13.8 Java Source for BookEntityBean Example [View full width] /* Generated by Together */ import java.sql.SQLException; import java.sql.DriverManager; import java.sql.Connection; import java.sql.PreparedStatement; /** * @ejbHome <{BookEntityHome}> * @ejbRemote <{BookEntity}> * @ejbPrimaryKey <{BookEntityPK} * @ejbTransactionAttribute Required * @persistent*/ public class BookEntityBean implements javax.ejb.EntityBean { private javax.ejb.EntityContext ctx; public int book_id; private int author_id; private String book_name; public void setEntityContext(javax.ejb.EntityContext context) throws javax.ejb ![]() |