Entity Types
- Definition: Objects with their own database identity (primary key) and lifecycle, existing independently of other entities.
- Characteristics:
- Persisted with a unique identifier (primary key).
- References to entities are stored as foreign keys in the database.
- Example:
College
(has its own primary key).
- Annotations:
@Entity
: Marks a class as an entity.
@Id
: Specifies the primary key.
Value Types
- Definition: Objects without their own database identity (no primary key), embedded within an owning entity, and tied to its lifecycle.
- Characteristics:
- Represent table columns in the database.
- Cannot exist independently; lifespan depends on the owning entity.
- Example:
Address
embedded in College
or Student
.
- Annotation:
@Embeddable
: Marks a class as a value type that can be embedded in an entity.
Types of Value Types
- Basic Value Types:
- Map a single database column to a single, non-aggregated Java type.
- Examples:
String
, Integer
, Boolean
, etc.
- Built-in support in Hibernate; no additional annotations needed for basic mappings.
- Composite Value Types (Embedded Types/Components):
- Similar to entities but lack their own lifecycle or identifier.
- Example:
Address
embedded in Student
or College
.
- Annotations:
@Embeddable
: Defines the composite type.
@Embedded
: Specifies that an entity field is an instance of an embeddable class.
@AttributeOverride
: Overrides column mappings (e.g., mapping streetAddress
to STREET_ADDRESS
in Student
and STREET
in College
).
@AttributeOverrides
: Used for multiple column overrides.
- Key Point: Value types do not support shared references; each entity (
Student
, College
) has its own copy of the value type (Address
).
- Collection Value Types:
- Represent collections of basic or composite types stored in a separate table.
- Annotations:
@ElementCollection
: Marks a collection of basic or embeddable types.
@CollectionTable
: Specifies the table storing the collection, with joinColumns
linking to the owning entity.
@Column
: Defines the column name for basic types in the collection table.
@AttributeOverride
: Overrides column mappings for embeddable types in collections.
- Examples:
- Collection of basic types:
Collection<String> contacts
mapped to a table (Contacts
) with a column (CONTACT_NO
).
- Collection of embeddables:
List<ContactAddress> address
mapped to a table (CONTACT_ADDRESS
) with overridden column names (e.g., STREET_ADDRESS
).
Key Clarifications
- Entity vs. Value Type: Entities have independent lifecycles and identities; value types are dependent on their owning entity and lack their own identity.
- Shared References: Value types (like
Address
) are not shared between entities; each entity gets its own copy, unlike entity references (foreign keys).
- Collection Mapping: Collections require a separate table because a single column cannot store multiple values. The
@CollectionTable
annotation defines this table and its relationship to the owning entity.
- Attribute Overrides: Useful when the same embeddable type (e.g.,
Address
) is used in multiple entities but maps to different column names in the database.