Java Reference
In-Depth Information
The
Catalog
entity has a many-to-many relationship with the
Edition
entity. We
shall make the
Catalog
entity the owning-side of the relationship. The join table
is defined on the owning side and cascade operations may be initiated only from
the owning side. Specify the
@ManyToMany
annotation in the
Catalog
entity with
cascade
element set to
ALL
, as we want to cascade all changes to the associated
Edition
entities when a
Catalog
entity is deleted. Setting cascading to
ALL
does
degrade performance slightly, as extra queries are required to be created, but
cascading propagates modifications to the associated entities.
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="CATALOGEDITIONS",
joinColumns=@JoinColumn(
name="catalogId", referencedColumnName="ID"),
inverseJoinColumns=
@JoinColumn(name="editionId", referencedColumnName="ID"))
The join table,
CATALOGEDITIONS
, is generated by the EJB container when the EJB
is deployed to the server; therefore, we don't need to generate the join table. The
join column
catalogId
references the primary key id of the
CATALOG
table, and the
inverse join column
editionId
references the primary key id of the
EDITION
table.
The
cascade
element is set to
ALL
, which implies that all operations are cascaded.
The
Catalog
entity bean class is listed next:
package model;
import java.io.Serializable;
import javax.persistence.*;
import java.util.*;
@Entity
@NamedQueries({
@NamedQuery(name="findCatalogAll", query="SELECT c FROM Catalog c"),
@NamedQuery(name="findCatalogByJournal",
query="SELECT c FROM Catalog c
WHERE c.journal = :journal")
})
public class Catalog implements Serializable {
static final long serialVersionUID = 1;
private int id;
private String journal;
private List<Edition> editions;
@Id
@GeneratedValue
Search WWH ::
Custom Search