Java Reference
In-Depth Information
The Edition entity class
The
Edition
entity bean has the properties
id
and
edition
:
private int id;
private String edition;
The
Edition
entity defines a
NamedQueries
findEditionAll()
(which finds all the
Edition
instances) and
findEditionByEdition()
(which finds an
Edition
by the
edition
date):
@NamedQueries( { @NamedQuery(name = "findEditionAll",
query = "SELECT e FROM Edition e"),
@NamedQuery(name = "findEditionByEdition",
query = "SELECT e from Edition e
WHERE e.edition = :edition")
} )
The
Edition
entity is on the non-owning side of a bidirectional many-to-many
relationship with the
Catalog
entity. Therefore, we specify the
@ManyToMany
annotation with the
mappedBy
element. We don't need to specify a join table, as
Edition
entity is the non-owning side. The
cascade
element is set to cascade
MERGE
,
PERSIST
, and
REFRESH
operations:
@ManyToMany(cascade =
{ CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH },
mappedBy = "editions")
protected List<Catalog> getCatalogs() {
return catalogs;
}
The
editions
field is defied in the
Catalog
entity, which is the owning side of
the relationship. The
Edition
entity also has a one-to-many relationship with the
SECTION
entity. In a one-to-many relationship the many side is made the owning
side; therefore, the many side has the join table. But, a join table may be added on
the one side also to initiate cascade operations from the one side. Therefore, we have
added a join table on the one side (
Edition
entity) also:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "edition")
@JoinTable(name = "EditionSection",
joinColumns = { @JoinColumn(
name = "editionId", referencedColumnName = "ID") },
inverseJoinColumns = { @JoinColumn(
name = "sectionId", referencedColumnName ="ID") } )
Search WWH ::
Custom Search