EJB with NetBeans and Wildfly – part 3

In previous post, we managed to set up our EJB application and configure persistence features. We created couple of JPA entities which represent our business objects. In this part of the tutorial, we will create beans which will contain application business logic.

We will create stateless session bean which will allow us to create an account, deposit or withdraw funds, and see account balance. We will also create another bean which will allow us to see transactions for the account.

Create beans structure

We will want to access our beans remotely from any kind of client, be it from command line, GUI client or web service. In order to support that, our beans must have remote interface. Remote interface allows us to access EJBs outside of the container. More detailed explanation of remote interface can be found in Oracle JEE tutorial. To create remote interface, we simply annotate regular Java interface with @Remote  annotation:

Once again, we will use NetBeans wizard to generate code for the our beans. Right-click ejb-money project and select New->Java EE->Session Bean.


Click “Next”, and in the next window fill out data for session bean.


This will create local bean, which we will need to change to remote. Modify the source code of the AccountBean  to look like bellow:

This is the default code generated by NetBeans, so don’t worry that it does nothing so far. We will change it later.

In order to create TransactionBean, repeat all the steps above. It should result in the following code (both interface and bean):


Implementing business logic

Now that we have complete structure for out beans, it’s time we add business logic, so our methods actually do something. Since we interact with database, the first thing we need to do is inject JPA EntityManager  into beans.

We do this by simply annotating EntityManager  field with @PersistenceContext . EJB container will automatically inject correct object when bean is registered. We also need to specify persistence unit name to which this entity manager is bound.

Implementing database operations is trivial. Complete AccountBean  source code is shown bellow:

Now we are ready to deploy beans to Wildfly. In NetBeans, right-click ejb-money project, and select “Run”. This will deploy beans to registered Wildfly instance. You can verify that beans are deployed correctly through  “Runtime” tab of the administration  interface.

Accessing the beans

Now that beans are deployed, we need a way to interact with them. We will cover this topic in the next installment of the tutorial. Until then, keep reading about Enterprise Java Beans!

Complete source code for this tutorial can be found on Github.


2 thoughts on “EJB with NetBeans and Wildfly – part 3

  1. When you build an enterprise application in the IDE, the EJB and web application modules are packaged in an EAR archive that is then deployed to the server.

Leave a Reply

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