EJB Entity Beans “Modeling your data” Model 2 with J2EE EJB’s Model Two Architecture View Control HTTP Request Servlet Web Server Java Bean Model Session Bean EJB Entity EJB Java Bean <<forward>> HTTP Response JSP page Web Container EJB Container <<creates>> Java Bean Advantages 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 Access Entity EJB ONLY!!! from a Session EJB Entity EJBs should represent developers logical view of data NOT the physical model in the database. Relationships View Order * 1 Customer * 1 Item Use JDBC To Access Data Class.forName("org.gjt.mm.mysql.Driver"); // load jdbc driver classes String url = "jdbc:mysql://localhost/northwind"; connection = DriverManager.getConnection(url, "USERNAME", ”PASSWORD"); statement = connection.createStatement(); String sql = "SELECT C.*, O.*, I.* " + "FROM Customer C INNER JOIN Orders O INNER JOIN Items I " + "ON C.customerId = O.FK_CustomerId " + "ON O.FK_ItemId = I.ItemId"; ResultSet resultSet = statement.executeQuery("SELECT * from customers"); while( resultSet.next() ) { String companyName = resultSet.getString( "CompanyName" ); System.out.printlin("\nCompanyName = " + companyName;) } Object Oriented View Customer Order Item Order Item Order Item Object Oriented View public class Customer implements Serializable { private String name; … private ArrayList<Order> private ArrayList<Order> orders; orders; public Customer() { } public getName() { return this.name; } public setName(String name) { this.name = name; } publicgetOrders() getOrders(){ { public this.orders; returnreturn this.orders; }} public setOrders(ArrayList<Order> orders) { public setOrders(ArrayList<Order> orders) { this.orders = orders; this.orders = orders; }} } Object Oriented View public class Order implements Serializable { private long quantity; … privateItem Itemitem; item; private public Order() { } public getQuantity() { return this.quantity; } public setQuantity (long quantity) { this. quantity= quantity; } publicgetItem() getItem(){ { public return this.item; return this.item; }} public setItem(Item orders) { public setItem(Item orders) { this.item = item; this.item = item; }} } Use Getter/Setters to Data public class GetChildData { public double totalOrders (Customer customer) { double total = 0; ArrayList<Order> = customer.getOrders(); ArrayList<Order> orders =orders customer.getOrders(); for (Iterator<Order> iterator = orders.iterator(); iterator.hasNext();) { Order order = (Order) iterator .next(); double price == order.getItem().getPrice(); order.getItem().getPrice(); double price double extendedPrice = order.getQuantity() * price; total += extendedPrice; } return total; } } Insert object into Datastore public void addBranch() { // create a branch java bean Branch branch = new Branch(); branch.setName(“Bank of Nauvoo”); branch.setPhone(“203-356-1426”); // inserts a branch into the database entityManager.persist(branch); } Update Object in Datastore public void renameBranch(String branchid, String newName) { // get branch by its Primary Key from datastore Branch branch = (Branch) entityManager.find(Branch.class, branchid); // update the branch branch.setBranchname(newName); entityManager.merge(branch); } Delete Object from Datastore public void deleteBranch(String branchid) { // get branch by its Primary Key from datastore Branch branch = (Branch) entityManager.find(Branch.class, branchid); // Delete the branch entityManager.remove(branch); } Query Object/s from Datastore public Collection<Branch> getBranches(String name){ // Define query prepared statement String ejbql = "SELECT b FROM Branch b WHERE b.branchname LIKE :branchname”; // Create query object Query query = entityManager.createQuery(ejbql); // Substitute value to search for in prepared statement query.setParameter("branchname", searchValue); // Execute query to get list of branches List<Branch>branches = query.getResultList(); 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 JobSkill AS s ON j.FK_skillID = s.skillID 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.customerId = O.FK_CustomerId ON O.FK_ItemId = I.ItemId