First I created a project in Netbeans. I picked Java Library. Then I selected create entity classes from database and used the sample JavaDB database that comes with Netbeans. It prompted me to create the persistence.xml and I accepted the defaults.
Next I created a Web project in Netbeans. During the project creation wizard, I selected Visual JSF to include the JSF and Woodstock libraries.
Next I clicked on the properties for the project and added my library to the project.
Next I created a created a new persistence.xml file. After it was created, I modified it so that it would include the specific entity classes that I created in the library project.
Note: This is required otherwise the entity classes will not be detected.
Next I added this code snippet to SessionBean1
@PersistenceUnit(unitName="SamplePersistenceWebPU")This fetches my data so that I can display it later in a table.
EntityManagerFactory emf;
public Customer[] getCustomers() {
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("SELECT c FROM Customer c");
Listlist = q.getResultList();
return list.toArray(new Customer[list.size()]);
}
Next I compile the project (no deployment yet). This refreshes the beans so that I can manipulate them visually.
Next I create a table on Page1 from the palette. I right click the table and select binding and point it from the DefaultTableProvider to customers in the SessionBean.
Now I deploy it. Poof the magic is complete.
The link to my example Netbeans project: SampleJPAPersistence.zip
11 comments:
Post a Comment