Home

 

Creating a JPA manager bean

In this section we create a JPA manager bean and JPA entities for the EJB3BANK database. With the logon.jsp open in the editor, go to the Page Data view:

If the server is running and uses the connection to the database, stop the server. Otherwise we cannot connect to the Derby database.

Expand JPA. Right-click JPA Manager Beans and select New Æ JPA Manager Bean.

In the JPA Manager Bean Wizard, click Create New JPA Entities.

For Connection, select the ITSOBANKderby connection. Click Reconnect if not connected already. Select the ITSO schema. Click Next.

In the Generate Entities from Tables dialog:

Type itso.bank.entity as package name.
Select Synchronize Classes in persistence.xml and select all the tables.
Overtype the Transact entity name with Transaction.
Click Finish.

Select the Customer entity and click Next.

For the Customer entity, go through the pages on the left-hand side:

Primary key: Select ssn (preselected).
Query Methods: Remove getCustomerByFirstname, getCustomerByLastname, and getCustomerByTitle.
Relationships: Account (preselected)
Concurrency Control: No Concurrency Control (preselected)
Other: Select all, except Use Resource Injection and Generate JSF Converter for target entity. Click What do these options mean? and you get a description of all the options.

Click Configure Project for JDBC Deployment to open the Set up connections for deployment dialog. Select the ITSOBANKderby connection, ITSO schema, clear Deploy JDBC Connection information to server, and click OK.

Click Finish in the JPA Manager Bean Wizard.

Notice that the three generated JPA entities (Customer, Account, Transaction) do not contain @Table annotations with the ITSO schema:

Open the Customer entity (itso.bank.entity.Customer). Add the @Table annotation after the @Entity annotation:

import javax.persistence.Table;

@Entity

@NamedQueries(....)

@Table (schema="ITSO", name="CUSTOMER")

Open the Account entity, add the @Table annotation, and also add the table and schema in the @JoinTable annotation:

@Entity

@Table (schema="ITSO", name="ACCOUNT")

......

@ManyToMany

@JoinTable(name="ACCOUNT_CUSTOMER",schema="ITSO",

joinColumns=@JoinColumn(name="ACCOUNT_ID"),

inverseJoinColumns=@JoinColumn(name="CUSTOMER_SSN"))

Open the Transaction entity and add the schema to the @Table annotation:

@Entity

@Table (schema="ITSO", name="TRANSACT")

Select Source Æ Organize Imports to resolve the classes

Open the generated itso.bank.entity.controller.CustomerManager. Notice the methods that have been generated for usage in JSF action code:

createCustomer-Persist a new customer entity
deleteCustomer-Delete a customer entity
updateCustomer-Update a customer entity
findCustomer-Find a customer entity by key
getCustomer-Retrieve all customers using a named query
getCustomerOrdered-Retrieve all customers sorted by ssn
getCustomerSelectList-Retrieve a list of ssn for a combo box

The CustomerManager is added to the Page Data view of the logon.jsp.

ibm.com/redbooks