Java Reference
In-Depth Information
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
In this modified version of our JPA entity, we added two fields to be persisted to the
database: firstName and lastName , which will be used to store the user's first name
and last name respectively. 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 constructors), and all fields must be private and accessed via public 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, use the keyboard shortcut Alt + Insert ,
select Getter and Setter from the resulting pop-up window, click on
the checkbox next to the class name to select all fields, and click on the
Generate button.
Before we can use JPA to persist our entity's fields into our database, we need to
write some additional code.
Creating a data access object
It is a good idea to follow the data access object ( 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 creates 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.
NetBeans can help us generate JPA controller classes from existing entities. These
JPA controller classes follow the DAO design pattern. To generate a JPA controller
class, we simply need to go to File | New , select the Persistence category, and select
the JPA Controller Classes from Entity Classes file type from the New File dialog.
 
Search WWH ::




Custom Search