Monday Feb 21, 2011

EJB Persist On Master Child Relationship

Let us take scenario where in users wants to persist master child relationship. Here will have two tables dept, emp (using Scott Schema) which are having master child relation.

Model Diagram:

Master Child -Ejb master child persist

Here in the above model diagram, Dept is the Master table and Emp is child table and Dept is related to emp by one to n relationship.

Lets assume we need to make new entries in emp table using EJB persist method. Create a Emp form manually dropping the fields, where deptno will be dropped as Single Selection -> ADF Select One Choice (which is a foreign key in emp table) from deptFindAll DC. Make sure to bind all field variables in backing bean.


Employee Form:

Employee Form - Ejb master child persist

Once the Emp form created, If the persistEmp() method is used to commit the record this will persist all the Emp fields into emp table except deptno, because the deptno will be passed as a Object reference in persistEmp method  (Its foreign key reference). So directly deptno can't be passed to the persistEmp method instead deptno should be explicitly set to the emp object, then the persist will save the deptno to the emp table.

Below solution is one way of work around to achieve this scenario -

  • Create a method in sessionBean for adding emp records and expose this method in DataControl.
     For Ex: Here in the below code 'em" is a EntityManager.
           private EntityManager em - will be member variable in sessionEJBBean

public void addEmpRecord(String ename, String job, BigDecimal deptno) {
        Emp emp = new Emp();
        emp.setEname(ename);
        emp.setJob(job);

        //setting the deptno explicitly
        Dept dept = new Dept();
        dept.setDeptno(deptno);

       //passing the dept object
       emp.setDept(dept);

       //persist the emp object data to Emp table
       em.persist(emp);
}

  • From DataControl palette Drop addEmpRecord as Method ADF button, In Edit action binding window enter the parameter values which are binded in backing bean.
     For Ex:
     If the name deptno textfield is binded with "deptno" variable in backing bean, then El Expression Builder pass value as "#{backingbean.deptno.value}"

Binding:
Employee Form Biinding



[Read More]
About

Tips and Tricks from Oracle's JDeveloper & ADF QA

Search

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