Sunday, May 18, 2014

Java EE7 and Maven project for newbies - part 3 - defining the ejb services and jpa entities modules #javaee #java #maven #juniordevs


      We are resuming for the third part, we already have a parent pom and we have already defined the pom for our war module. In our original setup we have defined that our application is going to include a services jar, in the form of an ejb jar. This where our Enterprise Java Beans are going to be, specifically the Session Beans. We had also defined another module (layer) that is going to host the Entity Beans (Database representation beans), the so called domain model.

      Defining the services (ejb) module

      Under the parent pom folder, we create a new sub-folder, like we did with the war module. In this folder we create a pom.xml file with the following contents.The name of the folder is sample-services. The pom looks like this. Eventually this pretty much it, for now. 

      Remember that we have already defined in the dependency management section of our parent pom, the version of the javaee-api jar and there is also in the plugin management section a maven plugin that is going to take care of the specific packaging our ejb.jar requires. It is the maven-ejb-plugin. Go back to the parent pom and search for the 2 above points. Because of all these elements defined in the parent pom , our ejb service pom looks very minimal. Maven by convention is going to take care most of the stuff. The maven ejb plugin is going to kick in since we have defined that the packaging required for this module is 'ejb'.

      Our project structure looks like this

      Defining the entity beans (ejb) module

       Under the parent pom folder, we create a new sub-folder, like we did with the previous ejb module. We will name it sample-domain. This is the module that we will code our Database representation beans, the so called Entity beans, following the JPA2 specification. 

      The pom looks fairly simple.

      The packaging is still ejb, since it is going to host EJB classes, the so called Entity Beans

      There is another thing we need to package along, since this module is going to 'host' our domain objects, this is an XML descriptor called persistence.xml, which defines the Data source that our application is going to connect to. In Java EE 7, this file has been simplified a lot and we can even skip the the definition of the datasource, since there already one default. Have a look here. From the packing point of view, which we are more interested right now, what you need to do, is under the folder src/main/resources to create a new folder called META-INF and in there to place the persistence.xml  file, like in the image below.

      The contents of the persistence.xml at this point are not relevant (we will focus on the on the next posts), you can look up a sample on this post(s) git branch.

      A note here regarding folder creations, if you add Maven modules using an IDE e.g Eclipse or IntelliJ, once you create a new Module and you define a POM the IDE automatically creates the standard layout folders that your module is supposed to have, according to the Maven conventions. If you follow theses post and you write your code using with a simper tool e.g a simple text editor, then you need to create the src / main  folder structures on your own.

      That is all for this post, we have added 2 more modules for our application, but we are still missing the one that is going to package them all, this is the ear module. We have also not covered the 'inter-dependencies' of our modules this is something we are going to do, in the next 'ear' dedicated post, where all come together.

      The code for these simple poms can be found on bitbucket project, under the tag post3.

      Continue to part 4


      1. Nice post it is very useful for me.
        Each and every year we are providing Cheap and best students project at Madurai.

      2. This comment has been removed by a blog administrator.