Java Reference
In-Depth Information
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
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 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 spec-
ify any other constructors), 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 DAO
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 patterns keep 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.
Search WWH ::




Custom Search