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
- Follow the steps for creating an EJB project. After the
Configuration field, click
modify:
- On the Project Facets page, select Java Persistence,
and click Okay:
- Follow the steps to create your EJB project; on the JPA
Facets page,
- 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.
- 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.
- 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.
- For your version of JPA, make the appropriate selection in the Platform
field:
- Create a session bean in your EJB project:
- In the Java™ EE perspective, right-click your project, and select New > Session Bean. The Create EJB 3.x Session Bean wizard appears.
- In the Source folder field, select the source folder for the new bean.
- In the Java package field, type the package name for the new bean.
- 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.
- Select Remote to add a remote interface and select Local to add a local interface, and click Finish.
- Create a JPA entity in your EJB project:
- Right-click your EJB project, and select JPA > Generate entities...
- On the Database Connection page, ensure that the connection and schema are correct, and click Next.
- 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.
- In the Java package field, type or browse to the Java package for your entities.
- Select Synchronize Classes in persistence.xml, if you want to synchronize your entity class with the persistence.xml file.
- 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.
- 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") })
- 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; } }
Parent topic: Creating enterprise beans using wizards

