1 / 19

EJB Entity Beans

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

carlton
Download Presentation

EJB Entity Beans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EJB Entity Beans “Modeling your data”

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

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

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

  5. Order Item Customer Relationships View * 1 * 1

  6. 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;) }

  7. Order Order Item Item Object Oriented View Customer Order Item

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

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

  10. 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();

  11. 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); }

  12. 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); }

  13. 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); }

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

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

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

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

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

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

More Related