Java Reference
In-Depth Information
It is a good idea to leave the Use Java Transaction APIs checkbox checked. This will
instruct our JPA implementation to use the Java Transaction API ( JTA ) to allow the
application server to manage transactions. If we uncheck this box, we will need to
manually write the code to manage transactions.
Most JPA implementations allow us to define a table generation strategy. We can
instruct our JPA implementation to create tables for our entities when we deploy
our application, to drop the tables then regenerate them when our application is
deployed, or not create any tables at all. NetBeans allows us to specify the table
generation strategy for our application by clicking the appropriate value in the
Table Generation Strategy radio button group.
When working with a newly created development database, it is a good
idea to select the Drop and Create table generation strategy. This will
allow us to add, remove, and rename fields in our JPA entity at will
without having to make the same changes in the database schema. When
selecting this table generation strategy, tables in the database schema will
be dropped and recreated every time we deploy our application, therefore
any data previously persisted will be lost.
Once we have created our new data source, database connection, and persistence
unit, we are ready to create our new JPA entity.
We can do so by simply clicking on the Finish button. At this point NetBeans
generates the source for our JPA entity.
JPA allows the primary field of a JPA entity to map to any column
type (VARCHAR, NUMBER, etc). It is a best practice to have
a numeric surrogate primary key, that is, a primary key that
serves only as an identifier and has no business meaning in the
application. Selecting the default Primary Key Type of long will
allow for a wide range of values to be available for the primary
keys of our entities.
package com.ensode.jpaweb;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
 
Search WWH ::




Custom Search