Download presentation
Presentation is loading. Please wait.
Published byMagnus Thomas Modified over 9 years ago
1
creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK kwickramanayake@virtusa.com Version:0.1 Last Updated: 11-Sept-2003 Bean Managed Persistence Container Managed Persistence
2
creating competitive advantage Copyright © 2003 creating competitive advantage Contents Bean Managed Persistence SQL overview JDBC overview Benefits of BMP Defining various methods Container Managed Persistence Benefits Primary key with CMP Defining various methods Finder methods Specifying CMP in DD
3
creating competitive advantage Copyright © 2003 Bean Managed Persistence
4
creating competitive advantage Copyright © 2003 creating competitive advantage SQL Overview Insertion: INSERT INTO emp_details (first_name, last_name, emp_id) VALUES (‘Nimal’, ‘Fernando’, 203); Selection: SELECT * FROM emp_details WHERE emp_id = 202; Deletion: DELETE FROM emp_details WHERE emp_id = 123; emp_idfirst_namelast_name 102KamalWickramanayake 204ArjunaSamaraweera 23IndakaRaigama
5
creating competitive advantage Copyright © 2003 creating competitive advantage JDBC Overview DriverManager Access a JDBC driver Open a connection Create a statement object Execute the SQL query DataSource Can offer automatic connection pooling and distributed transactions Provides consistent access through JNDI for better portability public void setEntityContext(EntityContext ctx) { this.ctx = ctx; try { InitialContext ic = new InitialContext(); ds = (DataSource) ic.lookup(“java:comp/env/jdbc/StudentDB”); catch (Exception e){ throw new EJBException(“Unable to get DataSource”); } }
6
creating competitive advantage Copyright © 2003 creating competitive advantage Benefits of BMP Typically uses simple JDBC calls Allows you flexibility in accessing database Does not require complex support from container Simpler to debug, because you are familiar with your code Waiting figures
7
creating competitive advantage Copyright © 2003 creating competitive advantage Features of the BMP Entity Bean Class The class is defined as public. The class cannot be defined as abstract or final. It contains an empty constructor. It does not implement the finalize method.
8
creating competitive advantage Copyright © 2003 creating competitive advantage Defining ejbCreate() Inserts a row into the database public String ejbCreate(String key, String name, String address) throws CreateException { if(key == null) { throw new CreateException(“Can’t create with null ID”); } Connection con = null; PreparedStatement stmt = null; try { conn = ds.getConnection(); stmt = conn.prepareStatement(“INSERT INTO STUDENTS (student, name, address) VALUES (?, ?, ?)”); stmt.setString(1, key); stmt.setString(2, name); stmt.setString(3, address); stmt.executeUpdate(); } catch (SQLException sqle) { //... } finally { // Close the db connection } // Return the primary of the new row to the container return key; }
9
creating competitive advantage Copyright © 2003 creating competitive advantage Defining ejbPostCreate() Each ejbCreate() method must have a corresponding ejbPostCreate() method, even if it is empty. It is called after ejbCreate(…) is called and returned to home, and home has created EJB object. This is used to finish the creation work after the bean has created the EJB object. Usually, it is kept empty. public void ejbPostCreate(String ssn) { //... Rarely used }
10
creating competitive advantage Copyright © 2003 creating competitive advantage Defining ejbRemove() Deletes a row from the database. Must obtain the primary key from the EJB object (through the context). public void ejbRemove(String ssn) { try { String key = (String) ctx.getPrimaryKey(); // Get the connection to the DB, and... stmt = conn.prepareStatement(“DELETE FROM STUDENT WHERE studentid = ?”); stmt.setString(1, key); stmt.executeUpdate(); } catch SQLException sqle) { throw new EJBException(“unable to delete”); // Catch any other exceptions } finally { // Close the DB connection } }
11
creating competitive advantage Copyright © 2003 creating competitive advantage Defining ejbLoad() Used to refresh the values of the bean. The SELECT should return back exactly one raw. public void ejbLoad() { try { String key = (String) ctx.getPrimaryKey(); // Get the connection to the DB, and... stmt = conn.prepareStatement(“SELECE * FROM STUDENT WHERE STUDENTID = ?”); stmt.setString(1, key); ResultSet rs = stmt.executeQuery(); if(rs.next()) { name = rs.getString(1).trim(); // Refreshing bean field address = rs.getString(2).trim(); // Refreshing again } else { throw new NoSuchEntityException(“Row not found: ” + key); } } catch SQLException sqle) { throw new EJBException(“unable to delete”); } finally { // Close the DB connection }...
12
creating competitive advantage Copyright © 2003 creating competitive advantage Defining ejbStore() Used to store the values of the bean in the persistent media. Should again update only one row. public void ejbStore() { try { // Get the connection to the DB, and... stmt = conn.prepareStatement(“UPDATE STUDENT SET name = ?, address =? wherestudentid = ?”); stmt.setString(1, name); stmt.setString(1, address); stmt.setString(1, key); stmt.executeUpdate(); } catch SQLException sqle) { throw new EJBException(“unable to delete”); } finally { // Close the DB connection }
13
creating competitive advantage Copyright © 2003 creating competitive advantage Finder Methods The ejbFindByPrimaryKey method must be implemented. A finder method name must start with the prefix ejbFind. The access control modifier must be public. The method modifier cannot be final or static. The arguments and return type must be legal types for the Java RMI API. The return type must be the primary key or a collection of primary keys.
14
creating competitive advantage Copyright © 2003 creating competitive advantage Business Methods and Home Methods Business Methods The logic in a business method applies to a single entity bean, an instance with a unique identity. Home Methods A home method contains the business logic that applies to all entity beans of a particular class. During a home method invocation, the instance has neither a unique identity nor a state that represents a business object. Consequently, a home method must not access the bean's persistence state (instance variables). In the home interface, the method name is arbitrary, provided that it does not begin with create or find. In the bean class, the matching method name begins with ejbHome.
15
creating competitive advantage Copyright © 2003 Container Managed Persistence
16
creating competitive advantage Copyright © 2003 creating competitive advantage Benefits of CMP Very little work for the bean provider Deployer maps bean fields to table columns Might provide better portability Might provide better database optimizations: Implicit use of cursors Optimized queries Customizable load/store policies Customizable pre-fetch policies
17
creating competitive advantage Copyright © 2003 creating competitive advantage Primary Key With CMP Primary key type must be a legal value type in RMI-IIOP Primary key can map to a single field or multiple fields (compound keys) in an entity bean class.
18
creating competitive advantage Copyright © 2003 creating competitive advantage Coding Differences
19
creating competitive advantage Copyright © 2003 creating competitive advantage Methods in the Bean The (public abstract) bean should implement The EntityBean interface Zero or more ejbCreate and ejbPostCreate methods The get and set access methods, defined as abstract, for the persistent and relationship fields Any select methods, defining them as abstract The home methods The business methods Must not implement The finder methods (they are in the home interface) The finalize method
20
creating competitive advantage Copyright © 2003 creating competitive advantage Example Bean public abstract class UserBean implements EntityBean { public abstract String getEmail(); public abstract void setEmail(String email); public abstract String getPassword(); public abstract void setPassword(String password); // Other methods }
21
creating competitive advantage Copyright © 2003 creating competitive advantage Sample Table CREATE TABLE TBL_USER ( email varchar (50) PRIMARY KEY, password varchar (50) NOT NULL )
22
creating competitive advantage Copyright © 2003 creating competitive advantage Bean Interface import javax.ejb.EJBLocalObject; public interface LocalUser extends EJBLocalObject { public String getEmail(); public String getPassword(); }
23
creating competitive advantage Copyright © 2003 creating competitive advantage Defining BMP in DD password email java.lang.String email
24
creating competitive advantage Copyright © 2003 creating competitive advantage Full Content user-mgmt-beans UserBean UserBean LocalUserHome LocalUser UserBean Container java.lang.String False 2.x UserBean no description password
25
creating competitive advantage Copyright © 2003 creating competitive advantage Full Content user-mgmt-beans... Shown in the previous slide False 2.x UserBean no description password no description email java.lang.String email
26
creating competitive advantage Copyright © 2003 creating competitive advantage Defining ejbCreate() Returning null still allows a subclass to override the method to provide BMP public abstract class UserBean implements EntityBean { public String ejbCreate(String email, String password) throws CreateException { setEmail(email); setPassword(password); return null; } public void ejbPostCreate(String email, String password) { } }
27
creating competitive advantage Copyright © 2003 creating competitive advantage Example Bean (Complete) public abstract class UserBean implements EntityBean { public String ejbCreate(String email, String password) throws CreateException { setEmail(email); setPassword(password); return null; } public void ejbPostCreate(String email, String password) { } public abstract String getEmail(); public abstract void setEmail(String email); public abstract String getPassword(); public abstract void setPassword(String password); public void setEntityContext(EntityContext context){ } public void unsetEntityContext(){ } public void ejbRemove(){ public void ejbLoad(){ } public void ejbStore(){ } public void ejbPassivate(){ } public void ejbActivate(){ } }
28
creating competitive advantage Copyright © 2003 creating competitive advantage Finder Methods Finder methods are declared in the home interface No corresponding ejbFind() methods in the bean Container vendor should provide a tool that allows developer/deployer to describe how to create finder methods.
29
creating competitive advantage Copyright © 2003 Questions & Feedback?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.