Home » Enterprise Java » ejb3 » Stateless Session Bean » Stateless Session Bean to persist Entity

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Stateless Session Bean to persist Entity

This is an example of how to create a Stateless Session Bean to persist an Entity. A stateless session bean does not maintain state with the client. When a client invokes the methods of a stateless bean, the bean’s instance variables may contain a state specific to that client, but only for the duration of the invocation. When the method is finished, the client-specific state should not be retained. Clients may, however, change the state of instance variables in pooled stateless beans, and this state is held over to the next invocation of the pooled stateless bean. Except during method invocation, all instances of a stateless bean are equivalent, allowing the EJB container to assign an instance to any client. That is, the state of a stateless session bean should apply accross all clients.

The Data Transfer (Domain) object

Class Employee is an entity, that is a lightweight persistence domain object. Typically an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The persistent state of an entity is represented either through persistent fields or persistent properties. These fields or properties use object/relational mapping annotations to map the entities and entity relationships to the relational data in the underlying data store. The class is annotated with the javax.persistence.Entity annotation, it has a public or protected, no-argument constructor and it implements the Serializable interface.

package com.javacodegeeks.snippets.enterprise;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Employee implements Serializable {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
    private String surname;
    private String title;
    private Double salary;
    private Date created;
    
    public Long getId() {

  return id;
    }
    public void setId(Long id) {

  this.id = id;
    }
    
    public String getName() {

  return name;
    }
    public void setName(String name) {

  this.name = name;
    }
    
    public String getSurname() {

  return surname;
    }
    public void setSurname(String surname) {

  this.surname = surname;
    }
    
    public String getTitle() {

  return title;
    }
    public void setTitle(String title) {

  this.title = title;
    }
    
    public Double getSalary() {

  return salary;
    }
    public void setSalary(Double salary) {

  this.salary = salary;
    }
    public Date getCreated() {

  return created;
    }
    public void setCreated(Date created) {

  this.created = created;
    }
    
    @Override
    public String toString() {

  return "Employee [id=" + id + ", name=" + name + ", surname=" + surname


    + ", title=" + title + ", salary="+salary+ ", created=" + created+"]";
    }

}

The EmployeeService EJB implementation class

The EmployeeService EJB implementation class is an EJB implementation class that is a stateless session bean and has a reference of the CalculatorService EJB. It uses the javax.persistence.EntityManager interface, that creates and removes persistent entity instances, finds entities by the entity’s primary key, and allows queries to be run on entities. The EntityManager is annotated with the javax.persistence.PersistenceContext annotation to express a dependency on the container-managed EntityManager and its associated persistence context, using the name of the persistence unit as defined in the persistence.xml file. The EmployeeService also has a local and a remote interface.

package com.javacodegeeks.snippets.enterprise;

import java.util.Collection;
import java.util.Date;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {

    @PersistenceContext(unitName = "TutorialPU")
    EntityManager entityManager;

    public EmployeeService() {
    }

    public Employee createEmployee(String name, String surname, String title, double salary) {

  Employee employee = new Employee();

  employee.setName(name);

  employee.setSurname(surname);

  employee.setTitle(title);

  employee.setSalary(salary);

  employee.setCreated(new Date());


  entityManager.persist(employee);


  return employee;
    }

    public void removeEmployee(long id) {

  Employee employee = findEmployee(id);

  if (employee != null) {


entityManager.remove(employee);

  }
    }

    public Employee promoteEmployee(long id, String newTitle, double newSalary) {

  Employee employee = entityManager.find(Employee.class, id);

  if (employee != null) {


employee.setTitle(newTitle);


employee.setSalary(newSalary);

  }

  return employee;
    }

    public Employee findEmployee(long id) {

  return entityManager.find(Employee.class, id);
    }

    public Collection<Employee> findAllEmployees() {

  Query query = entityManager.createQuery("SELECT e FROM Employee e");

  return (Collection<Employee>) query.getResultList();
    }

}

The EJB local interface (suitable for in VM communication)

package com.javacodegeeks.snippets.enterprise;

import java.util.Collection;

