Hello EclipseLink + BeansBinding on the NetBeans Platform
Above, Beans Binding is used in two different ways. Firstly, it is used to synchronize the JTable with the JPA query. Secondly, Beans Binding is used to synchronize the two JTextFields with the JTable. Thanks to the support in NetBeans IDE for JPA and Beans Binding, we will create the above application without typing a single line of code. In other words, everything will be generated via the various generators provided by NetBeans IDE. More than likely, especially since all these generators entail usage of the dreaded NetBeans blue blocks (which are a pain to work with), you probably don't want to take this approach in production code. However, despite that, the generators are useful in the prototyping phase of your applications, firstly because they show that the above effect can be created at all, and without much trouble (because potentially you've been wondering whether something like this is possible at all), and secondly because you can copy the generated code and use it in your own real life application, i.e., by copying the generated code and then reusing it, you'll not be working with the blue blocks anymore, while still benefiting from the fact that the generated code is correct.
Hence, the blue blocks are not a serious constraint in the approach that will be described here. There is, however, a truly serious objection that should be noted before continuing: the approach taken here does not leverage the true brilliance of the NetBeans Platform. Why? Because we're using a JTable to display the JPA query. It would be far more useful to use one of the NetBeans-specific Swing components instead of a JTable, i.e., one of the NetBeans Platform "explorer views". The NetBeans explorer views are alternative Swing components to the JTable and, as will be shown, are many times more effective than anything the JTable can do for you. However, this aspect will be focused on in a separate part of this sequence of topics. For now, we will assume that the JTable is an effective Swing component and that using it is the correct approach to take.
So, let's get started.
- Follow the Steps from the Earlier Article. Start by doing everything outlined in EclipseLink on the NetBeans Platform. You should end up with a NetBeans Platform application consisting of 4 modules: the UI integration module, the JPA entity integration module, the EclipseLink integration module, and the database server integration module.
- Remove the Code in the TopComponent Constructor. The UI integration module consists of a TopComponent with a JTextArea that displays the data from the database. The code that makes this possible is found in the TopComponent constructor. Delete that code completely. Also delete the JTextArea itself. In the next steps, instead of the JTextArea, we will drag-and-drop a JTable from the Palette and, in the very next step, we will, instead of the code in the constructor, drag-and-drop some Persistence components from the Palette. Therefore, instead of typing code manually, everything will be generated from the Palette, as pointed out in the introductory paragraphs above.
- Drag and Drop the Persistence Components from the Palette. When you look in the Palette (Ctrl-Shift-8), you should find an interesting grouping of Persistence components, highlighted below:
I only discovered these a day or so ago. All three of the components above are needed in the scenario described in this article. Drag and drop each of them onto the TopComponent. Once you have done this, you will not see anything in the TopComponent. However, open the Inspector (first open the TopComponent in the Design view, then open the Inspector from Window | Navigating | Inspector) and you should see this:
Good. Now you have the Persistence components available.
Tip: It is extremely important that the three components are added in the order shown above. In other words, the Entity Manager component needs to be first, the Query component needs to be second, and the Query Result component needs to be third. You can simply drag and drop the three components in the Inspector (i.e., in the screenshot above, just drag the three components into the correct order). Why? Because the order shown above is the order in which the components are initialized, which is extremely important. For example, if the Entity Manager is initialized after the Query, then the Query (which uses the Entity Manager) will return null from the Entity Manager.
- Set the Properties of the Persistence Components. Next, in the Inspector shown above, right-click each of the three Persistence components, one by one, and set their properties, as follows:
- Entity Manager. Set the "persistenceUnit" property to "EntityLibPU". Remember that that is the name of the persistence.xml in the entity classes module.
- Query. Set the "query" property to "SELECT c FROM Customer C". Set the "entityManager" property to the name of the entity manager, which in this case is "entityManager".
- Query Result. Set the "query" property to the name of the query, which in this case is "query". In the "Type Parameters" property (in the Code tab of the Properties sheet), set <Customer>.
To summarize the above instructions graphically, look at the screenshots below:
Great. You're done. You have now set up the Persistence components via the UI in NetBeans IDE. Also, you have enabled the NetBeans Beans Binding support to recognize these components so that they can be bound (in the upcoming steps in this article) to columns in the JTable that you will create in the next step.
- Drag and Drop the JTable and JTextFields from the Palette. Now drag and drop a JTable from the Palette, as well as two Strings and two JTextFields. In the next steps, we will hook these up to the Persistence components. For example, we will bind the "elements" of the JTable to the rows returned by the Query Result that we created in the previous step:
We will also bind the "text" property of the JTextFields to Beans Binding expressions obtained from the JTable. But, that will all come in the following steps. For now, simply lay out your UI on the TopComponent, as shown above: a JTable, two Strings, and two JTextFields. For now, also don't worry about the column names that you see above. These will be set automatically when we bind the JTable to the Query Result.
- Bind the JTable to the Query Result. In the Design mode of the TopComponent, right-click the JTable and then choose Bind | elements. In "Binding Source", choose "list", which is the Query Result you defined earlier:
Next, right-click the JTable again and choose Table Contents. Now, in the Columns tab, you should be able to map columns in the JTable to columns from the Query Result:
- Bind the JTextFields to the JTable. In Design mode, right-click each of the JTextFields in turn and choose Bind | text. Then you can map the JTextField to the JTable and, more specifically, to a particular column in the JTable, using the UI provided by NetBeans IDE:
- Deploy the Application. Start the database server and deploy the application. You should now see the following:
Select a new row in the JTable and then notice that the JTextFields are automatically updated.
You now have a NetBeans Platform application that accesses a database and displays its results in a JTable. The JTable, in turn, is a Master Table, with details being shown in the JTextFields below, which are bound to columns in the JTable. This is a nice further development of our EclipseLink integration with the NetBeans Platform, though now (as pointed out at the start), we should begin leveraging the Swing explorer views, instead of the JTable. The reason why, and the procedure how, will be discussed in the next part. In addition, we will also be adding some important features, such as Save functionality, following NetBeans Platform idioms, which are definitely preferable in this context.