Java Reference
In-Depth Information
private int quantity;
//bi-directional many-to-one association to Seat
@OneToMany(mappedBy="seatType", fetch=FetchType.EAGER)
[4]
private List<Seat> seats;
// Getters and Setters omitted for brevity
}
The first meaningful annotation is
@Entity [1]
, which declares the class
Entity
.
The
@Table [2]
annotation is used to map the bean class with a database table.
The
@Id
annotation,
[3]
, is a mandatory one; it describes the primary key of the table.
Along with
@Id
, there is the
@GeneratedValue
annotation. This is used to declare
that the database is in charge of generating the value. You can check the Javadoc of this
class to explore other strategies for value generation.
Moving along, the
@OneToMany
annotation
[4]
defines an association with one-to-
many cardinality. Actually, the
SeatType
class has many seats. The corresponding
Seat
reference is contained in a list collection. We define the
mappedBy
attribute in or-
der to set the field, which owns the relationship on the many side.
The
fetch
attribute defines that JPA should fetch the list of seats whenever a
seat
type
is loaded from the database. A lazy configuration for a relationship would cause the list to
be fetched on the first call to that field.
Finally, note that we have not included here, for the sake of brevity, the field getters and
setters that have been generated.
Let's take a look at the
Seat
entity:
@Entity
public class Seat implements Serializable {
private static final long serialVersionUID = 89897231L;
@Id