EJB Entity Beans Modeling your data Model 2
EJB Entity Beans “Modeling your data”
Model 2 with J 2 EE EJB’s Model Two Architecture View Control HTTP Request Web Server Servlet Java Bean Model Session Bean EJB Entity EJB Java Bean <<forward>> HTTP Response JSP page Web Container EJB Container <<creates>> Java Bean
Advantages n n n Automatic persistence management Share data in memory Automatic synchronization to the database Simpler to use than JDBC or ADO Greater scalability, portability, maintainability, reliability, code re-use Automatic transaction processing
Guidelines for use n Access Entity EJB ONLY!!! from a Session EJB n Entity EJBs should represent developers logical view of data NOT the physical model in the database.
Relationships View * * 1 1 Item
Use JDBC To Access Data Class. for. Name("org. gjt. mm. mysql. Driver"); // load jdbc driver classes String url = "jdbc: mysql: //localhost/northwind"; connection = Driver. Manager. get. Connection(url, "USERNAME", ”PASSWORD"); statement = connection. create. Statement(); String sql = "SELECT C. *, O. *, I. * " + "FROM Customer C INNER JOIN Orders O INNER JOIN Items I " + "ON C. customer. Id = O. FK_Customer. Id " + "ON O. FK_Item. Id = I. Item. Id"; Result. Set result. Set = statement. execute. Query("SELECT * from customers"); while( result. Set. next() ) { String company. Name = result. Set. get. String( "Company. Name" ); System. out. printlin("n. Company. Name = " + company. Name; ) }
Object Oriented View Customer Order Item
Object Oriented View public class Customer implements Serializable { private String name; … private. Array. List<Order>orders; private public Customer() { } } public get. Name() { return this. name; } public set. Name(String name) { this. name = name; } publicget. Orders(){ { public this. orders; return this. orders; }} public set. Orders(Array. List<Order> orders) { this. orders = orders; }}
Object Oriented View public class Order implements Serializable { private long quantity; … private. Itemitem; private public Order() { } } public get. Quantity() { return this. quantity; } public set. Quantity (long quantity) { this. quantity= quantity; } publicget. Item(){ { public return this. item; }} public set. Item(Item orders) { this. item = item; }}
Use Getter/Setters to Data public class Get. Child. Data { public double total. Orders (Customer customer) { double total = 0; Array. List<Order> = customer. get. Orders(); Array. List<Order> orders =orders customer. get. Orders(); for (Iterator<Order> iterator = orders. iterator(); iterator. has. Next(); ) { Order order = (Order) iterator. next(); double price ==order. get. Item(). get. Price(); double price double extended. Price = order. get. Quantity() * price; total += extended. Price; } return total; } }
Insert object into Datastore public void add. Branch() { // create a branch java bean Branch branch = new Branch(); branch. set. Name(“Bank of Nauvoo”); branch. set. Phone(“ 203 -356 -1426”); // inserts a branch into the database entity. Manager. persist(branch); }
Update Object in Datastore public void rename. Branch(String branchid, String new. Name) { // get branch by its Primary Key from datastore Branch branch = (Branch) entity. Manager. find(Branch. class, branchid); // update the branch. set. Branchname(new. Name); entity. Manager. merge(branch); }
Delete Object from Datastore public void delete. Branch(String branchid) { // get branch by its Primary Key from datastore Branch branch = (Branch) entity. Manager. find(Branch. class, branchid); // Delete the branch entity. Manager. remove(branch); }
Query Object/s from Datastore public Collection<Branch> get. Branches(String name){ // Define query prepared statement String ejbql = "SELECT b FROM Branch b WHERE b. branchname LIKE : branchname ”; // Create query object Query query = entity. Manager. create. Query(ejbql); // Substitute value to search for in prepared statement query. set. Parameter("branchname", search. Value); // Execute query to get list of branches List<Branch>branches = query. get. Result. List(); return branches; }
Entity Bean query language (ejb-ql) select_clause from_clause [where_clause] select_clause : : = SELECT [DISTINCT] {identification_variable | single_valued_path_expression } single_valued_path_expression : : = {single_valued_navigation | identification_variable } . cmp_field | single_valued_navigation } Examples: SELECT DISTINCT j FROM Job AS j SELECT DISTINCT j. * FROM JOB as j
Entity Bean query language (ejb-ql) select_clause from_clause [where_clause] from_clause : : = {abstract_schema_name | collection_member_declaration} [AS] identifictation_variable collection_member_declaration : : = INNER JOIN (collection_valued_path_expression) collection_valued_path_expression : : = idetification_variable. [single_valued_cmr_field. ] collection_valued_cmr_field
Entity Bean query language (ejb-ql) select_clause from_clause [where_clause] from_clause : : = {abstract_schema_name | collection_member_declaration} [AS] identifictation_variable From Examples: SELECT j FROM Job AS j SQL equivalent: SELECT j. * FROM Job AS j
Entity Bean query language (ejb-ql) select_clause from_clause [where_clause] from_clause : : = {abstract_schema_name | collection_member_declaration} [AS] identifictation_variable collection_member_declaration : : = INNER JOIN (collection_valued_path_expression) collection_valued_path_expression : : = idetification_variable. [single_valued_cmr_field. ] collection_valued_cmr_field From Examples: SELECT s FROM Job AS j, INNER JOIN j. Skills AS s SQL equivalent: SELECT s. * FROM Job AS j INNER JOIN Job. Skill AS s ON j. FK_skill. ID = s. skill. ID
Entity Bean query language (ejb-ql) select_clause from_clause [where_clause] Where Examples: SELECT OBJECT(o) FROM Customer AS c, INNER JOIN c. orders AS o, INNER JOIN o. items AS I SQL equivalent: SELECT C. *, O. *, I. * FROM Customer C INNER JOIN Orders O INNER JOIN Items I ON C. customer. Id = O. FK_Customer. Id ON O. FK_Item. Id = I. Item. Id
- Slides: 19