import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {

    public Employee createEmployee(String name, String surname, String title, double salary);
    public void removeEmployee(long id);
    public Employee promoteEmployee(long id, String newTitle, double newSalary);
    public Employee findEmployee(long id);
    public Collection<Employee> findAllEmployees();
    
}

The EJB remote interface (suitable for intra VM communication)

package com.javacodegeeks.snippets.enterprise;

import java.util.Collection;

import javax.ejb.Remote;

@Remote
public interface EmployeeServiceRemote {
    
    public Employee createEmployee(String name, String surname, String title, double salary);
    public void removeEmployee(long id);
    public Employee promoteEmployee(long id, String newTitle, double newSalary);
    public Employee findEmployee(long id);
    public Collection<Employee> findAllEmployees();
    
}

The persistence.xml file driving the JPA framework

<persistence xmlns="http://java.sun.com/xml/ns/persistence"


 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
  <persistence-unit name="TutorialPU" >

  <jta-data-source>java:/DefaultDS</jta-data-source>

  <properties>

   <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

  </properties>
<!--
    <properties>

<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>

<property name="hibernate.hbm2ddl.auto" value="update"/>

<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>

<property name="hibernate.connection.username" value="sa"/>

<property name="hibernate.connection.password" value=""/>

<property name="hibernate.connection.url" value="jdbc:hsqldb:data/tutorial"/>
    </properties>
-->    
  </persistence-unit>
</persistence>

The application.xml file describing the modules in the .ear archive

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" 


 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  


 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"


 version="1.4">
  <display-name>ExampleEJB3</display-name>

  <module>
     <java>exampleEJB3-persistence.jar</java>
  </module>

  <module>
     <ejb>exampleEJB3.jar</ejb>
  </module>

</application>

The structure of the .ear archive

exampleEJB3.ear
 |
 |_exampleEJB3-persistence.jar 
 |  |_com
 |  |  |_javacodegeeks
 |  |     |_snippets
 |  |
  |_enterprise
 |  |
     |_Employee.class
 |  |_META-INF
 |     |_persistence.xml
 |
 |_exampleEJB3.jar
 |   |_com
 |   |  |_javacodegeeks
 |   |     |_snippets
 |   |
  |_enterprise
 |   |
     |_EmployeeService.class
 |   |
     |_EmployeeServiceLocal.class
 |   |
     |_EmployeeServiceRemote.class
 |   |_META-INF
 |
 |_META-INF
    |_application.xml

A simple client example

In EmployeeServiceClient we connect to JBoss naming service running on local host and on default port 1099. We use the Context to set the configuration for the JBoss server, such as Context.INITIAL_CONTEXT_FACTORY, Context.PROVIDER_URL and Context.URL_PKG_PREFIXES. We get the EmployeeService EJB using the lookup(Name name) method of Context to invoke its methods.

package com.javacodegeeks.snippets.enterprise;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;


public class EmployeeServiceClient {

    public static void main(String[] a) throws Exception {


  /*

   * Connecting to JBoss naming service running on local host and on

   * default port 1099 the environment that should be created is like the

   * one shown below :

   */

  Hashtable env = new Hashtable();

  env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

  env.put(Context.PROVIDER_URL, "jnp://localhost:1099");

  env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

  

  Context ctx = new InitialContext(env);

  

  // We get a reference of the remote EJB interface to invoke its business methods

  EmployeeServiceRemote employeeService = (EmployeeServiceRemote) ctx.lookup("exampleEJB3/EmployeeService/remote");

  

  Employee employee = employeeService.createEmployee("Byron", "Kiourtzoglou", "Master Software Engineer", 2000d);

  

  long employeeId = employee.getId();

  

  System.out.println(employeeService.findEmployee(employeeId));

  

  employeeService.promoteEmployee(employeeId, "Principal Software Engineer", 3000d);

  

  System.out.println(employeeService.findEmployee(employeeId));

    }

}

Output:

Employee [id=1, name=Byron, surname=Kiourtzoglou, title=Master Software Engineer, salary=2000.0, created=2011-12-03 17:31:30.203]
Employee [id=1, name=Byron, surname=Kiourtzoglou, title=Principal Software Engineer, salary=3000.0, created=2011-12-03 17:31:30.203]

 
This was an example of how to create a Stateless Session Bean to persist an Entity.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of