Time to complete this step: 15 minutes
The Oracle Enterprise Pack for Eclipse (OEPE) provides a powerful and flexible object relational mapping interface to popular persistence services like OpenJPA. Depending on the development scenario, OR Mappings can be generated through two different mechanisms.
You will perform the following tasks in this step:
In this step, we will use OEPE to automatically generate JPA entity beans from an existing database schema by reverse engineering the schema. OEPE will generate Java classes with the appropriate accessors and JPA annotations.
A technician can have references to tickets but not ticket history. Hence to avoid bi-direction association between technician and ticket history, un-check the option Generate a reference to a collection of TICKETHISTORY in TECHNICIAN.
OEPE also supports the creation of new entity associations in case your database lacks foreign key definitions (such as for performance reasons). It can create Simple Associations (one to one, one to many, many to one) between two tables and Many to Many Associations through an intermediate table.
When we create entity mappings, we define each property as having one of six property types: basic, id, many-to-one, one-to-one, one-to-many, and many-to-many. When you generate entities from a database, OEPE annotates the generated source code with JPA annotations that designate which type a given property is. In this section, we will review some basic JPA annotations.
Review the following annotations in the Technician class.
Every non-static non-transient property (field or method) of an entity bean is considered persistent. Not having an annotation for property is equivalent to the appropriate
@Basic
annotation. The
@Basic
annotation allows to declare the fetching strategy for a property.
The
@Basic
annotation is followed by the
@Column
annotation defining an attribute name which is the name of the column to which the property is bound; the attribute nullable is false to specify that the column cannot store null values; the column length attribute specifies the maximum length.
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
An
@Id
annotation is followed by a
@Column
annotation defining the attribute unique which is true to specify that the column is UNIQUE in the SQL sense (can have only unique values).
private String id;
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
The following shows the one-to-one mapping association between
Technician and
Technicianprivatedata in the
Technicianprivatedata
class. The following
@JoinColumn
annotation declares the column in the targeted entity that will be used for the join.
//bi-directional one-to-one association to Technician
@OneToOne
@JoinColumn(name="ID")
private Technician technician;
public Technician getTechnician() {
return this.technician;
}
public void setTechnician(Technician technician) {
this.technician = technician;
}
The following shows the one-to-one mapping association between Technicianprivatedata and Technician in the Technician class. The mappedBy attribute refers to the property name of the association on the owner side.
//bi-directional one-to-one association to Technicianprivatedata
@OneToOne(mappedBy="technician")
private Technicianprivatedata technicianprivatedata;
public Technicianprivatedata getTechnicianprivatedata() {
return this.technicianprivatedata;
}
public void setTechnicianprivatedata(Technicianprivatedata technicianprivatedata) {
this.technicianprivatedata = technicianprivatedata;
}
Here the Technician entity references multiple Ticket entities, but a Ticket entity can have only one Technician entity reference.
The one-to-many annotation defines an attribute mappedBy which is the name of the many-to-one field in the Ticket entity that maps this bidirectional relation.
//bi-directional many-to-one association to Ticket
@OneToMany(mappedBy="technician")
private Set tickets;
public Set getTickets() {
return this.tickets;
}
public void setTickets(Set tickets) {
this.tickets = tickets;
}
The many-to-one annotation defines an attribute fetch which is a enum that specifies whether to load the field's persisted data before the entity object is returned by the persistence provider (FetchType.EAGER) or later, when the property is accessed (FetchType.LAZY). The many-to-one are defaulted to FetchType.EAGER.
The many-to-one annotation is followed by a
@JoinColumn
annotation defining the column name which is the name of the column to which the property is bound and a
referencedColumnName attribute which is the name of the primary key column being joined to.
Open the Ticket class.
//bi-directional many-to-one association to Technician
@ManyToOne
@JoinColumn(name="TECHNICIANID")
private Technician technician;
public Technician getTechnician() {
return this.technician;
}
public void setTechnician(Technician technician) {
this.technician = technician;
}