Tuesday, February 17, 2009

Developing an ADF/Faces-Toplink application

In this post I'd like to address a few common issues that developers face while creating an ADF/Faces and Toplink application. For such an application, usually this is the flow of steps:

  1. This is assuming the usage of JDeveloper for development.
  2. Create an application with two projects. One for model(which will contain toplink objects) and one for the UserInterface.
  3. First, in the model project, start by creating "Java objects from tables". Make the toplink POJOs Serializable. Create the named toplink queries(find, update and insert) methods. Create the Facade Session bean to facade the named queries. Then create a DataControl out of the Session Bean. Mind you, this Datacontrol is good as long as things work by dragging and dropping into the JSF pages.
  4. Now, create the JSF pages in the UI project. The best way to do this is by going to the Overview window of the faces-config.xml file. Here you drop the jsf pages and navigations. Then you double-click on the jsf pages and create the actual jsf jsp pages.
  5. Sometimes when you'd like to call the facade session bean methods on your own then you have the job of looking it up. But a few pointers first, that I have noticed: Always make the facade session bean implement the Remote interface(implementing only Local is logical but doesn't work :( ), following is how the SessionBean looks like:

  6. @Stateless(name="SessionEJB1")
    public class SessionEJB1Bean implements SessionEJB1, SessionEJB1Local {


    and the following is the code to lookup the session bean in JDev 10.1.3.2/3:

    InitialContext ctx=new InitialContext();
    Object o = ctx.lookup("SessionEJB1");
    SessionEJB1 ejb = (SessionEJB1)PortableRemoteObject.narrow(o, SessionEJB1.class);
    return ejb;


  7. One big challenge in such applications is the creation of new objects or in other words new rows in a table, how would you handle the new ID to be assigned. If you are using Oracle database(I do not experience with other databases) the best way to handle assignment of new IDs is to use native sequencing. In the top-level of the toplink mapping set the sequencing to use "Native sequencing". Obviously, you will need to have sequences created in the database for the tables in which new rows are going to be created. In the toplink mapping window go to the details of a table and set the sequencing information by using the sequence name you created for that table in the database. That's it! You will not have to set the ID parameter in the new java object before persisting it. The method to use is the persistEntity(Object) method in the facade session bean. It returns the persisted object which contains the newly assigned ID. Cool!

No comments: