You are on page 1of 12

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA...

Page 1 of 12

Part 3: Adding and Exposing a New Method to the UI


In this section you add a new method to the entity bean using the EJB 3.0 annotation technique. You then expose the new method to
the facade and use it as a data control for pages in a bounded task flow.
Additionally you use the task flow as a region within the mainHR page.

Step 1: Add a New Method to the Entity and Expose it


In the EJB, you add a named query using the @NamedQueries annotation syntax.
1. Close all open tabs and expand the Model project.

2. Double click the Employees.java class to open it in the Source editor.

What makes these objects different from other Java files are the annotations that identify them as EJB entities.
A key feature of EJB 3.0 and JPA is the ability to create entities that contain object-relational mappings by using metadata
annotations rather than deployment descriptors as in earlier versions.
3. In the Source editor, open the Entity node to visualize the @NamedQuery statement within the @NamedQueries annotation.

Named queries enable you to define queries at design time and then use them at run time.
Creating the FacadeBean created one NamedQuery metadata statement in the Employee entity. This query retrieves all
rows from the Employees table:
@NamedQueries({ @NamedQuery(name = "Employees.findAll", query = "select o from Employees o") })
4. Add a comma after the closing parenthesis of the NamedQuery statement and hit Enter to add a new line.
5. Add a query to the class that retrieves data about employees having a salary greater than a parameter value. Add the following

statement:

