In this blog I will add relevant materials for the courses I teach at the University of Coimbra, Portugal

segunda-feira, 27 de outubro de 2014

An Enterprise Application Repository to Access the Players Table

In my previous message, I explained how you could create a stand-alone JPA project using EclipseLink. Now, I'm going to pick this project and turn it into a part of a more complete Enterprise Application Repository. Overall, we are going to need four different projects, all of them interconnected:

I will not go into details on this code in this message, because I gave some in the previous one. You may simply copy the files from one project to the other, if you red the previous example. The one thing that is different is the persistence.xml file. It looks like this:

Notice the reference to the database in the javax.persistence.jdbc.url. You may use exactly the same database as in the previous example. It will be useful to fill data for us here. We are going to use the Java Transactions API (TPA) and we need to refer to the jta-data-source. The name we put here (java:/PlayersEAR) must exist in a configuration file of WildFly. It could be standalone.xml, or standalone-full.xml if you need Java Message Service. Look for the tag <datasources> to insert this piece of configuration:

The EJB Project

Our next step is to create the Enterprise JavaBeans project. We will need a new specific "EJB Project" for that:

We need to add a reference to the previous ServerPlayers-JPA to refer the Player class in this project (see the source code, after this picture):

We will write a very simple Enterprise JavaBean (EJB). It contains two methods: one to populate the database, the other to search for players taller than a given value. This class refers an EJB remote interface, which we present afterwards. To create the bean and the respective interface, you need to do File-->New-->Session Bean, as in the figure.

The EJB implementation. You may want to take a longer more detailed look at the query:

The Dynamic Web Project

Now, we will provide a web access to the EJB, through a servlet. For this we must first create a Dynamic Web Project (via File-->New):

Again, we need to refer to the other two projects (more on this below):

It would be better to use Data Transfer Objects between the EJB and the presentation layer, to ensure a strict separation between the presentation layer and the data layer, as they are not adjacent. A library like MapStruct may help here. This would make the example more complicated. Maybe in the future I will ensure this strict separation.

I must say that this is not the most elegant code I've ever written, as I control the operation I want using two URLs and I have an HTTP GET command changing the state of the system (it should be a POST). Anyway, the get accepts two commands:

http://localhost:8080/ServerPlayers-Web/PlayersTallerThan?height=1.80, where 1.80 might be changed to the "height" threshold you want to set. But before we are able to do this, we must populate the database with:

http://localhost:8080/ServerPlayers-Web/PlayersTallerThan?fill=1

The parameter "fill" can actually have any value, the code will still work.

The Enterprise Application Repository (EAR) Project

Before we can put anything to run, we must create and deploy an EAR project. We start with File-->New--> Enterprise Application Project:

Once you push "Next" you are prompted to select the projects that compose the EAR. In our case we have the following three:

Once this step is finished, we may deploy the whole package, as follows (use the right button of the mouse):

Select the appropriate location in the deployments directory of the application server:

I assume that WildFly was running. Once you do the deployment, you should see the following output in WildFly, with the references to our project: