Creating a session bean with JPA entities

You can use the Create EJB 3.x Session Bean wizard to create a session bean and a JPA entity in your EJB project.

Procedure

  1. Follow the steps for creating an EJB project. After the Configuration field, click modify:

    Modify configuration

  2. On the Project Facets page, select Java Persistence, and click Okay:
    Add JPA facet
  3. Follow the steps to create your EJB project; on the JPA Facets page,
    1. For your version of JPA, make the appropriate selection in the Platform field:
      • For JPA 1.0 or 2.0, select RAD JPA Platform.
      • For JPA 2.1, select either Generic or EclipseLink.
    2. In the Connection field, select a connection, or click Add Connection to create a connection. Follow the steps to create a database connection of your choice.
    3. Select Override default schema from connection, and select an alternative schema in the Schema field, if you do not want to use the default schema, and click Finish.
  4. Create a session bean in your EJB project:
    1. In the Java™ EE perspective, right-click your project, and select New > Session Bean. The Create EJB 3.x Session Bean wizard appears.
    2. In the Source folder field, select the source folder for the new bean.
    3. In the Java package field, type the package name for the new bean.
    4. In the Bean name field, type the name that you want to assign to the enterprise bean. By convention, bean names begin with an uppercase letter.
      Note: You can use Unicode characters for the bean name, but Unicode characters are not supported for enterprise bean packages and classes associated with enterprise beans.
    5. Select Remote to add a remote interface and select Local to add a local interface, and click Finish.
  5. Create a JPA entity in your EJB project:
    1. Right-click your EJB project, and select JPA > Generate entities...
    2. On the Database Connection page, ensure that the connection and schema are correct, and click Next.
    3. On the Generate Entities from Tables page in the Source folder field, type a name for your source folder or browse to the path of the folder that contains the Java source file for your entity.
    4. In the Java package field, type or browse to the Java package for your entities.
    5. Select Synchronize Classes in persistence.xml, if you want to synchronize your entity class with the persistence.xml file.
    6. In the Tables field, select the table or tables from which you want to create entities, and click Finish. The Java Editor opens with your JPA entity class.
  6. Create queries in your JPA entity class: Open your JPA entity class in the Java Editor, and you can create queries the retrieve data from the database. For example, here are two simple queries in the entity class:
    @Entity
    @NamedQueries({
    	@NamedQuery(name = "findBySalaryLessThan", query = "SELECT e FROM Employee e WHERE e.salary < :salary"),
    	@NamedQuery(name = "findBySalaryGreaterThan", query = "SELECT e FROM Employee e WHERE e.salary > :salary")
    })
  7. Use your JPA entity in your session bean In your stateless session bean, you can use injection of EntityManager to run the queries that you created in your JPA entity. Open your session bean class in the Java Editor and create an EntityManager that connects to the JPA entity. Here is an example that calls the queries created in a previous step:
    @Stateless
    public class HumanResourcesBean implements HumanResources {
    
    	@PersistenceContext
    	private EntityManager emanager;
    
    	public HumanResourcesBean() {
    	}
    
    	public List<Employee> findBySalaryLessThan(double salary) {
    		Query query = emanager.createNamedQuery("findBySalaryLessThan");
    		query.setParameter("salary", BigDecimal.valueOf(salary));
    
    		@SuppressWarnings("unchecked")
    		List<Employee> result = query.getResultList();
    		return result;
    	}
    
    	public List<Employee> findBySalaryGreaterThan(double salary) {
    		Query query = emanager.createNamedQuery("findBySalaryGreaterThan");
    		query.setParameter("salary", BigDecimal.valueOf(salary));
    
    		@SuppressWarnings("unchecked")
    		List<Employee> result = query.getResultList();
    		return result;
    	}
    }
Icon that indicates the type of topic Task topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: tsessbeanjpawiz.html