Enterprise Java

Java EE7 and Maven project for newbies – part 3 – defining the ejb services and jpa entities modules

Resuming from the previous parts

Part #1
Part #2

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.

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>gr.javapapo</groupId>
        <artifactId>sample-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>sample-services</artifactId>
      <name>sample-services</name>   
       <description>EJB service layer</description>
      <packaging>ejb</packaging>
     <build>
            <finalName>${project.artifactId}</finalName>
      </build>
            <dependencies>
                    <dependency>
                            <groupId>javax</groupId>
                            <artifactId>javaee-api</artifactId>
                    </dependency>
            </dependencies>
    </project>

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:

CapturFiles

 

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.

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>gr.javapapo</groupId>
        <artifactId>sample-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>sample-domain</artifactId>
      <packaging>ejb</packaging>
      <name>sample-domain</name>
      <description>JPA entities for our project - domain model</description>
      <build>
            <finalName>${project.artifactId}</finalName>
      </build>
      <dependencies>
                    <dependency>
                            <groupId>javax</groupId>
                            <artifactId>javaee-api</artifactId>
                    </dependency>
            </dependencies>
    </project>

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.
 

CapturFiles_2

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.

 

Paris Apostolopoulos

Paris is a senior software engineer focusing on J2EE development, loves Business process modelling and is keen on software quality challenges. He is passionate about Java and Java communities. He is a co-founder and administrator of the first Java User Group in greece(JHUG.gr) and occasional speaker on meet-ups and seminars and regular blogger. For his contributions and involvement on the Java community he has been awarded the title of Java Champion in 2007 by Sun Microsystems.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button