190 likes | 334 Views
EJB Entity Beans. “ Modeling your data ”. Model 2 with J2EE EJB ’ s. Model Two Architecture. EJB Container. Web Container. View. Control. Model. Web Server. Servlet. Entity EJB. Entity EJB. HTTP Request. Session EJB. Session Bean. Java Bean. Java Bean. <<forward>>. <<creates>> .
E N D
EJB Entity Beans “Modeling your data”
Model 2 with J2EE EJB’s Model Two Architecture EJB Container Web Container View Control Model WebServer Servlet Entity EJB Entity EJB HTTP Request Session EJB Session Bean JavaBean JavaBean <<forward>> <<creates>> JavaBean HTTP Response JSP page
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.
Order Item Customer Relationships View * 1 * 1
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;) }
Order Order Item Item Object Oriented View Customer Order Item
Object Oriented View public class Customer implements Serializable { private String name; … private ArrayList<Order> orders; public Customer() { } public getName() { return this.name; } public setName(String name) { this.name = name; } public getOrders() { return this.orders; } public setOrders(ArrayList<Order> orders) { this.orders = orders; } } } private ArrayList<Order> orders; public getOrders() { return this.orders; } public setOrders(ArrayList<Order> orders) { this.orders = orders; }
Object Oriented View public class Order implements Serializable { private long quantity; … private Item item; public Order() { } public getQuantity() { return this.quantity; } public setQuantity (long quantity) { this. quantity= quantity; } public getItem() { return this.item; } public setItem(Item orders) { this.item = item; } } } private Item item; public getItem() { return this.item; } public setItem(Item orders) { this.item = item; }
Use Getter/Setters to Data public class GetChildData { public double totalOrders (Customer customer) { double total = 0; ArrayList<Order> orders = customer.getOrders(); for (Iterator<Order> iterator = orders.iterator(); iterator.hasNext();) { Order order = (Order) iterator .next(); double price = order.getItem().getPrice(); double extendedPrice = order.getQuantity() * price; total += extendedPrice; } return total; } } ArrayList<Order> orders = customer.getOrders(); double price = order.getItem().getPrice();
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; }
Examples: SELECT DISTINCT jFROM Job AS j 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 } 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
From Examples: SELECT jFROM 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
From Examples: SELECT sFROM Job AS j, INNER JOIN j.Skills AS s SQL equivalent: SELECT s.*FROM Job AS j INNER JOIN JobSkill AS sON j.FK_skillID = s.skillID 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
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 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