Skip to main content

Posts

Showing posts from February, 2023

JPA implemntation many to many parent child relation of using one table and one mapping table

  JPA implemntation many to many parent child relation of using single table and one mapping table, with save api and fetch api by the root id. In JPA (Java Persistence API), a many-to-many relationship on a single class can be modeled using a self-referencing join table. This is done by creating two entities, one for the class and another for the join table, and using the @ManyToMany annotation on the class with the @JoinTable annotation on the join table. The @JoinTable annotation is used to specify the name and columns of the join table, and the @ManyToMany annotation is used to specify the mapping between the class and the join table. Here is an example of implementing a many-to-many parent-child relationship using JPA, with a Category table and a mapping table, as well as a save API and a fetch API by root ID: Create Category and Mapping entities. @Entity  public class Category { @Id@GeneratedValue(strategy = GenerationType.IDENTITY)   private Long id;   p