Hello everyone
in this post we will learn how we can insert data programmatically
for this example i am using jdeveloper 11.1.2.3.0 and Departments table of HR schema
Steps
1- Create a new application and connect it with oracle HR Schema
2-Create Department and Location view objects and make sure these objects are Entity base because only Entity base view objects are update able.
3-Create a .jsf page
4-Drag and drop two input texts and a button on .jsf page from component palette
5-Drag and drop LocationView1 on .jsf page as selectOneChoice
6-Dreate a bean and create bindings of input texts and button with bean
in the bean method you need two things
a- Iterators ID and
b- Row Impl Classes
so for Iterators id you need to do the following
we need access two iterators in bean
a) DepartmentView Iterator (to insert new row )
b)and LocationView Iterator (to get selected location id )
to access Iterator in bean we need iterator ID and in order to get Iterator ID we need to go
page bindings select the iterator and copy id from property Inspector
and secondly we need Row Impl to access getters and setters
//4-get current row of location view iterator
LocationsViewRowImpl locCRow = (LocationsViewRowImpl)locIter.getCurrentRow();
//6-create new row in department view object
DepartmentsViewRowImpl nrow = (DepartmentsViewRowImpl)vo.createRow();
So to generate RowImpl classes open view object and click on java tab and then click on Edit java option
here is the complete code that we use to insert new department
public class DepartmentInseration {
private String deptID;
private String deptName;
public DepartmentInseration() {
}
public String insertNewDepartn() {
// 1- get bindings
BindingContainer container=BindingContext.getCurrent().getCurrentBindingsEntry();
//2-get department view iterator
DCIteratorBinding deptIter = (DCIteratorBinding)container.get("DepartmentsView1Iterator");
//3- get location view iterator to get selected location id
DCIteratorBinding locIter = (DCIteratorBinding)container.get("LocationsView1Iterator");
//4-get current row of location view iterator
LocationsViewRowImpl locCRow = (LocationsViewRowImpl)locIter.getCurrentRow();
//5-get department view object from deptIter
ViewObject vo=deptIter.getViewObject();
//6-create new row in department view object
DepartmentsViewRowImpl nrow = (DepartmentsViewRowImpl)vo.createRow();
//7-set values
nrow.setDepartmentId(Integer.valueOf(this.getDeptID()));
nrow.setDepartmentName(this.getDeptName());
//8-get location id from loaction view current row
nrow.setLocationId(locCRow.getLocationId());
vo.insertRow(nrow);
deptIter.getDataControl().commitTransaction();
//to show popup
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Data saved...."));
return null;
}
public void setDeptID(String deptID) {
this.deptID = deptID;
}
public String getDeptID() {
return deptID;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptName() {
return deptName;
}
public void setLocatonID(String locatonID) {
this.locatonID = locatonID;
}
public String getLocatonID() {
return locatonID;
}
}
in this post we will learn how we can insert data programmatically
for this example i am using jdeveloper 11.1.2.3.0 and Departments table of HR schema
Steps
1- Create a new application and connect it with oracle HR Schema
2-Create Department and Location view objects and make sure these objects are Entity base because only Entity base view objects are update able.
3-Create a .jsf page
4-Drag and drop two input texts and a button on .jsf page from component palette
5-Drag and drop LocationView1 on .jsf page as selectOneChoice
6-Dreate a bean and create bindings of input texts and button with bean
in the bean method you need two things
a- Iterators ID and
b- Row Impl Classes
so for Iterators id you need to do the following
we need access two iterators in bean
a) DepartmentView Iterator (to insert new row )
b)and LocationView Iterator (to get selected location id )
to access Iterator in bean we need iterator ID and in order to get Iterator ID we need to go
page bindings select the iterator and copy id from property Inspector
and secondly we need Row Impl to access getters and setters
//4-get current row of location view iterator
LocationsViewRowImpl locCRow = (LocationsViewRowImpl)locIter.getCurrentRow();
//6-create new row in department view object
DepartmentsViewRowImpl nrow = (DepartmentsViewRowImpl)vo.createRow();
So to generate RowImpl classes open view object and click on java tab and then click on Edit java option
here is the complete code that we use to insert new department
public class DepartmentInseration {
private String deptID;
private String deptName;
public DepartmentInseration() {
}
public String insertNewDepartn() {
// 1- get bindings
BindingContainer container=BindingContext.getCurrent().getCurrentBindingsEntry();
//2-get department view iterator
DCIteratorBinding deptIter = (DCIteratorBinding)container.get("DepartmentsView1Iterator");
//3- get location view iterator to get selected location id
DCIteratorBinding locIter = (DCIteratorBinding)container.get("LocationsView1Iterator");
//4-get current row of location view iterator
LocationsViewRowImpl locCRow = (LocationsViewRowImpl)locIter.getCurrentRow();
//5-get department view object from deptIter
ViewObject vo=deptIter.getViewObject();
//6-create new row in department view object
DepartmentsViewRowImpl nrow = (DepartmentsViewRowImpl)vo.createRow();
//7-set values
nrow.setDepartmentId(Integer.valueOf(this.getDeptID()));
nrow.setDepartmentName(this.getDeptName());
//8-get location id from loaction view current row
nrow.setLocationId(locCRow.getLocationId());
vo.insertRow(nrow);
deptIter.getDataControl().commitTransaction();
//to show popup
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Data saved...."));
return null;
}
public void setDeptID(String deptID) {
this.deptID = deptID;
}
public String getDeptID() {
return deptID;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptName() {
return deptName;
}
public void setLocatonID(String locatonID) {
this.locatonID = locatonID;
}
public String getLocatonID() {
return locatonID;
}
}
Please Feel Free To Ask if you Face any Difficulty
Like and share to help others
Thanks
Date 17-01-2019