Binding Database Table to a Table Component Using JPA
This walk through is based on Netbeans 6.0 IDE
with Visual Web Pack. Download it from
here
Add Glassfish (Application Server) to the IDE
- In the Runtime tab click on the IDE and select Add Server
- In the dialog point to the directory of Glassfish
- Download Glassfish from
here.
- Use "adminadmin" as password.
Create the model project
- Click on the Create project icon in the toolbar (second
button)
- Select Java -> Java Class Library
- Project Name: TestModelApp
- Package: com.samples.model
Create the Web project
- Click on the Create project icon in the toolbar (second
button)
- Select Web -> Web Application
- Project Name: TestWebApp
- Package: com.samples.web
- Add the project TestModelApp as dependent project to Web
Project
- Right Click on the project node and select "properties"
- In the dialog click on the "Library" node in the left
- In the right hand side click on the add project and
select the location of "TestModelApp"
Create the Database Connection
- Select the "Runtime" tab
- Open the Databases Node and right click on the Drivers Node
and select menu item "New Driver"
In the resulting dialog select your MySql connextor/j JDBC
driver
- Right Click on the Databases Node and select "New
Connection"
- In the Name combobox select MySql (Connector/J) and then
Provide the necessary connection parameters. For Ex
- Database URL:jdbc:mysql://localhost/sample
- User Name: sample
- Password: sample
- Open the table node in the added connection and make sure
the connection is correct.
Note: In this walk through, it is assumed that
you already have a "sample" MySQL database that has one table called
"Users". The table "Users" has four columns "user_id" (Primary Key,
Type: INTEGER), userName, password, email_address (all of type
String).
Create the Entity Class from Database in the
Model Project
- Right click on the project "TestModelApp" and select New ->
other -> Persistence -> Entity Classes from Database
- Select Database Connection "Sample" and select "Users" as
the Selected Tables
- In the next tab of the wizard click on the Create
Persistence Unit and call it "samplePU"
- In the Entity Bean Tab of the Wizard provide
- ClassName: User (IDE may suggest
Users)
- Package: com.samples.model
- Check that the persistence Unit is created correctly
- Open Source Packages -> META-INF and double click on the
Persistence.xml and check that the file has been created
with persistence-unit element as
shown below.
<persistence-unit name="samplePU" transaction-type="RESOURCE_LOCAL">
<provider>
oracle.toplink.essentials.PersistenceProvider
</provider>
<class>com.samples.model.User</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost/sample"/>
<property name="toplink.jdbc.user" value="sample"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.password" value="sample"/>
</properties>
</persistence-unit>
Create the Entity Controller Class
- In the package "com.samples.model" create a class called "UserController"
and add the code
private EntityManagerFactory emf;
private EntityManager getEntityManager() {
if(emf == null){
emf = Persistence.createEntityManagerFactory("samplePU");
}
return emf.createEntityManager();
}
public Users[] getUsers() {
EntityManager em = getEntityManager();
try{
Query q = em.createQuery("select c from Users as c");
return (User[]) q.getResultList().toArray(new User[0]);
} finally {
em.close();
}
}
Bind Entity Bean to the Table Component
For this first we need to create a property that returns the
array of User objects and then bind that array to the Table
Component. This User[] array will be initialized using JPA
Entity Manager.
- Add a Property called users to
the Session Bean.
- Double click on the
SessionBean1 node in the outline window and open
it in the Java Editor.
- Type the line private User[] users;
- Right click on this line and select the action
"Generate Code"
- In the resulting popup menu select "Getter and
Setter"
- Add a method called updateUsers()
as below that will be used to initialize the
users property.
public void updateUsers(){
UsersController usersController = new UsersController();
users = usersController.getUsers();
}
- Fix the needed import for UserController
- Now call the initialization method updateUsers() in the
init() of
Sessionbean1.java
updateUsers();
- Compile the project (Note: This is a
needed step)
- Open the Page1.jsp in the designer
- Note: if the designer is already
opened click on the refresh button
- Drag and drop a Table Component
on to the designer.
- Click on the Table Component and select
users as the binding array.
- Adjust the columns to be displayed if needed
- Deploy the project and the Table Component should display
the data as
