Definition: A relationship where one entity is associated with exactly one instance of another entity, navigable in one direction only.
Example: Product
HAS-A ProductDetails
(1:1).
Product
has a reference to ProductDetails
, but ProductDetails
does not reference Product
.Annotations:
@OneToOne
on the owning side (Product
).@JoinColumn
to specify the foreign key column.Code Example:
@Entity
public class Product {
@Id
private Long id;
private String name;
@OneToOne
@JoinColumn(name = "product_details_id")
private ProductDetails productDetails;
// Getters, setters
}
@Entity
public class ProductDetails {
@Id
private Long id;
private String description;
// No reference to Product
// Getters, setters
}
Database: Product
table has a product_details_id
column (foreign key to ProductDetails
).
Definition: A relationship where multiple instances of one entity can be associated with multiple instances of another, navigable in one direction.
Example: Product
HAS-A multiple Brands
, and vice versa (:), but only Product
references Brands
.
Annotations:
@ManyToMany
on the owning side (Product
).@JoinTable
to define the join table, specifying joinColumns
(for Product
) and inverseJoinColumns
(for Brand
).Code Example:
@Entity
public class Product {
@Id
private Long id;
private String name;
@ManyToMany
@JoinTable(
name = "product_brand",
joinColumns = @JoinColumn(name = "product_id"),
inverseJoinColumns = @JoinColumn(name = "brand_id")
)
private List<Brand> brands;
// Getters, setters
}
@Entity
public class Brand {
@Id
private Long id;
private String name;
// No reference to Product
// Getters, setters
}
Database: A join table product_brand
with columns product_id
and brand_id
.
Embeddable
classes) lack their own database identity and are embedded within an owning entity. Covered in your previous query.@Embeddable
: Marks a class as a value type (e.g., Address
).@Embedded
: Used in the entity to include the embeddable.Address
embedded in User
or College
.