Java Reference
In-Depth Information
Section entity
Similarly, to the
Section
entity add named queries to find all
Section
entities and
find a
Section
entity by ID. As the
Section
entity has a one-to-many mapping with
the
Article
entity define a parameterized variable of type
List
for the
Article
entity.
Specify a
@ManyToOne
relationship with the
Edition
entity. If we want to initiate
merge, persist, refresh operations from the
Section
entity, we need to add a join
table on the
Section
entity side. But, if we don't want the associated
Edition
to
be deleted, then if the
Section
entity is deleted don't set the
cascade
element to
CascadeType.ALL
. Set the fetch strategy to
EAGER
as we want to retrieve associated
entities when an entity is retrieved. The
mappedBy
element is not required to be set
in a unidirectional relationship; the
mappedBy
element is set on the non-owning side
of the relationship, which in the edition-section relationship is the
Edition
entity.
However, if you want to make the
Section
entity as the owning side too, add the
mappedBy
element. The
@ManyToOne
relationship with the getter/setter methods for
the
Edition
entity is defined as follows:
@ManyToOne(cascade ={ CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH },fetch = FetchType.EAGER)
@JoinTable(name = "Edition_Section", joinColumns = {
@JoinColumn(name = "edition_id",
referencedColumnName = "id")} ,
inverseJoinColumns = {
@JoinColumn(name = "section_id",
referencedColumnName ="id")})
public Edition getEdition() {
return edition;
}
public void setEdition(Edition edition) {
this.edition = edition;
}
We also need to add a
@OneToMany
relationship to the
Article
entity. Set the
cascade
element to
CascadeType.ALL
as we want to cascade all operations to the
Article
entity. An
Article
entity without a
Section
entity wouldn't have much
significance. Set the
mappedBy
element to
Section
as the
Section
entity is the non-
owning side in the relationship. Add getter/setter methods for the
Article
entity
and also add add/remove methods to add and remove an
Article
entity:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "section",
fetch = FetchType.EAGER)
@JoinTable(name = "Section_Article", joinColumns = {
@JoinColumn(name = "section_id",
referencedColumnName = "id")} ,
inverseJoinColumns = {
Search WWH ::
Custom Search