Pregunta

I have a bean in my Fusion Web Application where I'm supposed to insert new data into a table of my database through java code (after appropriate validation).

The question is how should I do the insertion?

Should I use Entity Objects?

How?

P.S.: This is not the way it should work http://jneelmani.blogspot.com/2009/11/adf-insert-using-storeprocedure.html

¿Fue útil?

Solución

I created Entity Object and View Object by the database table "Employees" and then created application module where included this view object (also were generated java classes for entity object, view object and appModule. EmployeeInfo is just POJO). Inside the application module I created methods:

public EmployeeViewRowImpl saveEmployee(EmployeeInfo EmployeeInfo) {
    // Получаем ViewObject
    EmployeeViewImpl employeeView = getEmployeeView1();
    // Готовим новую строку.
    EmployeeViewRowImpl employee = createEmployeeViewRowImpl(employeeView, employeeInfo);
    // Производим операцию вставки.
    employeeView.insertRow(employee);
    // Коммитим
    try {
        getDBTransaction().commit();
        return employee;
    } catch (JboException e) {
        getDBTransaction().rollback();
        return null;
    }
}

private EmployeeViewRowImpl createEmployeeViewRowImpl(EmployeeViewImpl employeeView, EmployeeInfo employeeInfo) {
        EmployeeViewRowImpl employee = (EmployeeViewRowImpl)EmployeeView.createRow();
        employee.setName(employeeInfo.getName());
        return employee;
}

And to use this one should just call:

public static AppModuleImpl getApp() {
    return (AppModuleImpl)Configuration.
            createRootApplicationModule(
            "com.test.service.AppModule", // where your module is stored
            "AppModuleShared"); // chosen configuration
}

and then ...

RegistrationAppModuleImpl app = getApp();
app.saveUser(userInfo)

...

Otros consejos

May be i'm not to clear on the dynamics of what you are trying to do, but with Oracle ADF, CRUD operations (such as Insert), are easily handled by exposing them from Data Controls. To be more specific, once you have an EO, you should create a View Object and an Application Module. After that, inside the AppMod -> Data Model , add the created VO. This way it will be exposed in the Data Controls panel, and you can expand the 'Operations' folder, and drag'n'drop the CreateInsert operation possibly within a form, or an updatable table. Please refer to this link: CreateInsert Operation - ADF.

If for some other reason you want to handle this process in a programmatic approach, i might think about two possible ways:
1. Get into your managed bean code an instance of the above mentioned AppMod, and from that, a VO instance.
AppModule mod = AppModule)Configuration.createRootApplicationModule("packageName.AppModule", "AppModuleLocal"); ViewObject vo = mod.getViewObject1();
After that, create a new row and commit the newly added values.
2. If you have already exposed a UI component (such a table), you can grab the Binding Context of the current page and from the table's iterator, create a new row.

        DCBindingContainer DCB = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
                DCIteratorBinding iterator = bc.findIteratorBinding("ViewObject1Iterator");
        Row r = iterator.getCurrentRow();
        r.setAttribute("attibName", attribValue);

You can do the insertion using entity object as below:

/* Create a new Customer and Return the new id */
public long createCustomer(String name, String city, Integer countryId) {
    EntityDefImpl customerDef = CustomerImpl.getDefinitionObject();
    CustomerImpl newCustomer = 
       (CustomerImpl)customerDef.createInstance2(getDBTransaction(),null);
    newCustomer.setName(name);
    newCustomer.setName(name);
    newCustomer.setCountryId(countryId);

    try {
        getDBTransaction().commit();
    }
    catch (JboException ex) {
        getDBTransaction().rollback();
        throw ex;
    }

    DBSequence newIdAssigned = newCustomer.getId();
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top