Java Reference
In-Depth Information
In this modified version of our JPA entity, we added two fields to be persisted to
the database; firstName will be used to store the user's first name, lastName will
be used to store the user's last name. JPA entities need to follow standard JavaBean
coding conventions. This means that they must have a public constructor that takes
no arguments (one is automatically generated by the Java compiler if we don't
specify any other constuctors), and all fields must be private, and accessed through
getter and setter methods.
Automatically Generating Getters and Setters
In NetBeans, getter and setter methods can be generated
automatically. Simply declare new fields as usual then use the "insert
code" keyboard shortcut (default is Alt+Insert ), then select Getter and
Setter from the resulting pop-up window, then click on the check
box next to the class name to select all fields, then click on the
Generate button.
Before we can use JPA persist our entity's fields into our database, we need to write
some additional code.
Creating a Data Access Object (DAO)
It is a good idea to follow the DAO design pattern whenever we write code that
interacts with a database. The DAO design pattern keeps all database access
functionality in DAO classes. This has the benefit of creating a clear separation of
concerns, leaving other layers in our application, such as the user interface logic and
the business logic, free of any persistence logic.
There is no special procedure in NetBeans to create a DAO. We simply follow the
standard procedure to create a new class by selecting File | New , then selecting
Java as the category and the Java Class as the file type, then entering a name and a
package for the class. In our example, we will name our class CustomerDAO and place
it in the com.ensode.jpaweb package.
At this point, NetBeans create a very simple class containing only the package and
class declarations.
 
Search WWH ::




Custom Search