<T> T find(Class<T>
Retrieves a row of data
matching the supplied
primary key from the
database table our JPA entity
maps to. The first parameter
must be the class of our JPA
entity, the second parameter
must be the primary key.
class, new Long(2));
<T> T merge(T entity) Updates the data in the
database with the values
contained in the JPA entity's
remove(Object entity) Deletes the row
corresponding to the JPA
entity from the database table
the entity maps to.
Just like the persist() method, the methods listed on the above table must
be invoked inside a transaction, therefore a DAO's method invoking the above
EntityManger methods should follow the pattern in our DAO's persist() method
(lookup EntityManager , begin a transaction, invoke the method, commit the
transaction, look for any exceptions and throw a RuntimeException to roll back the
transaction, if necessary).
If we are working with a JSF application, such as in our example, we would need to
write additional methods on our Controller managed bean that would invoke DAO
methods whenever a user submits a form. Additionally we would have to write
additional JSP pages to create the user interface.
As we will see later in this chapter, NetBeans can automate most of the steps
described in the previous two paragraphs. But before we get there, we will discuss
one great feature of NetBeans: automated generation of JPA entities from an existing
Automated Generation of JPA Entities
In the previous section, we saw how we can automatically create database tables
from JPA entities. This is an optional feature of the JPA specification, however most
JPA implementations implement it. One feature that is not available from JPA is the
converse, generating JPA entities from database tables. Luckily for us, NetBeans
provides this functionality.