Thursday, May 12, 2016

NetBeans Java EE Tip #8: Persistence Units

The foundational goals of any good IDE are to simply and organize one's code and development workflow.  NetBeans does a superb job of achieving these goals, and sometimes we take the basics for granted.  For instance, if your eyes have ever glossed over from trying to read XML in a plain text editor, you may have had issues configuring and working with Persistence Units in the past.  In case you aren't familiar with Persistence Units, they are XML configuration files that are used by a Java EE project to configure database connections and options.  NetBeans has a very easy-to-use persistence unit wizard, which will be showcased in this post.

For starters, NetBeans provides a wizard for generating a Persistence Unit for a project.  Simply right-click on a project, choose "New" from the context menu, and then choose the "Persistence"->"Persistence Unit" option.

Next, supply a name for your Persistence Unit, along with a Persistence Provider using the drop-down menu of selections.  You can then choose from an available Data Source, and choose a table generation strategy.

Once created, the Persistence Unit can be found within the "Configuration Files" folder of a NetBeans project.

Selecting and double-clicking the file will open the Persistence Unit editor.  NetBeans provides an easy to modify configuration screen for the Persistence Unit that allows changes to be made without working directly with XML.


For instance, one may wish to add an entity class for use with the Persistence Unit.  To do so, simply click the "Add Class" button and choose the desired class(es).


If you'd rather work directly with the XML of a Persistence Unit, choose the "Source" button at the top of the editor.  Rather than seeing drab XML, you will be presented with color-coded and an auto-completing XML document.



Working with history is a breeze with the Persistence Unit configuration within NetBeans.  Choose the "History" button at the top of the editor to see a listing of all changes made to the unit, and it also provides diffs so you can easily see the changes that have been made, and/or apply or revert code, as needed.



NetBeans provides and easy way to work with Persistence Units.  Forget about dealing with XML within a text editor...you'll never want to do that again.

No comments:

Post a Comment

Please leave a comment...