Apache TomEE is a nice small fully certified JavaEE 6 web profile container. In this blog I will show you how to configure netbeans IDE 7.3 beta 2 to work with TomEE Web Profile 1.5. To learn more about TomEE and to download the server you can visit the following link
http://tomee.apache.org/apache-tomee.html
And you can get the latest version of Netbeans from its site at
http://netbeans.org
Now first of all I would like to tell you both TomEE works great Netbeans. There are couple of posts which shows about creating things like EJB and CDI beans in TomEE and Netbeans. But what I could not found in the web is a guide to how to make JPA work in TomEE with Netbeans. So I would try to demonstrate the same in this post.
So to begin lets just create a Java Web project in Netbeans (I am assuming you have basic working knowladge with netbeans and you know how to create and run a web project in netbeans). In the server screen we can add TomEE like the given screenshot below.
|
Screenshot 1: Select Tomcat as server |
Do remember that TomEE is also tomcat so we can easily choose to add the TomEE server as a Tomcat Server. I have given it the name Apache TomEE 1.5 for distinguishing it from the other servers that I have. Now hit next. In the next step you need to select the folder where the TomEE server is present.
|
Screenshot 2: Select the path of TomEE server |
As soon as you hit finish your work is done. Netbeans would add the manager user in the tomcat server and also you will get back to the project creation window. Continue to the step for choosing your favorite web framework and complete the project creation process. For this demo I had chosen JSF. By default this would give a sample page. You can just add a few pages and try running the server to see that everything is fine.
Once you are satisfied with this part. Let us move to the point where we will do the more interesting part. We will configure OpenJPA in Netbeans so that the netbeans Entity generator can nicely work work OpenJPA and TomEE. So to do that you need to first create a connection to a database. For this demo I created one for my myql server as shown in the screenshot below.
|
Screenshot 3: Add Database Connection |
From your netbeans IDE choose Services tab. From there choose the mysql driver. Right click on it and from the menu choose "connect using". This would open the dialog box shown in the screenshot 3. There give the appropriate parameters and hit finish to complete the connection. So once this connection is there we are ready to create Entity classes from the selected database. To do that follow the menu as shown in the screenshot below.
|
Screenshot 4: Add Entity Classes from Database |
Once you select "Entity Classes from Database" You will be presented with the follow dialog
|
Screenshot 5: Tables Selection |
In this dialog first of all we would select the connection that we created before from the top dropdown. Once you select the connection Netbeans would bring all the tables in "Available Tables" list. You can hit "Add All" to select all tables for entity class generation. Now lets hit "Next" and go to the next screen. And then again hit next and in the next screen hit finish. You can accept all the defaults that are given by the system and finish the Entity creation wizard. With this you are done creating the entities. But as soon as you are done creating the entities You will get a failed message from TomEE server (In case your TomEE server is running). This is because by default Netbeans generate Entity classes using Eclipselink which is not present in TomEE. So to make everybody happy. Next we will configure OpenJPA. To do that you need to open the persistence.xml file from the project (Project explorer -> double click on persistence.xml). This would open the persistence.xml editor window. As shown below
|
Screenshot 6: Add New Persistence Library |
Now in this window we will select "New Persistence Library" option from the Persistence Library dropdown. This will open the following dialog box.
|
Screenshot 7: Add Persistence Library |
Here you need to choose the following two jar files from the TomEE installation folder - javaee-api-6.0-4-tomcat.jar and openjpa-asm-shaded-2.2.0.jar. Do make sure that you are choosing the jar files from the same location where your TomEE server is. Once you choose the proper libraries hit Ok to close the dialog and go back to the persistence.xml editor screen of netbeans.
|
Screenshot 8: Choose Open JPA as persistence library |
Now here from the persistence library choose OpenJPA and save the file as shown in the above screenshot. Now just restart the server once and you are all set to run TomEE with JPA.
Wait the post is not yet completed. One last thing to add. You must enable JSP hot deployment by editing the TomEE servers web.xml (present in
/conf/web.xml).
jsp
org.apache.jasper.servlet.JspServlet
....
development
true
....
You need to change the value of development to true.
Now you are all set. Happy coding :)
Comments