What is an entity (Entity) bean
A persistent object usually it is as a "thing", or will exist for a long time
BankAccount Employee ORDER is not just data-status and behavior, just like regular objects
Entity bean in the database
One entity bean directly maps to a line of data in the database
IDLASTNMMGRDEPTSAlary ............. 42 "Joe" Smith055000.097 "Bob" Jones4240000.0102 "Mary" chen98763000.0 .............
EMPLOYEE
ID: INT First: String Last: String Salry: Double Managerld: int DPArtMentld: int
Entity Bean and Persistence
According to the specifications, the entity bean exists once it is created.
They are written directly to the database without being affected by any server, the CREATE method must provide enough information to save the bean to the database.
The difference between entity bean and session bean
Entity bean
Persistent objects; there is a primary key that is not affected by the server's downtime; can be searched can be shared and must be explicitly deleted
Session bean
Not lasting; the impact of the server is not available to the server, usually a user will be automatically recycled after a bean has passed a non-active period.
Aside: What is a primary key?
(1 or more) domains can uniquely distinguish a row of data / a bean-used to find and update data / objects in the life cycle of data / objects is constant - primary key is the constant data primary key is usually Generate instead of "active" data
SSN is clearly suitable for the primary key for the error selection as a primary key to be modified.
Aside: What is a relational database?
Database: A set of data sets can be retrieved and lookup - a relational database maintains the relationship between different line data.
Data is organized into a table-foreign key (Foreign Key) to map relationships, Structured Query Language (SQL) is used to operate database
Read (Insert, Update), Delete (DELETE)
Synchronous access control
Container Management Synchronous Access - Thread Get unique access to beans, that is, each method of Bean is synchronized
Warning: When you die!
Bean itself is also a customer!
Client 1 has the unique lock of Bean 1 CLIENT 2 with unique lock bean 1 to bean 2 must wait for the end of the Client 2 Bean 2 must wait for the end of the Client 1 without any part!
Entity bean has a primary key
The primary key is used to distinguish bean - similar to the concept of database main keys
Primary key: Reflection of the "key" state of the bean class, you must provide Equals (...) and havehcode () methods
Primary key may consist of multiple domains
...
Employee EMPLOYEEEEEEEEEE
= Employeehome.FindByPrimaryKey (New Employeekey (42));
...
CREATE method
Home Defines one or more "create" methods - after creation, beans must be written immediately to the database.
EmployeeHome Method
Employee Create (int Arg1, String Arg2, String Arg3)
Throws CreateException, RemoteExceptionEmployeebean Method
Public void ejbcreate (int Argid, string first, string last) throws createException, RemoteException {
ID = argid;
SetFirst (first);
SetLast (Last);
SetManagerID (0);
SetDepartmentID (0);
}
Post-crete
There must be an ejbpostcreate for each ejbcreate method - with the same parameters
The container will:
Call the EJBCREATE (...) method, it will return a primary key of the created entity to create a EJB object reference to the primary key to call the ejbpostcreate (...) method to notify the instance to fully initialize it itself
Public void ejbpostcreate (int Argid, String First, String Last)
THROWS CREATEXCEPTION, RemoteException {
}
Finder method
The Bean Home interface defines the "EJBFIND" method --- provides an implementation through VAJ's persistence lay
Methods will return: --- Remote interface of a single result, multiple results Enumeration
Must Be ProvidEmployee FindbyPrimaryKey (EmployeeKey Key)
THROWS RemoteException, FINDEREXCEPTION; Employee FindbyId (INT)
Throws RemoteException, FINDEREXCEPTION;
ENUMERATION FINDBYLASTLIKE (STRING PATTERN)
Throws RemoteException, FINDEREXCEPTION;
ENUMERATION FINDBYSAlaryRange (Double Low, Double High)
Throws RemoteException, FINDEREXCEPTION;
life cycle
Container-managed personistence
Container Management Bean lifecycle - When created, loaded, stored, and deleted containers to provide storage mechanism - how to create, load, store, and remove mappings between beans between beans and databases
Visualage and CMP
VisuaLage provides tools:
Creating a database mode from an entity bean from an entity bean? MEET IN THE MIDDLE: Using existing beans and patterns
Build an entity bean
Container management domain ...
You can define the domain when Bean is created.
The middle screen can switch the bean class and domain --- Use this feature to define the domain of additional container management
Schema and Mapp (MAP)
Schema
Description Database Mode converts raw data into a Java type to convert Java types into raw data
Map
Define how to define how to construct a data line from an object
Generate database mode
Add a mode and mapping - "EJB> Add> Schema and map from ejb group"
Export Mode Database - Open Schema Browser
Aside: JDBC
JDBC driver
Describe the interface between Java and a database as a Java class, is generally provided by the database manufacturer.
JDBC URL
Description Database location (how to find) partial format is defined by various manufacturers
Generate library table
Most DBA mocking database tables automatically - there is a reason: efficient library table design requires carefully considering the generated library table for testing
CMP provides shielding between your bean and storage mechanism - uses automatic generation library tables when testing, hard work is considered
"FinderHelper" class
Define "where" statement for each Finder method
"?" Is replaced by parameter value (in order) WebSphere unique
Employee Findbyid (Int ID) throws ...
ENUMERATION FINDALL () ...
ENUMERATION FINDBYLASTLIKE (STRING PATTERN) ...
Enumeration FindbysalaryRange (double low, double high) ..public interface EmployeebeanfinderHelper {
Public Static Final String FindByidwhereClause = "ID =?";
Public static final string findAllwhereclause = "1 = 1";
Public Static Final String FindbyLastLikewhereClause
= "Last Like?";
Public Static Final String FindBysalaryRangewhereClause
= "SALARY>? And SALARY "
}
Aside: Setting DB2
VAJ's workspace must be able to access DB2 drivers - use "Window> Options ..."
Create a database
Use the DB2 console to build a database
Refer to the instructions in SmartGuide VisuaLage to automatically generate library tables
This chapter tells the content
Entity Bean is a lasting object (relative) Entity bean directly reacts into the database - they can be able to find the attribute of the Entity Bean to be labeled as container-management or As part of its primary link in "Finder Helper", you can specify "where" statement