JPA2
JPA2
1. Create a new entity class by selecting the option Entity Classes from Database… as
shown in the screen shot below:
2. A dialog box as shown below is displayed on the screen. You have to either select a
stored connection (if you have already created other entities for the same project) or
else select New Database Connection… and follow the wizard to create a new
database connection.
3. It is very important that when a new database connection is created, the appropriate
database is selected as shown in the screen shot below:
5. Select or type in a package where to store the entity classes and click on finish.
6. When you click on finish notice that an entity class is created together with a
persistence.xml file in a META-INF sub-directory directory under the source package
directory as shown in the screen shot below. This file will be explained later in this
tutorial.
The following is the code (excluding the package declaration and the imports) that was
generated in the above example for the Flights table:
@Entity
@Table(name = "flights")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Flights.findAll", query = "SELECT f FROM Flights f"),
@NamedQuery(name = "Flights.findById", query = "SELECT f FROM Flights f WHERE f.id = :id"),
@NamedQuery(name = "Flights.findByFlightNum", query = "SELECT f FROM Flights f WHERE f.flightNum = :flightNum"),
@NamedQuery(name = "Flights.findByFlightDate", query = "SELECT f FROM Flights f WHERE f.flightDate = :flightDate"),
@NamedQuery(name = "Flights.findBySeatsLeft", query = "SELECT f FROM Flights f WHERE f.seatsLeft = :seatsLeft")})
public class Flights implements Serializable {
public Flights() {
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Flights)) {
return false;
}
Flights other = (Flights) object;
if ((this.id == null && other.id != null) || (this.id != null &&
!this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entities.Flights[ id=" + id + " ]";
}
}
The following is the SQL code that was used to create the flights database table on the
MySQL server:
The entity class is following the rules of encapsulation and therefore all attributes are set to
be private and getters and setters are used to provide access to them. An empty and
parameter less constructor is included and the entity class is also implementing the
Serializable interface.
Note that although every item in the table is mapped to a Java POJO, the Java naming
convention is used in the Java entity. For this reason, fields such as flight_num where
written as flightNum in the Java entity class. To indicate the mapping (as different naming
convention is used) notice the use of the tags @Column and @Table both with the name
field for the table fields and table name respectively. Other tags such as @Id and
@GeneratedValue as used to indicate which are the primary fields and the method of auto
filling them with values.
There are a lot of annotations that can be used with JPA 2.0. For a full list of annotations
and their respective explanation, consult with the official Oracle Java EE 6 tutorial
http://docs.oracle.com/javaee/6/tutorial/doc/javaeetutorial6.pdf.
2. Type in the name of the entity and the appropriate primary key type as shown in the
screen shot below.
3. The outline of the entity class is automatically generated. The attributes of this
entity should now be coded including the relevant JPA annotations, getters, setter
and constructors.
The following source code illustrates and example of an entity (excluding the package
declaration and the imports) that was created from scratch and it will be used to create a
new SQL table in the database. Notice that only few annotations were used in this example:
@Entity
@Table(name="passengers")
public Passenger(){
// Empty parameter less constructor
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Passenger)) {
return false;
}
Passenger other = (Passenger) object;
if ((this.id == null && other.id != null) || (this.id != null &&
!this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entities.Passenger[ id=" + id + " ]";
}
}
The above screen shot represents a simple GUI that can be used to configure the
persistence.xml file. This GUI is a mapping to an xml file (click on source to view the file) that
is shown below:
The tag persistence-unit name is used to define an identifier for this particular persistence
application. The provider tag is used to indicate the persistence unit provider. In the above
example the provider is the EclipseLink however other such as Hibernate can be used. The
list of class tags lists the entity classes. The property tag includes a number of very
important properties such as the database url, username, password and driver. The
property <property name="eclipselink.ddl-generation" value="create-tables"/>
is used to allow the persistence unit to create new SQL tables in the database from a given
entity class.
EntityManagerFactory emFactory =
Persistence.createEntityManagerFactory("JPA_DemoPU");
EntityManager em = emFactory.createEntityManager();
Query query = em.createNamedQuery("Flights.findByFlightNum");
query.setParameter("flightNum","KM102");
List<Flights> result = query.getResultList();
// ...
em.close();