Reader small image

You're reading from  Java EE 7 Development with NetBeans 8

Product typeBook
Published inJan 2015
Reading LevelIntermediate
PublisherPackt
ISBN-139781783983520
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
David R Heffelfinger
David R Heffelfinger
author image
David R Heffelfinger

David Heffelfinger is the Chief Technology Officer of Ensode Technology, LLC, a software consulting firm based in the greater Washington DC area. He has been architecting, designing, and developing software professionally since 1995, and has been using Java as his primary programming language since 1996. He has worked on many large-scale projects for several clients including the US Department of Homeland Security, Freddie Mac, Fannie Mae, and the US Department of Defense. He also has a Masters degree in Software Engineering from Southern Methodist University. David is Editor-in-Chief of Ensode.net (http://www.ensode.net), a website about Java, Linux, and other technology topics.
Read more about David R Heffelfinger

Right arrow

Chapter 4. Interacting with Databases through the Java Persistence API

The Java Persistence API (JPA) is an object-relational mapping (ORM) API. ORM tools help us to automate the mapping of Java objects to relational database tables. Earlier versions of J2EE used Entity Beans as the standard approach for ORM. Entity Beans attempted to always keep the data in memory synchronized with the database data, a good idea in theory, however, in practice this feature resulted in poorly performing applications.

Several ORM APIs were developed to overcome the limitations of Entity Beans, such as Hibernate, iBatis, Cayenne, and TopLink, among others.

Java EE 5 deprecated Entity Beans in favor of JPA. JPA took ideas from several ORM tools and incorporated them in the standard. As we will see in this chapter, NetBeans has several features that make development with JPA a breeze.

The following topics will be covered in this chapter:

  • Creating our first JPA entity

  • Interacting with JPA entities using EntityManager...

Creating our first JPA entity


JPA entities are Java classes whose fields are persisted to a database by the JPA API. These Java classes are Plain Old Java Objects (POJOs), and as such, they don't need to extend any specific parent class or implement any specific interface. A Java class is designated as a JPA entity by decorating it with the @Entity annotation.

In order to create and test our first JPA entity, we will create a new web application using the JavaServer Faces framework. In this example, we will name our application jpaweb, and (as with all of our examples) we will use the bundled GlassFish application server.

Tip

Refer to Chapter 2, Developing Web Applications Using JavaServer Faces 2.2, for instructions on creating a new JSF project.

To create a new JPA entity, select the Persistence category from the new file dialog and select Entity Class as the file type.

After doing so, NetBeans presents the New Entity Class wizard.

At this point, we should specify the values for the Class Name...

Automated generation of JPA entities


In many projects, we will be working with an existing database schema created by a database administrator. NetBeans can generate JPA entities from an existing database schema, which saves us a lot of potentially tedious work.

In this section, we will use a custom database schema. In order to create the schema, we need to execute an SQL script that will create the schema and populate some of its tables. In order to do this, we need to go to the Services window, expand Databases, right-click on JavaDB, and select the Create Database... option.

Then, we need to add the database information in the Create Java DB Database wizard.

At this point, we can open our SQL script file by going to File | Open File... and navigating to its location on our disk and opening it. The filename of our script is create_populate_tables.sql. It is included as part of the source bundle for this chapter. The following screenshot shows the file as soon as we open it in our project...

Generating JSF applications from JPA entities


One very nice feature of NetBeans is that it allows us to generate JSF applications that will perform Create, Read, Update, and Delete (CRUD) operations from existing JPA entities. This feature, combined with the ability to create JPA entities from an existing database schema as described in the previous section, allows us to write web applications that interact with a database in record time.

To generate JSF pages from existing JPA entities, we need to right-click on File, select New File, select the JavaServer Faces category, and then select the JSF Pages from Entity Classes file type.

Note

In order to be able to generate JSF pages from existing JPA entities, the current project must be a Web Application project.

After clicking on Next, we need to select one or more JPA entities. We would typically want to select all of them, and they can easily be selected by clicking on the Add All button.

The next page in the wizard allows us to specify a package...

Summary


In this chapter, we saw the many ways in which NetBeans can help us speed up the development of applications by taking advantage of JPA.

We saw how NetBeans can generate new JPA classes with all the required annotations already in place. Additionally, we covered how NetBeans can automatically generate code to persist a JPA entity to a database table. We also covered how NetBeans can generate JPA entities from an existing database schema, including the automated generation of JPQL named queries and validation. Finally, we saw how NetBeans can generate a complete, visually appealing JSF application from existing JPA entities.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Java EE 7 Development with NetBeans 8
Published in: Jan 2015Publisher: PacktISBN-13: 9781783983520
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
David R Heffelfinger

David Heffelfinger is the Chief Technology Officer of Ensode Technology, LLC, a software consulting firm based in the greater Washington DC area. He has been architecting, designing, and developing software professionally since 1995, and has been using Java as his primary programming language since 1996. He has worked on many large-scale projects for several clients including the US Department of Homeland Security, Freddie Mac, Fannie Mae, and the US Department of Defense. He also has a Masters degree in Software Engineering from Southern Methodist University. David is Editor-in-Chief of Ensode.net (http://www.ensode.net), a website about Java, Linux, and other technology topics.
Read more about David R Heffelfinger