Java Reference
In-Depth Information
Large Objects
A persistent property or field can be marked for persistence as a database-supported large
object type by applying the
@Lob
annotation.
The annotation takes no attributes, but the underlying large object type to be used will
be inferred from the type of the field or parameter. String- and character-based types will be
stored in an appropriate character-based type. All other objects will be stored in a BLOB.
Listing 6-27 maps a
String
into a large object column type.
Listing 6-27.
An Example of a Large Object Property
@Lob
public String getTitle() {
return this.title;
}
The
@Lob
annotation can be used in combination with the
@Basic
annotation.
Mapped Superclasses
A special case of inheritance occurs when the root of the hierarchy is not itself a persistent
entity, but various classes derived from it are. Such a class can be abstract or concrete. The
@MappedSuperclass
annotation allows you to take advantage of this circumstance.
The class marked with
@MappedSuperclass
is not an entity, and is not queryable (it cannot
be passed to methods that expect an entity in the
Session
or
EntityManager
objects). It can-
not be the target of an association.
The mapping information for the columns of the superclass will be stored in the same
table as the details of the derived class (in this way, the annotation resembles the use of the
an
@Inheritance
tag with the
SINGLE_TABLE
strategy).
In other respects, the superclass can be mapped as a normal entity, but the mappings will
apply to the derived classes only (since the superclass itself does not have an associated table
in the database). When a derived class needs to deviate from the superclass's behavior, the
@AttributeOverride
annotation can be used (much as with the use of an embeddable entity).
For example, if in our example model
Book
was a superclass of
ComputerBook
, but
Book
objects themselves were never persisted directly, then
Book
could be marked as
@MappedSuperclass
, as in Listing 6-28.
Listing 6-28.
Marking the
Book
Class As a Mapped Superclass
@Entity
@MappedSuperclass
public class Book {
...
}
The fields of the
ComputerBook
entity derived from
Book
would then be stored in the
ComputerBook
entity class's table. Classes derived directly from
Book
but not mapped as entities
in their own right, such as a hypothetical
MarketingBook
class, would not be persistable. In this
respect alone, the mapped superclass approach behaves differently from the conventional
@Inheritance
approach with a
SINGLE_TABLE
strategy.
Search WWH ::
Custom Search