- Using Netbeans to create Mavan based Apache Wicket projects.
- Using Netbeans to add and configure Hibernatea, a JPA comaptable Persistence provider to the project.
- Render tabular data from a database to the browser using Apache Wicket repeating views and models.
- Creating a Maven Apache Wicket Web project in Netbeans
- Configuring the project to use JPA and Hibernate.
- Adding the required libraries to the project using Maven
- Mapping POJOs to Database tables using JPA support in Netbeans.
- Creating a JPA utility class that adds CRUD capability using JPA support in Netbeans
- Netbeans v6.5 or greater
- Maven Netbeans Plugin
- Derby Sample Database which should have been installed when you downloaded and installed Netbeans
- Completely expand the Test Packages node in the Projects pane and select both the Start.java and TestHomePage.java files
- Right click and select Delete which will open the Delete window.
- Click the Safely delete checkbox so that it is checked and then click the Refactor button. This will remove the 2 files from the project but will not delete them.
- Right click the project node in the Projects pane and select New | Other. This will open the New File window. Now select Persistence from the Categories pane and Entity Classes from Database from the File Types pane as pictured below:
- Click the Next button and in the Database connection drop down menu select the jdbc derby sample database. In the Available Tables list box select Customer an click the Add button which will then add the Customer and the Discount_Code tables to be included in the Selected Tables list box as pictured below:
- Now click the Next button and in the Package text field enter Hibernate for the name of the Java package and then click the Create Persistence Unit button which will open the Create Persistence Unit window.
- From the Persistence Library drop down menu please select Hibernate and then click the Create button which will close the Create Persistence Unit window.
- Now click Next and Finish which will close the New Entity Classes from Database window.
<provider>org.hibernate.ejb.HibernatePersistence</provider>
Please fully expand the Source Packages node in the Project pane. Notice that 2 Pojos, Customer.java and DiscountCode.java, our entity classes, have been added and are located in the hibernate package as pictured below:Double click each Pojo and they will open in Netbeans' Java editor window. Notice how each Pojo has numerous errors which we will now correct by adding libraries to our project using Netbeans' Maven support.
Replacing and adding libraries to our project
We need to replace and add a few libraries to our project. We will use Netbeans Maven support to do this. First, lets replace the hibernate-3.2.5.ga.jar with a more recent version.
Right click the Libraries node in the Project pane and select Add Library. This will open the Add Library window, which will allow us to search for the most recent version of this library. Please follow these steps:
You can also see that there are 2 class tags which identify the Java classes that represent our business entity classes, Customer and DiscountCode:
<class>hibernate.Customer</class>
<class>hibernate.DiscountCode</class>
In addition to the above tags, there are numerous property tags that are used to identify the database driver, username and password, connection url and cache provider. JPA uses these properties at run time as well to connect to the da.
Please fully expand the libraries node in the Project pane and you will notice that the hibernate-3.2.5.ga.jar Hibernate library has been added to our project. Later, we will use Netbeans' Maven support to replace this library with a more recent version.
- In the GroupId text box enter org.hibernate
- In the ArtifactId text box press the space bar on your keyboard. This will open a list of all the Artifacts that are identified by the org.hibernate GroupId we entered.
- From this list select Hibernate.
- In the Version text box press the space bar on your keyboard. This will open a list of all the different versions that are available for this library.
- Select 3.2.6.ga from the list.
- In the GroupId text box enter org.hibernate
- In the ArtifactId text box press the space bar on your keyboard. This will open a list of all the Artifacts that are identified by the org.hibernate GroupId we entered.
- From this list select hibernate-annotations.
- In the Version text box press the space bar on your keyboard. This will open a list of all the different versions that are available for this library.
- Select 3.4.0.GA from the list.
- In the GroupId text box enter org.hibernate
- In the ArtifactId text box press the space bar on your keyboard. This will open a list of all the Artifacts that are identified by the org.hibernate GroupId we entered.
- From this list select hibernate-entitymanager.
- In the Version text box press the space bar on your keyboard. This will open a list of all the different versions that are available for this library.
- Select 3.4.0.GA from the list.
- In the GroupId text box enter javax.persistence
- In the ArtifactId text box press the space bar on your keyboard. This will open a list of all the Artifacts that are identified by the org.hibernate GroupId we entered.
- From this list select persistence-api.
- In the Version text box press the space bar on your keyboard. This will open a list of all the different versions that are available for this library.
- Select 1.0 from the list.
- In the GroupId text box enter org.apache.derby
- In the ArtifactId text box press the space bar on your keyboard. This will open a list of all the Artifacts that are identified by the org.hibernate GroupId we entered.
- From this list select derbyclient.
- In the Version text box press the space bar on your keyboard. This will open a list of all the different versions that are available for this library.
- Select 10.4.2.0 from the list.
- In the GroupId text box enter javax.transaction
- In the ArtifactId text box press the space bar on your keyboard. This will open a list of all the Artifacts that are identified by the org.hibernate GroupId we entered.
- From this list select jta.
- In the Version text box press the space bar on your keyboard. This will open a list of all the different versions that are available for this library.
- Select 1.1 from the list.
- Right click the project node in the Project pane and select New | Other. This will open the New File window.
- Select Persistence from the Categories pane and JPA Controller Classes from Entity Classes from the File Types pane and click Next.
- Click Add All to add the 2 available entity classes to the list of selected entity classes and click Next.
- Select hibernate from the Package drop down menu and then click Finish.
- We can connect to the sample Derby database.
- We can query the Customer table to return all the rows.
- We can get a list of Customer entities as a result from the query and the list contains the correct number of entities.
- In the Services window, expand the Databases node and right click the jdbc derby sample database node and then select Connect to connect to the database.
- Once the connection to the database is made, expand the Tables node, right click the Customer table and select View Data. This will open a SQL Command window.
- Fully expand the Source Packages node in the Projects pane and double click the HomePage.java file which will open the file in the Netbeans Java editor window.
- Add this method to the HomePage class: private void TestDatabase(){ CustomerJpaController custcont = new CustomerJpaController(); int i = custcont.findCustomerEntities().size(); System.out.println("custcont.findCustomerEntities() returned " + Integer.valueOf(i) + " Customer entities"); }
- Modify the HomePage constructor to call our new method. The constructor should look like the following: public HomePage(final PageParameters parameters) { // call out method TestDatabase(); // Add the simplest type of label add(new Label("message", "If you see this message wicket is properly configured and running")); }
- INFO: custcont.findCustomerEntities() returned 13 Customer entities
- We added all the latest versions of the required libraries.
- We configured JPA/Hibernate.
- We added 2 Pojos to represent our domain objects.
- We added CRUD support.
- We created a testing strategy.
- We tested and confirmed that our application works as anticipated.