Wednesday, February 10, 2016

NetBeans Java EE Tip #1 - Entity Classes From Database

The NetBeans IDE is an excellent choice for developing applications of all kind.  Specifically, I use it on a daily basis for developing and maintaining Java EE applications.  Not only has Java EE become much more productive over the past few releases, but the NetBeans IDE has also reduced the time that it takes to develop an application...making Java EE and NetBeans an excellent match!

One of the features that I use the most is the ability to generate entity classes from the database.  NetBeans makes it easy to add an entity class to your project without any coding.

First, right-click on a project package, and choose "New->Entity Classes from Database" (Figure 1).

Figure 1:  New Entity Class from Database

Next, select a data source, and then choose at least one table from the "Available Tables" list, as shown in Figure 2.

Figure 2:  Choose Table from Available Tables list

Choose the Project, location, and package to which the entity class will be added.  Then specify any preferences to indicate if you'd like named queries, JAXB annotations, or MappedSuperClass instead of entities. (Figure 3)
Figure 3:  Specify Package name and preferences

Lastly, select mapping options for your entity class, as shown in Figure 4.

Figure 4:  Entity Class Mapping Options have an entity class that is ready to use...compliments of NetBeans IDE!

Figure 5:  Completed Entity Class

1 comment:

  1. It is a nice feature, but I quickly had a additional functionality. Especially the fact that once loaded, the coder is responsible for keeping the in memory representations in sync. So I created a generator that based on the foreign keys also adds setters, getters, adders and removers that called each other. These generated classes are @MappedSuperclass where everything is protected and the business logic can then be placed in the extending custom class. Works remarkably well.


Please leave a comment...