EJB Persist On Master Child Relationship
- by deepak.siddappa(at)oracle.com
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 sessionEJBBeanpublic 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: