How to Plug a Small Hole in NetBeans JSF (Join Table) Code Generation

I was asked recently to provide an assist with designing and building a small-but-vital application that had at its heart some basic CRUD (Create, Read, Update, & Delete) functionality, built upon an Oracle database, to be accessible from various locations. Working from the stated requirements, I fleshed out the basic application and database designs and, once validated, set out to complete the first iteration for review.

Using SQL Developer, I created the requisite tables, indices, and sequences for our first run. One of the tables was a many-to-many join table with three fields: one a primary key for that table, the other two being primary keys for the other tables, represented as foreign keys in the join table. Here is a simplified example of the trio of tables:


Once the database was in decent shape, I fired up NetBeans to let it have first shot at the code. NetBeans does a great job of generating a mountain of essential code, saving developers what must be millions of hours of effort each year by building a basic foundation with a few clicks and keystrokes. Lest you think it (or any tool) can do everything for you, however, occasionally something tosses a paper clip into the delicate machinery and makes you open things up to fix them. Join tables apparently qualify.  :-)

In the case above, the entity class generated for the join table (New Entity Classes from Database) included an embedded object consisting solely of the two foreign key fields as attributes, in addition to an object referencing each one of the "component" tables. The Create page generated (New JSF Pages from Entity Classes) worked well to a point, but when trying to save, we were greeted with an error: Transaction aborted. Hmm.

A quick debugger session later and I'd identified the issue: when trying to persist the new join-table object, the embedded "foreign-keys-only" object still had null values for its two (required value) attributes...even though the embedded table objects had populated key attributes. Here's the simple fix:

In the join-table controller class, find the public String create() method. It will look something like this:

    public String create() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("JoinEntityCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

To restore balance to the force, modify the create() method as follows (changes in red):

    public String create() {

        try {
            // Add the next two lines to resolve:
            current.getJoinEntityPK().setTbl1id(current.getTbl1().getId().toBigInteger());
            current.getJoinEntityPK().setTbl2id(current.getTbl2().getId().toBigInteger());
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("JoinEntityCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

I'll be refactoring this code shortly, but for now, it works. Iteration one is complete and being reviewed, and we've met the milestone. Here's to happy endings (and customers)!

All the best,
Mark

Comments:

Thanks!!

Is just i needed....

:)

Posted by guest on June 28, 2012 at 03:01 PM PDT #

Thanks a lot, you've save my day!!!!!!! ;-)

Posted by Daniel Domingos on October 12, 2012 at 06:24 AM PDT #

Hi Daniel,

Glad it helped. All the best to you!

Mark

Posted by Mark Heckler on October 12, 2012 at 01:19 PM PDT #

You are a GENIUS!!!, it also solved the problem with the bad values in the list table!

Posted by Edgar Morel, PARAGUAY on December 09, 2012 at 05:59 AM PST #

De nada, Edgar, glad you found it useful!

Todo lo mejor,
Mark

Posted by Mark Heckler on December 10, 2012 at 07:37 AM PST #

Thanks very much, you've helped me alot, one love from Angola!!!!

Posted by Daniel on June 20, 2013 at 01:47 AM PDT #

You're very welcome, Daniel - glad it helped. All the best to you in Angola!!!

Posted by Mark Heckler on June 20, 2013 at 07:09 AM PDT #

God bless you and bundle of thanks. Really helpful.

Posted by Muhammad Umair Naqvi on April 07, 2014 at 09:24 PM PDT #

Thanks and hope you could help people with Netbeans primefaces generated code as well.

Posted by Muhammad Umair Naqvi on April 07, 2014 at 09:25 PM PDT #

And to you, Muhammad. That's a great idea...I'll try to get some PrimeFaces-related material out there soon. Thanks, and happy coding!

Mark

Posted by Mark Heckler on April 08, 2014 at 08:11 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

The Java Jungle addresses topics from mobile to enterprise Java, tech news to techniques, and anything even remotely related. The goal is to help us all do our work better with Java, however we use it.

Your Java Jungle guide is Mark Heckler, an Oracle Java/Middleware/Core Engineer with development experience in numerous environments. Mark's current work pursuits and passions all revolve around Java and leave little time to blog or tweet - but somehow, he finds time to do both anyway.

Mark lives with his very understanding wife, three kids, and dog in the St. Louis, MO area.



Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today