Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.
Advertisements

12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
5 Copyright © 2005, Oracle. All rights reserved. Accessing the Database with Servlets.
Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009.
Java II--Copyright © Tom Hunter. Chapter 18 JDBC.
EJB Entity Beans. Entity Beans Data versus logic Used to represent an instance rather than a collection of data (depending on underlying storage) Represents.
1 JDBC Java Database Connectivity. 2 c.pdf
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 Pointbase Developer Manual.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
Java Database Connectivity (JDBC) java.sql package to query and update the database. JDBC is an API that allows java to communicate with a database server.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Database Programming in Java Corresponds with Chapter 32, 33.
EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.
Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container.
Enterprise Java Beans - (EJB)
Writing Enterprise Applications with J2EE (Second lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Entity Beans BMP Celsina Bignoli
Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.
Lecture 8 Advanced Topics in Enterprise JavaBeans.
Objektorienteret netværkskommuniation(ITONK1) Persistence.
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
Container-Managed Persistence (CMP) Entity Beans Lesson 3A / Slide 1 of 42J2EE Server Components Objectives In this lesson, you will learn to: Identify.
Distributed Systems 1 Master of Information System Management Distributed Systems Persistence.
The Triad of Beans I Oleh: Dini Addiati ( ) Fahrurrozi Rahman ( Y) Irfan Hilmy ( ) Salman Azis A ( ) Aziiz Surahman.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
Enterprise JavaBeans. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports application development.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Enterprise Java Bean Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Part 2: EJB Persistency Jianguo Lu. 2 Object Persistency A persistent object is one that can automatically store and retrieve itself in permanent storage.
Enterprise JavaBeans Understanding EJB Components Version 0.1 Kamal Wickramanayake
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Chapter 8 Databases.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
© jGuru.com Enterprise JavaBeans Fundamentals.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
Enterprise Java v041109Container Managed Relationships1 Container Managed Relationships (CMR) Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
EJB Overview: Constraint Types and Enforcement in JBoss Elissa Newman Fluid Meeting 6/3/04.
Entity Beans & Persistence Chris Alexander CS 486 Spring 2001.
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
JDBC™ Advanced Concepts
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Enterprise Java Beans N.V.RAJASEKHAR REDDY. Definition of EJB EJBs are the components that are the set of classes and interfaces deployed within a container.
Copyright © 2002 ProsoftTraining. All rights reserved. Enterprise JavaBeans.
Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
D Copyright © 2004, Oracle. All rights reserved. BMP Entity EJBs J2EE Connector Architecture.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
1 JDBC – Java Database Connectivity CS , Spring 2010.
JDBC I IS Why do we have databases?
14 Copyright © 2004, Oracle. All rights reserved. Achieving State Management in the Business Tier.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JAVA & J2EE UNIT – 8 ENTERPRISE JAVA BEAN - EJB 28-Jun-16 Prepared by: SANTHIYA.M & GANGA V. C. Department: Computer Science & Engineering Course code:
JDBC III IS Outline  Scrollable ResultSets  Updatable ResultSets  Prepared statements  Stored procedures.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Entity Bean Chuyên đề Lập trình Java & J2EE Chương 15
JDBC – Java Database Connectivity
HW#4 Making Simple BBS Using JDBC
JDBC.
“Controlling your application” (the business logic)
Structure of Enterprise Java Beans
Objectives In this lesson, you will learn to:
Understanding and Designing with EJB
Entity Beans B.Ramamurthy 2/17/2019 BR.
Knowledge Byte In this section, you will learn about:
Understanding and Designing with EJB
Presentation transcript:

creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated: 11-Sept-2003 Bean Managed Persistence Container Managed Persistence

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

creating competitive advantage Copyright © 2003 Bean Managed Persistence

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

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”); } }

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

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.

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; }

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 }

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 } }

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 }...

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 }

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.

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.

creating competitive advantage Copyright © 2003 Container Managed Persistence

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

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.

creating competitive advantage Copyright © 2003 creating competitive advantage Coding Differences

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

creating competitive advantage Copyright © 2003 creating competitive advantage Example Bean public abstract class UserBean implements EntityBean { public abstract String get (); public abstract void set (String ); public abstract String getPassword(); public abstract void setPassword(String password); // Other methods }

creating competitive advantage Copyright © 2003 creating competitive advantage Sample Table CREATE TABLE TBL_USER ( varchar (50) PRIMARY KEY, password varchar (50) NOT NULL )

creating competitive advantage Copyright © 2003 creating competitive advantage Bean Interface import javax.ejb.EJBLocalObject; public interface LocalUser extends EJBLocalObject { public String get (); public String getPassword(); }

creating competitive advantage Copyright © 2003 creating competitive advantage Defining BMP in DD password java.lang.String

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

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 java.lang.String

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 , String password) throws CreateException { set ( ); setPassword(password); return null; } public void ejbPostCreate(String , String password) { } }

creating competitive advantage Copyright © 2003 creating competitive advantage Example Bean (Complete) public abstract class UserBean implements EntityBean { public String ejbCreate(String , String password) throws CreateException { set ( ); setPassword(password); return null; } public void ejbPostCreate(String , String password) { } public abstract String get (); public abstract void set (String ); 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(){ } }

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.

creating competitive advantage Copyright © 2003 Questions & Feedback?