Using selectOneChoice with JPA relationships

On OTN some asked how to create a selectOneChoice item from an object that relates to another object when using JPA and ADF Faces.

Here is the quick how-to.
We'll use the default Employees and Departments table from the HR schema. Just create default JPA entities on them with the JDev wizard and then create a service facade and right click it to create a data control.

First you'll drag the Departments object from the queryDepartmentsFindAll DC onto a JSF page and drop as an ADF Form.
Now you want to show the name of the manager of each department in a drop down list.
To do this expand the Employees object underneath the departments and click the firstName.
bind.jpg

Drag it over to the JSF page and drop as a selectOneChoice component.
You'll be prompted to bind the list. You want to bind it to a list of employees - so you'll create a new List data source based on the queryEmployeesFindAll

You'll map the EmployeeID and you can choose to display the first name.
Like this:

bind.jpg

And that's it. Run the page and it will look like this:

bind.jpg

Comments:

Hi! In TP4, with this example, I was not able to update the manager. SelectOneChoice is populated as expected (with dpts manager selected as expected). But if I change the manager, only Id is changed in employee entity of manager while all other attributes remain the same. So, when I call merge(department) EJB Session method to update department with newly selected manager, nothing happens. So, I think that you example is good for selecting the Id of new manager, but the session facade method must "know" to fetch a employee entity using messed-up managers entity (new Id with all other attributes preserved from old manager) provided in department. This is very error prone, as this behavior is not consistent with EJB mapping philosophy. I think that the ADFm should enable native support for mapping the entire entity object (department-manager) instead to be limited to ADF BCs (and database table) attribute (id) mapping philosophy. Regards, Pavle

Posted by Pavle on July 19, 2008 at 09:35 PM PDT #

Pavle, Seems like a bug in TP4 - this works perfectly fine in 10.1.3.3

Posted by shay on July 25, 2008 at 01:33 AM PDT #

Hi All Your form is edit form , If this form is create new then selectOneChoise is not work , it disabled . Why ? thank for help

Posted by vha on December 01, 2008 at 09:34 AM PST #

Shay, I tried to use this mechanism to render a selectOneChoice field on my Toplink Essentials based project. But there was a bad effect if the "employees" attribute is null (because the DB allows it, in our model), the selectOneChoice simply dissapear. The work around for this issue is to use an accessor for the "firsName" attribute on the Departments entity in order to use it as a binding for the selectOneChoice. Hope this help somebody somewhere :) Thanks for this article.

Posted by Nelson on February 10, 2009 at 05:02 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

me
I'm a Director of Product Management for the Oracle Java Development Tools.
Follow me:
Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today