@NamedQuery(name = "Employees.findBySal", query = "select o from Employees o where o.salary


> :p_sal")

So that the code looks like the following:


@NamedQueries( {
@NamedQuery(name = "Employees.findAll", query = "select o from Employees o") ,
@NamedQuery(name = "Employees.findBySal", query = "select o from Employees o where o.salary > :p_sal")
})

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA... Page 2 of 12

If required, use the ALT + Enter keystroke combination to import the javax.persistence.NamedQueries library.

Any symbol in Java code beginning with @ is known as an annotation.


The use of annotations allows you to add metadata to your objects. Examples of annotations follow:
Annotation

Description

@Entity

Identifies the file as an EJB 3.0 entity

@NamedQuery

A query that can be used at run time to retrieve data

@Table

Specifies the primary table for the entity

@Id

Can define which property is the identifier for the entity

@Column

Specifies a mapped column for a persistent property or field

@ManyToOne

Specifies a type of foreign key relationship between tables

@JoinColumn

Specifies the join column and referenced column for a foreign key relationship

6. Click the Make icon to compile your project and check that no errors are returned.

7. In the Application Navigator, right-click HRFacadeBean.java and select Edit Session Facade from context.

8. In the Specify Session Facade Options, check the new query you just added getEmployeesFindBySal().

Click OK. Click the Save All

icon to save your work.

9. In the Application Navigator, right-click HRFacadeBean.java and select Create Data Control from context.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA... Page 3 of 12

10. In the Choose EJB Interface, leave the default (local) selection and click OK.

11. In the Data Control Registry, expand the HRFacadeLocal node. Open the new method you just created and click the

Employees node. Notice the existing attributes corresponding to the EJB JPA definition.

12. Your new method is now ready to be used as a data control in your pages.
13. Click the Save All

icon to save your work.

Step 2: Build a Bounded Task Flow with Two JSF Pages


ADF task flows provide a modular approach for defining control flow in a Fusion web application.
1. Close the Model project. Right-click the ViewController project and select New from context.

2. In the New Gallery, select Web Tier > JSF/Facelets as the Category and ADF Task Flow as the item. Click OK.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA... Page 4 of 12

3. In the Create Task Flow dialog, enter findBySalFlow.xml. Create as a bounded task flow with page fragments. Click OK.

There are two types of ADF task flow:


Unbounded task flow: A set of activities, control flow rules, and managed beans that interact to allow a user to complete a
task. The unbounded task flow consists of all activities and control flows in an application, that are not included within a
bounded task flow.
Bounded task flow: A specialized form of task flow that, in contrast to the unbounded task flow, has a single entry point (an
entry point is a view activity that can be directly requested by a browser) and zero or more exit points. It contains its own set
of private control flow rules, activities, and managed beans. A bounded task flow allows reuse, parameters, transaction
management, reentry, and can render within an ADF region in a JSF page.
Create with Page Fragments: Clear this checkbox if you want the view activities that you add to the task flow to reference
JSF pages that render in the main browser window as the root page. Leave the Create with Page Fragments checkbox
checked if you want the view activities that you add to the task flow to reference page fragments files (.jsff) that the task flow
displays in an ADF region at runtime.
4. Drag and drop a View component onto the task flow.

5. Drag and drop a second View component onto the task flow.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA... Page 5 of 12

Notice the circle around view1. It represents the entry point within the task flow.
A bounded task flow has a single point of entry, a default activity that executes before all other activities in the task flow.
6. Select the Control Flow Case component and clicking in view1, drop it onto view2. Rename the flow find.

7. Repeat the operation to create a second Control Flow Case from view2 to view1 and rename it back.

8. Double-click view1 to create the page. In the Create ADF Page Fragment the page should be a jsff file type.

Click OK.

A page fragment is a JSF document that renders as content in another JSF page.
Page fragments are typically used in bounded task flows that can be added to a JSF page as a region
9. Open the Data Controls pane, and drop the getEmployeesFindBySal() method onto the page as Parameter --> ADF

Parameter Form.

10. In the Edit Form Fields enter Salary > in the Display Label field and click OK to accept other defaults. Notice the parameter

p_sal.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA... Page 6 of 12

11. Select the getEmployeesFindBySal button. Then in the Property Inspector, set the Action property to find from the list and the

Text property to Find.

12. From the Component Palette, in the Operations library, select Set Action Listener and drop it onto the Find button.

You use the Set Action Listener to hold the parameter value typed in View1 at a pageFlowScope level.
13. In the Insert Set Action Listener dialog, click the down arrow next to the From field and select Expression Builder.

14. In the Expression Builder, expand ADF Bindings > bindings and select getEmployessFindBySal_p_sal.

Click OK.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA... Page 7 of 12

15. Back in the Insert Set Action Listener dialog, in the To field, enter #{pageFlowScope.sal}.

Click OK.
16. The Structure pane and the property inspector should now look like the following:

17. Click the findBySalFlow.xml tab to reopen the task flow diagram. In the task flow diagram, double click view2 to create the

page.

18. In the Create ADF Page fragment, accept the default values.

Click OK.
19. In the Data Controls pane, expand the getEmployeesFindBySal() method then drag and drop Employees onto the page. Select

Table --> ADF Read-only Table from context.

20. In the Edit Table Columns dialog, select Single Row , Enable Sorting and Enable Filtering. Delete all columns after salary

and also delete hiredate.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA... Page 8 of 12

Click OK.
21. In the Edit Action Binding dialog, in the Value field enter #{pageFlowScope.sal}.

Click OK.

The pageFlowScope.sal value indicates which salary parameter value to use.


The value is passed to the p_sal parameter before executing the query.
22. From the Component Palette, select the Button component in the ADF Faces > General Controls library and drop it above the

af:table node in the Structure pane.

23. In the Property Inspector, enter Back as Text and select back for the action.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, JPA... Page 9 of 12

24. Click the Save All

icon to save your work.

Step 3: Use the Task Flow as a Region in the mainHR page


You can render a bounded task flow in a JSF page or page fragment (.jsff) by using an ADF region.
1. Click the mainHR.jsf node in the Application Navigator to reopen the mainHR.jsf page.
2. Right click the Graph tab and select Insert After Show Detail Item > Show Detail Item.

3. In the Property Inspector, change the Text field to Search.

4. In the Application Navigator, expand the Web Content > WEB-INF nodes and drag and drop the findBySalFlow.xml node

within the Search pane.

5. Select Create > Region from context.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, J... Page 10 of 12

6. The page region should look like the following:

A primary reason for executing a bounded task flow as an ADF region is reuse.
You can isolate specific pieces of application functionality in a bounded task flow and an ADF region in order to reuse it
throughout the application.
You can extract, configure, and package application functionality within a bounded task flow so that it can be added to other
pages using an ADF region. ADF regions can be reused wherever needed, which means they are not dependent on a
parent page.

7. Click the Save All

icon to save your work.

Step 4: Run the mainHr Page


1. Right click within the page and select Run from context.

2. In your Browser window, click the Search tab.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, J... Page 11 of 12

3. Enter a value in the Salary field (for example 8000) and click the Find button.

4. A list of the corresponding employees is returned. Scroll right to verify that no salaries =< 8000 are returned.

5. Click the Back button.

6. Enter a new value in the Salary field (for example 10000) and click the Find button.

7. A more restricted list is now displayed.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

Oracle JDeveloper 11g Release 2 Tutorials - Building a Web Application Using EJB, J... Page 12 of 12

8. Close the browser window.

Previous 1 2 3 4 Next

Copyright 2011, Oracle and/or its affiliates. All rights reserved.

http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_51/jdtut_11r2_51_3.html

08/03/2012

You might also like