id="info"

Developer

Learning Oracle ADF: Creating Java objects from database tables

If you're like me, trying to learn a new programming technology in your spare time can be difficult. Unless it's part of your immediate responsibilities at work, there's little motivation pushing you to learn. That's how it has been with me and Oracle ADF (Application Development Framework). I know I should learn it, since it appears to be the future direction for creating custom forms and reports against Oracle databases. But my current responsibilties focus on the previous technologies, Oracle Forms and Reports. It's hard finding time and motivation to worry about the future when the present takes up all my energy at work.

I finally decided the only way I'm going to do this is to find a forcing function — something that will prod me off my lazy programmer butt to dive into the wonders of ADF. I realized that this blog could be that forcing function. So I'm going to spend the next few posts poking around in ADF.

Caveat: There will probably be things that I do horribly wrong, since I don't know the technology in advance. I'll be trying to learn some new aspect of it for each blog entry. This means that something I blog about might be completely wrong, and I won't find out until a later blog entry.

But, hey, I don't mind making a fool of myself in public, so here goes.

Working with Oracle ADF

First, you need to install the right version of JDeveloper. I am running 10.1.3.3, which I downloaded from Oracle's site. Make sure you get the Studio Edition because the other two editions do not include the ADF components.

For those new to Oracle application development, ADF is a bunch of pieces that integrate into Oracle JDeveloper. There's the underlying Java framework supporting all three layers of the infamous MVC (model-view-controller) architecture, and there's a bunch of ADF wizards to help you start building the pieces. There are also various ADF editors that let you modify the starting blocks created from the ADF wizards.

You start an ADF application the same way you start any application in JDeveloper —with the Create Application wizard. I know a couple ways to launch the Create Application wizard — the easiest is to right-click the Application node at the top of the Application pane (unless you've re-arranged the panes, it's the one in the upper left corner of JDeveloper). The very first option in the right-click menu will be New Application. Click that option, and you will see the wizard pop up:

Create Application

Most of the options are pretty self-explanatory — name for your app, location for the files, and the package prefix that JDeveloper should use when creating new classes. The one part that matters for our purposes is the Application Template. You have to pick a template that includes ADF; in this case, I've selected the Web Application that includes support for JSF and ADF BC (ADF Business Components).

When you finish the wizard, it builds two projects in your new app — one for the model and the other for the view-controller. For now, I'll focus on the model; I'll save the view-controller for a future blog post.

In general, I've found JDeveloper works best if you do the database connections before you actually need them. So, before you dive into the rest of the ADF wizards, go to the Connections tab and create your database connection.

Then go back to the Applications tab. Expand your new application tree (if it isn't expanded already) by clicking the little plus sign to the left of it. Now right-click on the Model node in the tree and select New from the pop-up menu. This loads the New Gallery screen. If you selected the Web Application template like I did, you'll see a list something like this:

New Gallery

Open the Business Tier from the left-hand side and then select ADF Business Components. On the right, you'll see the available components. We're going to start by creating an Entity Object to represent a table, so select that option and click OK.

You should get a dialog box where you specify the database connection to use. If you didn't create one earlier, you'd click the New button here. But if you did already create the connection, it should appear in the drop-down list. Select it, and if it's an Oracle database, you can accept the rest of the default settings and click OK. If not, you'll need to tweak the settings to match your connection.

This launches the Create Entity Object wizard. Click Next and select the table you're going to model with this object. There are check boxes at the bottom of the wizard if you want to see other database objects besides tables, like views and synonyms. For now, I'm keeping it simple and just modeling a table. My table is called ConferenceLine, so I'm also using that as the name for my object.

Click Next to show the list of available attributes on the table. You can even create attributes unique to this object; again, for this first attempt, I'm going to stick to a strict model of the underlying table. Click Next again to bring up the following Attribute Settings screen:

Create Entity Object

This is where things get kinda hairy. In general, you can probably accept the default settings for each attribute. In my case, I happen to know that the ID field has a trigger on it to auto-increment the ID. I want to tell ADF that this is an auto-incremented field, so it knows to keep in sync when updating and inserting. That's why in the above screenshot, you'll see I selected DBSequence as the type for the ID field.

Strictly speaking, if you're just creating a view object, you probably wouldn't worry about this. But, if you plan on doing updates/inserts thru this model object, you should tell it about identity fields like that.

You can click Next to go thru the rest of the screens, but at this point, you could also  click Finish to have JDeveloper create the object for you. You should now see a folder under the Model called Application Sources. Your new entity object will appear in the folder.

Now you create a View Object to work with this Entity. Right-click on the model node, select New, and this time create a View Object. Similar wizard the Entity Object, one of the first screens of the View Object gives you the option of creating a view that can update data or simply display data from the Entity. (I think it's a little weird that you do this part before even selecting which Entity you're mapping. To me, you should pick the Entity first and then specify whether you want updatable or just viewable access.) The Entity you created earlier should appear in the View Object wizard. Select it and then select all the attributes you want included in this view. At this point, you can click Finish to let it create the new View Object.

But wait... we're still not quite done. Finally, you'll go thru the same steps, but this time create an Application Module. As you step thru the wizard, the View Object you just created should appear, and you'll want to select it for your new Application Module. Click Finish once you've reached that screen.

At this point, JDeveloper has created a bunch of Java and XML files for you. You can peek at them in the Structure pane, but the thing you probably most want to do is see if this is working.

I think this is one of the cooler features in ADF. Instead of making you work up a real UI at this point, there's a built-in test framework to let you check that the new application module works. Right-click the name of your application module and select Test from the pop-up menu. JDeveloper compiles the code and prompts you with a window to select the connection it should use for this test. Select the same connection name you used earlier and click Connect.

The Component Browser will launch with your Application Module listed on the left:

Component Browser

Under that should be the name of the View Object you linked to earlier in this Application Module. Double-click the View Object, and if everything's wired properly, it will open a database browser into the table. You can insert new records and cruise thru existing records to verify that it's all working.

I know some people are really, really good with JDBC, but it would have taken me days if not a week to work up all the code that these wizards gave me in about 30 minutes. The real proof is in the UI — that will really show whether all this stuff is worth it.

My next step is seeing how you wire these back-end business components into a Web UI.

Editor's Picks