Explain
@OneToOne(mappedBy = "demographicData", fetch = FetchType.LAZY, optional = false)
This is a JPA (Java Persistence API) annotation used to define a one-to-one relationship between two entity classes in Java, typically for ORM (Object-Relational Mapping) with databases.
Key Elements Explained
Attribute Meaning @OneToOne Declares a one-to-one relationship between two entities. mappedBy = "demographicData" Indicates this side is the inverse (non-owning) side of the relationship. The owning side is the other entity’s field named demographicData. fetch = FetchType.LAZY The related entity is loaded on demand (not immediately when the parent is loaded). optional = false The relationship is mandatory; the associated entity must exist (cannot be null).
Example Context
Suppose you have two entities: Person and DemographicData. If Person has a field:@OneToOne(mappedBy = "person", fetch = FetchType.LAZY, optional = false) private DemographicData demographicData;
And DemographicData has:@OneToOne @JoinColumn(name = "person_id") private Person person;
The mappedBy attribute tells JPA that the DemographicData entity owns the relationship (it has the foreign key).
fetch = FetchType.LAZY means demographicData is only loaded when accessed.
optional = false means every Person must have a DemographicData.
Tip
Use mappedBy to avoid duplicate foreign keys and clarify which entity manages the relationship.
LAZY fetching can improve performance but may cause LazyInitializationException if not handled properly.

















