EJB with NetBeans and Wildfly – part 2

This is the second part in the series about EJB development. In the previous article,  we discussed general concepts of Enterprise Java Beans, and we installed tools needed for application development. We also outlined what we will build as part of the series. In this post, we start actually building our application. Complete source code for this part can be found at Github.

Create the application

First step is creating enterprise application project. In NetBeans, select File>NewProject->Java EE ->EJB Module. Enter application name and select location:


Click “Next” and in the next screen, select Wildfly application server (one that we registered in previous post):



Click “Finish” and NetBeans will generate application files. If you examine application structure, you can see it is typical Java application structure which produces a JAR. This JAR will contain our business logic.

Creating persistence layer

Now that we have application structure ready, we need to set up persistence layer. This includes creating database, configuring data source in application server and creating JPA entities for application.

Create database

The first step is to create database in out Postgresql installation. The easiest way to do this is by using pgAdmin. This is GUI tool for Postgresql administration. Using pgAdmin, create new database called ejb-money . This database will contain tables for our application.

Configure data source in Wildfly

Next thing is to configure data source in Wildfly. Log in to administration console (usually at http://localhost:9990) , and go to Configuration->Subsystems->Data Sources->Non XA. Click “Add” button in the upper right corner.


In the popup dialog, select Postgresql data source. After clicking “Next”, set EJBMoneyDS  as name and java/EJBMoneyDS  as JNDI name.


In the next window, leave default values. In the next dialog window, set database URL to jdbc:postgresql://localhost:5432/ejb-money .


After this, data source is completely configured. It can be accessed using JNDI name java/EJBMoneyDS .

Create JPA entities

In EJB version 3, entity beans are replaced by JPA entities. If you are not familiar with JPA (Java Persistense API), please check out Oracle Java EE tutorial. Basically, JPA is ORM layer that allows us to easily persist POJO classes which represent business objects. For this part of tutorial, I will be using NetBeans wizards for creating persistence entities.

For our application, we will create 2 entities, Account  and Transaction . Account  represent a bank account, and Transaction  will represent a transaction associated with an account.

To create Account entity, right-click the project in NetBeans. Select New->Other->Persistence->Entity Class, like shown in the image bellow:


Click “Next”, and in the next screen, enter Account  as class name and select package. Make sure you check “Create persistence unit” checkbox. This will create persistence.xml  file for our application.


Click “Next”, and in the next screen, enter ejb-money-PU as persistence unit name. For data source name, enter java:/EJBMoneyDS, which we have configure previously in Wildfly:


NetBeans will generate basic code structure for the entity. By default, it will generate ID fields with default generation strategy (for more information about JPA annotations, please this page). We will add two more fields to this entity:

We add fields for name and amount, and set these fields to not be nullable (ie. must have a value). We will use the same wizard to create Transaction  entity. After initial entity is generated, add the following code to it:

This code will create foreign key for transaction which references account ID. We also create amount field, which represents transaction amount.

If you now deploy this code to Wildfly, it will automatically create database tables based on entities. To do this, simply right-click project in NetBeans and choose “Run”. This will deploy module to server and create database tables.

Take a break

We’ll stop for now, since this might be a lot of information to digest. We will continue our journey in next post, where we will start building business logic.

Related posts

  • EJB with NetBeans and Wildfly – part 1

Leave a Comment

Your email address will not be published. Required fields are marked *