Enterprise Java

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.

netbeans-ee-8a

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.

netbeans-ee-8b

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

netbeans-ee-8c

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.

netbeans-ee-8d

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).

netbeans-ee-8e

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.

netbeans-ee-8f

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-ee-8g

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.

Josh Juneau

Josh is an application developer and technical writer. He has authored several books for Apress, primarily focusing on Java development. Most recently, he has authored Java EE 7 Recipes, Introducing Java EE 7, and Java 8 Recipes. Josh is a member of the JCP, and he is on the JSF 2.3 Expert Group.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ro Ro
Ro Ro
5 years ago

forget we will use NetBeans creepy designer, source code is most effective as work style

Back to top button