X

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

Guest Author

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

Join the discussion

Comments ( 10 )
  • guest Thursday, June 28, 2012

    Thanks!!

    Is just i needed....

    :)


  • Daniel Domingos Friday, October 12, 2012

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


  • Mark Heckler Friday, October 12, 2012

    Hi Daniel,

    Glad it helped. All the best to you!

    Mark


  • Edgar Morel, PARAGUAY Sunday, December 9, 2012

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


  • Mark Heckler Monday, December 10, 2012

    De nada, Edgar, glad you found it useful!

    Todo lo mejor,

    Mark


  • Daniel Thursday, June 20, 2013

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


  • Mark Heckler Thursday, June 20, 2013

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


  • Muhammad Umair Naqvi Tuesday, April 8, 2014

    God bless you and bundle of thanks. Really helpful.


  • Muhammad Umair Naqvi Tuesday, April 8, 2014

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


  • Mark Heckler Tuesday, April 8, 2014

    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


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.