Java Persistence API (JPA) is a standard API that defines mapping between database tables and Java classes. These POJOs can then be used to perform all the database operations using Java Persistence Query Language (JPQL) which is a string-based SQL-like syntax or a type-safe Criteria API. Both JPQL and Criteria API operate on the Java model instead of the database tables.
ノート
This section will generate JPA entities from a sample database and customize them to be more intuitive for Java developers.
Click on “Next>”.
The database table name and the corresponding mapped class name is shown in the “Class Name” column and can be changed here, if needed.
ノート
Click on “Finish” to complete the entity generation.
In NetBeans, expand “Source Packages”, org.glassfish.samples.entities, and double-click Customer.java.
ノート
Notice the following points in the generated code:
- The generated class-level @NamedQuery annotations uses JPQL to define several queries. One of the queries is named “Customer.findAll” which maps to the JPQL that retrieves all rows from the database. There are several “findBy” queries, one for each field (which maps to a column in the table), that allows to query the data for that specific field. Additional queries may be added here providing a central location for all your query-related logic.
- The bean validation constraints are generated on each field based upon the schema definition. These constraints are then used by the validator included in the JPA implementation before an entity is saved, updated, or removed from the database.
- The regular expression-based constraint may be used to enforce phone or zipcode in a particular format.
- The zip and discountCode fields are marked with the @JoinColumn annotation creating a join with the appropriate table.
ノート
This section will customize the generated JPA entities to make them more intuitive for a Java developer.
Replace the following code:
@Size(max = 30)
@Column(name = "ADDRESSLINE1")
private String addressline1;
@Size(max = 30)
@Column(name = "ADDRESSLINE2")
private String addressline2;
@Size(max = 25)
@Column(name = "CITY")
private String city;
@Size(max = 2)
@Column(name = "STATE")
private String state;
and
@JoinColumn(name = "ZIP", referencedColumnName = "ZIP_CODE")
@ManyToOne(optional = false)
private MicroMarket zip;
with
@javax.persistence.Embedded private Address address;
Click on the yellow bulb in the left bar to create a new class in the current package as shown:
ノート
Notice the following points:
- The two blocks of code above are not adjacent.
- Copy/pasting only the fields will show a red line under some of the methods in your entity but will be fixed later.
- The @Embedded annotation ensures that this field’s value is an instance of an embeddable class.
@javax.persistence.Embeddable
public class Address implements java.io.Serializable {
Fix all the imports by right-clicking in the editor, selecting “Fix Imports...”, and taking all the defaults.
public Address getAddress() { return address; }
public void setAddress(Address address) { this.address = address; }
Here is one of the updated query:
@NamedQuery(name = "Customer.findByCity", query = "SELECT c FROM Customer c WHERE c.address.city = :city")
@Override
public String toString() {
return name + "[" + customerId + "]";
}
This will ensure that the customer’s name and unique identifier are printed as the default string representation.