Wednesday, April 18, 2012

Ejb Commit 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:

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:
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();

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

       //passing the dept object

       //persist the emp object data to Emp table
  • 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}"


No comments:

Post a Comment