In JPA (Java Persistence API), the @OneToMany annotation is used to establish a one-to-many relationship between two entity classes. This relationship is typically represented as a parent-child relationship, where the parent entity contains a collection (like a List or Set) of child entities, and each child entity has a reference back to the parent. This is commonly modeled using a foreign key in the child table that references the primary key of the parent table.
Let's consider a simple example with two entities: Parent and Child. Each Parent instance can have multiple Child instances, but each Child instance is associated with exactly one Parent.
import javax.persistence.*; import java.util.List; @Entity public class Parent { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // Other fields @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) private List<Child> children; // Getters and setters } import javax.persistence.*; @Entity public class Child { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // Other fields @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "parent_id") private Parent parent; // Getters and setters } Parent Entity: The Parent class has a children list annotated with @OneToMany. The mappedBy attribute indicates that the Parent entity does not control the relationship. Instead, it is the Child entity that contains the foreign key and controls the relationship.
Child Entity: The Child class has a parent field annotated with @ManyToOne, signifying that each Child is linked to one Parent. The @JoinColumn annotation specifies the name of the foreign key column in the Child table that refers to the Parent.
Cascade and Orphan Removal: In the Parent entity, cascade = CascadeType.ALL means that persistence operations (like save, update, delete) on the Parent will also apply to its Child entities. orphanRemoval = true indicates that if a Child is removed from the children collection, it will be automatically deleted from the database.
Lazy Fetching: The fetch = FetchType.LAZY in the @ManyToOne annotation specifies that the Parent entity should be lazily loaded. This means the Parent is not fetched from the database until it is explicitly accessed in the code.
To establish the relationship between a Parent and its Child entities, you would typically set the parent field of each Child object and add these Child objects to the children collection of the Parent.
Parent parent = new Parent(); Child child1 = new Child(); child1.setParent(parent); Child child2 = new Child(); child2.setParent(parent); parent.setChildren(Arrays.asList(child1, child2));
equals and hashCode in entities with bidirectional relationships. Consider business keys or IDs to avoid circular calls.This basic setup should help you establish a one-to-many relationship between entities in JPA. Remember, the actual database schema and relationships might require additional configurations based on your specific requirements and database constraints.
asp-net-config-builders domain-driven-design formsy-material-ui angular-ui beanshell v-model githooks windows-task-scheduler webrequest information-visualization