Java Reference
In-Depth Information
Listing 6-20. The Author Side of the Many-to-Many Association
@ManyToMany(mappedBy = "authors")
public Set<Book> getBooks() {
return books;
}
Alternatively, we could specify the link table in full, as in Listing 6-21.
Listing 6-21. Specifying the Link Table in Full Using the Topic Entity Annotations
@ManyToMany(cascade = ALL)
@JoinTable(
name="Books_to_Author",
joinColumns={@JoinColumn(name="book_ident")},
inverseJoinColumns={@JoinColumn(name="author_ident")}
)
public Set<Author> getAuthors() {
return authors;
}
Inheritance
The EJB 3 standard and Hibernate both support three approaches to mapping inheritance
hierarchies into the database. These are as follows:
Single table ( SINGLE_TABLE )
Joined ( JOINED )
Table-per-class ( TABLE_PER_CLASS )
Persistent entities that are related by inheritance must be marked up with the
@Inheritance annotation. This takes a single strategy attribute, which is set to one of three
javax.persistence.InheritanceType enumeration values corresponding to these approaches
(shown in brackets in the preceding bulleted list).
The single table approach manages one class for the superclass and all its subtypes.
There are columns for each mapped field or property of the superclass, and for each dis-
tinct field or property of the derived types. When following this strategy, you will need to
ensure that columns are appropriately renamed when any field or property names collide
in the hierarchy.
To determine the appropriate type to instantiate when retrieving entities from the data-
base, an @DiscriminatorColumn annotation should be provided in the root (and only in the
root) of the persistent hierarchy. 3 This defines a column containing a value that distinguishes
between each of the types used. The attributes permitted by the @DiscriminatorColumn anno-
tation are as follows:
3. That is to say, the highest class in the hierarchy that is mapped to the database as an entity should be
annotated in this way.
Search WWH ::




Custom Search