Presentation is loading. Please wait.

Presentation is loading. Please wait.

EJB Entity Beans “Modeling your data”.

Similar presentations


Presentation on theme: "EJB Entity Beans “Modeling your data”."— Presentation transcript:

1 EJB Entity Beans “Modeling your data”

2 Model Two Architecture
Model 2 with J2EE EJB’s Model Two Architecture Web Container EJB Container View Control Model Web Server HTTP Request Servlet Entity EJB Entity EJB Session EJB Session Bean Java Bean Java Bean Java Bean <<creates>> <<forward>> The Model 2 architecture breaks up the Model-View-Controller pattern such that each part of the model can run in a different container on a different server. What is the purpose of breaking the pattern in this way? Adaptability Flexibility Scalability Thus far we have implemented the View and the Control. To implement the Model we will need to develop a seaparate tier of code to handle all of the dataaccess. One way to do this is to create your own custom code (A Data Access Object) Create your own custom code to access the database Access the database or flat file Manage persistence of data System dependent Database/file dependent 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 Advantages Remember that one of the advantages of Session Beans is that the Application Server stores the EJB in memory in the “bean pool”. This allows the bean to remain in memory and shared over large number of clients. Entity Java Beans work the same way but for data objects. Provides access and synchronization to the database Much simpler programming interface for client to access that data Greater portability, maintainability, reliability, code re-use Automatically handles transaction processing for you

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. Guidelines for use Probably not best for doing simple data access like performing simple queries. Entity Java Beans are more complex to create than the data access code to perform a query. Coarse-Grained Business Objects vs. Finely-Grained Business Objects There is some extra overhead associated with using entity beans (locating the object and accessing it) and there over use can effect performance. Entity beans should seldom be used to represent developers logical view of data (entities) and not the physical view of how the data is stored in the database. For example, A Customer Order and all the information contained in the customer order (order information, and line items purchased on order and totals).

5 Relationships View Order * 1 Item * 1 Customer

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 Object Oriented View Customer Order Item

8 private ArrayList<Order> orders;
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(“ ”); // 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 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 Examples: 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 Entity Bean query language (ejb-ql)
select_clause from_clause [where_clause] from_clause ::= {abstract_schema_name | collection_member_declaration} [AS] identifictation_variable SELECT j FROM Job AS j From Examples: SELECT j.* FROM Job AS j SQL equivalent:

18 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 SELECT s FROM Job AS j, INNER JOIN j.Skills AS s From Examples: SELECT s.* FROM Job AS j INNER JOIN JobSkill AS s ON j.FK_skillID = s.skillID SQL equivalent:

19 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 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 SQL equivalent:


Download ppt "EJB Entity Beans “Modeling your data”."

Similar presentations


Ads by Google