Software Development

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.

11 comments
premal4u
premal4u

Hi Really Really Great begining..

binitakumari07
binitakumari07

It is interesting,Thanks,can u tell me how to use public synonyms in ADF?

parameshwarr
parameshwarr

very basic, but useful for the beginners

v_legaspi
v_legaspi

I get an error when I reach the "Test" step and select my database connection. The error is: Aug 15, 2007 12:56:40 PM oracle.adf.share.config.ADFConfigFactory findOrCreateADFConfig INFO: oracle.adf.share.config.ADFConfigFactory No META-INF/adf-config.xml found What did I do wrong?

Oktet
Oktet

Okay, I am an amateur in the world of Oracle, I have just recently purchased ?Learn SQL in 10 minutes? and I have downloaded Oracle Database Express Edition 10g Release 2 (10.2). I just found your post on Oracle and wanted to learn more, so I commenced to download the Oracle J Developer in addition to the Oracle Database Express Edition 10 g. I was following all the posted instructions and understand that you are also learning about Oracle J Developer; however, I have a really stupid question. Do I need to have an actual database connection in order to try out J Developer? I do not have an external host with a DBMS to connect to that I know of; furthermore, I don?t even know the host name, type or authentication. And when I am prompted with the Test connection prompt I get error messages especially since I am using the default settings for ?Create Database Connection.?

steve.muench
steve.muench

Rex, also check out the "Business components from Tables" wizard in the ADF Business Components category of the gallery. It lets you reverse-engineer entity objects, view objects, and an application module for existing set of related database tables in one shot. Some more ADF resources that might be of interest are: The ADF Learning Center [http://www.oracle.com/technology/products/adf/learnadf.html] has our latest resources for enterprise 4GL Developers getting started with Oracle ADF. This includes the complete ADF Developer's Guide for Forms/4GL Developers [http://www.oracle.com/technology/documentation/jdev/b25947_01/index.html], a companion sample application [http://www.oracle.com/technology/products/jdev/samples/srdemoadfbc.html], and a companion step-by-step tutorial [http://www.oracle.com/technology/obe/ADFBC_tutorial_1013/10131/index.htm]. To complement the free Developer's Guide from Oracle, you might be interested in the Oracle Press title Oracle JDeveloper 10g for Forms & PL/SQL Developers: A Guide to Web Development with Oracle ADF [http://www.amazon.com/Oracle-JDeveloper-Forms-SQL-Developers/dp/0072259604]. My bimonthly DEVELOPER: Frameworks [http://www.oracle.com/technology/products/jdev/tips/muench/oramag/index.html] Oracle Magazine column articles. There is an instructor-led course for Oracle ADF For Forms/4GL Developers [http://education.oracle.com/pls/web_prod-plq-dad/show_desc.redirect?dc=D18384GC10&p_org_id=1001&lang=US&source_call=]. Potentially of interest to any former Oracle Forms or Oracle Designer user is the additional Oracle JHeadstart for ADF [http://www.oracle.com/technology/consulting/9iServices/JHeadstart.html]extension for JDeveloper. This is a complete web-tier page generation solution that has its own step-by-step tutorial [http://www.oracle.com/technology/products/jdev/tips/muench/jhstutorial/index.html?_template=/ocom/technology/content/print] so customers can evaluate whether it might benefit their developer productivity. The JHeadstart Features/Benefits document is here [http://www.oracle.com/technology/consulting/9iservices/JHeadstart%20White%20Paper.pdf] (pdf). The OTN J2EE for Forms Devs [http://otn.oracle.com/formsdesignerj2ee] home page has additional resources of particular interest to Oracle Forms developers. Last but not least, the JDeveloper OTN Discussion Forum [http://forums.oracle.com/forums/forum.jspa?forumID=83] is a great resource to ask JDeveloper/ADF questions of your peers and Oracle employees working on/with the technology. Hope this helps

Justin James
Justin James

Rex - A lot of this is comparable to Visual Studio. I do like some of those testing features though. VS's Query Editor is the standard Microsoft widget that I've been seeing for years, and it is a touch quirky, just like the image editor Office used didn't change in 10 years and it was wretched. VS's Query Editor really is not very good for testing and such, the tools you show at the end of the piece look much better. J.Ja

RexWorld
RexWorld

When I run the Test widget I get that same note in the JDeveloper screen. It's just a warning, it's not an error. The Component Browser window should still launch. It may have launched behind the JDeveloper main window. I'm running on Ubuntu Linux, and for me the Component Browser window launches on top of the JDeveloper window. But like I said, it might be launching elsewhere for you.

RexWorld
RexWorld

All my sample screenshots are from my laptop where I'm running the same configuration you are -- an Oracle XE database as the backend source and JDeveloper as my front-end tool. Couple possibilities--if you're creating a connecting to XE, I believe the defaults in JDeveloper are not correct. There's a part of the connection screen where it says the SID is 'ORCL'. But if your connecting to an XE database the default SID (assuming you did not change it when creating the database) is actually 'XE' and not 'ORCL'. Also make sure you've selected "thin" as the client type. Hope that helps.

RexWorld
RexWorld

Thank you for sharing all these resources. They will definitely help a ton. That Oracle Press book you mention is the one that I have been working with to try and learn this stuff.

v_legaspi
v_legaspi

I was getting an empty Business Component Browser window. Looks like I forgot the step about including my View Object in the AppModule. I'm good now... thanks!