NetBeans Platform CRUD Tutorial Using Maven
This tutorial needs a review. You can edit it in GitHub following these contribution guidelines. |
This tutorial demonstrates how to use the Maven build framework to create a simple NetBeans Platform application that can read from and write to a database. In this document you will use Maven archetypes to create the NetBeans Platform application and module, and the Swing UI toolkit and "Matisse" GUI Builder to create window components.
This document is based on the Ant-based NetBeans Platform CRUD Application Tutorial and can be used to understand some of the differences between using Ant and Maven to develop NetBeans Platform applications. After you understand how Maven is different from Ant, you can easily proceed through other tutorials on the NetBeans Platform Learning Trail.
You do not need to download Maven because it is bundled with the IDE. Optionally, use your own download of Maven, in which case use the Options window to configure your Maven settings. |
This is not a beginners tutorial. Before starting this tutorial, it is important that you familiarize yourself with the following introductory Maven documentation: |
Watch this YouTube movie before beginning to work on the tutorial because it shows the steps you will take below:
Creating and Running the Sample Application
In this section, you set up a sample application provided by NetBeans IDE. The sample application access data and displays data in a Maven-based NetBeans Platform application, similar to the application you create in this tutorial. Successfully running the sample application and displaying data will be useful in determining whether the database connection is set up correctly.
-
Choose File > New Project (Ctrl-Shift-N) to open the New Project wizard. Select the Samples | Maven category and then select "CRUD Sample Application":
Click Next.
-
Specify a location:
Click Next.
-
Change, if needed, the location of libraries that the sample will use:
Click Finish.
Once the project is opened, if you see projects with [unloadable] in the display name, that means something is wrong with the POM file or that plugins are missing:
Developers seeing the above in the IDE are typically new to Maven or are setting up Maven for the first time on their system.
If you see the above, wait for the Maven dependencies to download, which is a process that you can follow by looking in the status bar of the IDE. Once the downloading has completed, and not before, right-click each project and choose "Build with Dependencies". Also see this issue if problems relating to missing plexus-utils.jar
occur. If problems continue, refer to the links at the start of this document, to set up your Maven repository correctly.
-
Examine the project structure:
The project structure above is explained here:
-
NetBeans Platform Application. This project is a Maven reactor project for the NetBeans Platform application, which lists the modules to include and the location of the project’s repositories. This project does not contain any sources. The IDE generates the modules containing the sources and resources in sub-directories of this project.
-
NetBeans Platform based application. The NetBeans Platform application project enumerates included modules; permits interactive runs; produces various kinds of packaging, such as ZIP by default, but optionally JNLP, NBMs; holds functional tests. Each module project specifies its own compile dependencies, while the app project’s dependencies are anything additional that should be present at runtime. For example, that includes, by default, the whole platform cluster, that is, the set of modules constituting the "platform" cluster. The app project could include other clusters, or subsets of clusters, or whatever plugins you want included in your app which are not used as compilation dependencies. At a minimum, core.startup and its transitive dependencies are included.
-
Platform application branding resources. This project contains the global resources used for branding the application, such as the splash screen.
-
sample NetBeans Module. The project that contains the source code for accessing the database and displaying it in a window.
1. You need to have access to a database server. This tutorial uses the JavaDB database server, together with a sample database. Before continuing, start the JavaDB server in the Services window by connecting to the sample database. Right-click on the connection node shown below and choose Connect:
If it is not already available, an easy way to make the JavaDB database server available is to register an instance of GlassFish that is bundled with the IDE. The JavaDB database server and a sample database are included with the GlassFish server.
-
Right-click the NetBeans Platform based application ( ) and choose Run. The application starts up and should show the following:
Use Window | Customer to open the Customer window, wait a few seconds, and you should see your data displayed:
-
Open the editor window and notice that the selected customer’s details are shown in the editor. Make changes in the editor and save them and then notice that the viewer is automatically updated. Changes you make are stored in the database.
If you were not able to complete the above steps, do not continue with the rest of the tutorial. If you encountered problems relating to Maven repositories and unresolved Maven dependencies, refer to the introductory Maven links at the start of this document. |
Since your database is being accessed correctly and your Maven dependencies have been correctly resolved, you are now ready to continue creating your own CRUD application.
Creating the NetBeans Platform Application
In this section you use the New Project wizard to create a NetBeans Platform application from a Maven archetype.
-
If it is still open, close the sample application you created in the previous section. You do not need the sample anymore and leaving it open may cause confusion while you work on your real application.
-
Choose File > New Project (Ctrl-Shift-N) to open the New Project wizard. Select NetBeans Application from the Maven category:
Click Next.
-
Type MavenPlatformCRUDApp for the Project Name and set the Project Location:
Click Next.
-
Select the version of the NetBeans APIs you’d like to use and type the name of a NetBeans module that you’d like to be created together with the application, in this case MavenPlatformCRUDApp-dbaccess:
Click Finish.
Depending on the availability of JARs as specified by Maven dependencies in your repository, the IDE will spend some time downloading Maven dependencies. Once the downloading of Maven dependencies is completed, you should see the following:
The above project structure is explained here:
-
app. This project is a Maven reactor project for the NetBeans Platform application, which lists the modules to include and the location of the project’s repositories. This project does not contain any sources. The IDE generates the modules containing the sources and resources in sub-directories of this project.
-
branding. This project contains the global resources used for branding the application, such as the splash screen.
-
dbaccess. The project that will contain the Java sources you will create in the next section.
-
parent. The NetBeans Platform application project enumerates included modules; permits interactive runs; produces various kinds of packaging, such as ZIP by default, but optionally JNLP, NBMs; holds functional tests. Each module project specifies its own compile dependencies, while the app project’s dependencies are anything additional that should be present at runtime. For example, that includes, by default, the whole platform cluster, that is, the set of modules constituting the "platform" cluster. The app project could include other clusters, or subsets of clusters, or whatever plugins you want included in your app which are not used as compilation dependencies. At a minimum, core.startup and its transitive dependencies are included.
1. Right-click the "parent" project and choose "Build with Dependencies". After the process has completed, right-click the "app" project and choose Run. You should see the default splash screen and then the main window of the application:
You have successfully set up the application and are now ready to begin generating the application’s model classes.
Creating the Model
In this section you generate Java Persistence API (JPA) entity classes from tables in the Java DB database. To create the entity classes and to use JPA in your application, you need to have access to a database server and the JPA persistence provider libraries.
Though this tutorial uses the JavaDB database server, you can later configure the application to use other database servers.
Generating Entity Classes From the Database
In this section you use a wizard to generate entity classes in the dbaccess module.
-
Right-click the Source Packages of the dbaccess module and choose New > Other. Select Entity Classes from Database in the Persistence category:
Click Next.
-
Select the Java DB sample database from the Database Connection drop-down list. Select the Customer table from the Available Tables list and click Add. When you click Add, the related tables, such as DiscountCode, which could vary depending on your version of the database, are also added to the list of Selected Tables list:
Click Next.
-
Leave the package name as suggested, that is, com.mycompany.mavenplatformcrudapp. Make sure that the Create Perisistence Unit and Generate Named Query Annotations are selected:
-
Click Finish. When you click Finish, the IDE generates an entity class for each selected table. The IDE also generates the
persistence.xml
file in theMETA-INF
package under the Other Sources node in thesrc/main/resources
directory:
-
Right-click the dbaccess module and choose Build. Depending on their availability in your repository, several JARs may now start to be downloaded.
Adding the DerbyClient as a Runtime Dependency
In this section you will add derbyclient.jar
library as a dependency.
-
Right-click the Dependencies node of the dbaccess module and choose Add Dependency:
The Add Library dialog appears.
-
Specify the library by typing org.apache.derby for the "Group ID", derbyclient for the "Artifact ID", select runtime from the Scope drop-down, and choose the latest version available:
Notice that you can use code completion to help you throughout the above dialog:
Click Add.
-
Expand the Runtime Dependencies node in the Projects window and you can see that the
derbyclient
library is listed as a dependency:
You have now registered the Derby database dependency for your module.
In this section, you created a module that now contains entity classes for the tables you’d like to access, together with a persistence.xml
file providing the data access information, and new dependency declarations in the project’s POM file.
Creating the Viewer
In this section, we create a simple prototype GUI component that accesses our data and displays it.
-
Right-click the dbaccess module and choose New | Window. Define the window to be opened in the "explorer" position and let it open when the application starts:
Click Next.
-
Set "Viewer" as the class name prefix.
Click Finish.
-
In the Source tab of the new window, redefine the constructor as follows:
public ViewerTopComponent() {
initComponents();
setName(Bundle.CTL_ViewerTopComponent());
setToolTipText(Bundle.HINT_ViewerTopComponent());
setLayout(new BorderLayout());
JTextArea area = new JTextArea();
add(area, BorderLayout.CENTER);
EntityManager entityManager = Persistence.createEntityManagerFactory("com.mycompany_MavenPlatformCRUDApp-dbaccess_nbm_1.0-SNAPSHOTPU").createEntityManager();
Query query = entityManager.createNamedQuery("Customer.findAll");
List<Customer> resultList = query.getResultList();
for (Customer c : resultList) {
area.append(c.getName() + " (" + c.getCity() + ")" + "\n");
}
}
-
Right-click in the editor and choose Fix Imports (Ctrl-Shift-I). Make the choices below in the dialog that appears:
Click OK. The import statements at the top of the class should now be as follows:
import java.awt.BorderLayout;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JTextArea;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;
-
Right-click the parent project and choose Clean. Right-click the app project and choose Build with Dependencies and, once the process has completed, right-click it again and choose Run.
After the splash screen is shown, you should see this:
The simple prototype is finished. You’re using very few NetBeans APIs at the moment, but you’re able to retrieve data from your database and display it in your view component.
Creating the Editor
In this section, you add the start of an editor module to the application. You then continue with the instructions in the Ant based NetBeans Platform application tutorial to set up and listen to the selection and populate the editor with objects found there.
-
In the New Project dialog, choose NetBeans Module in the Maven category:
Click Next.
-
Name the module MavenPlatformCRUDApp-editor. On the next page, set the project location to the folder where the other modules are found, which is within the parent folder. Click Next.
-
Deselect the OSGi checkbox:
Click Finish. Because you set the project location to be the folder where the parent project is found, you see that the new module is created and visible within the parent project:
-
Though the new module is now part of the parent project, it is not part of the app project yet. Therefore, when you run the application, your module will currently not be included. Let’s include it now. In the app project, right-click Dependencies, click Add Dependency, select the Open Projects tab, and choose the editor module.
1. In the same way as you did in the previous section, create a new window, in the editor module. This time, it should be opened at start up in the "editor" position, with "Editor" as its class name prefix.
-
Right-click the parent project and choose Clean. Right-click the app project and choose Build with Dependencies and, once the process has completed, right-click it again and choose Run.
After the splash screen is shown, you should see this:
-
Now that you have completed the steps above, you can refer to the Ant-based NetBeans Platform CRUD Application Tutorial, since all the Java code in that tutorial is applicable to the application you are creating here.
You have a basic understanding of how developing with Maven is different from Ant. You can easily proceed through other tutorials on the NetBeans Platform Learning Trail and apply them to your Maven scenarios.
See Also
This concludes the CRUD Tutorial. This document has described how to use the Maven build framework to create a new NetBeans Platform application with CRUD functionality. For more information about creating and developing applications, see the following resources.
If you have any questions about the NetBeans Platform, feel free to write to the mailing list, dev@platform.netbeans.org, or view the NetBeans Platform mailing list archive.