[JAVA] One-to-many relationship

I posted it because I had a hard time implementing it as a memorandum for myself.

Overview

Parent table

RestaurantsEntity.java


@Entity
@Table(name="restaurants")
public class RestaurantsEntity {

  //Description of each field variable Omitted ……

  @OneToMany(mappedBy="restaurantId")
  private List<RestaurantMenuEntity> restaurantMenuEntityList;

  //The following setter/getter abbreviation ……
	 
}

The field on one side of "one-to-many" holds a collection of entities on the other side (private List restaurantMenuEntityList;).

Annotate @OneToMany and specify the field of the multi-sided entity to be associated with mappedBy (mappedBy = "restaurantId").

Child table

RestaurantMenuEntity.java


@Table(name="restaurantMenu")
public class RestaurantMenuEntity {

  //Description of each field variable Omitted ……

     @ManyToOne(fetch = FetchType.LAZY)
     @JoinColumn(name = "restaurantId")
     private RestaurantMenuEntity restaurantId;
     
  //The following setter/getter abbreviation ……
	 
}

The "many-to-one" multi-sided field holds one-sided entities (private RestaurantMenuEntity restaurantId;).

The relationship is defined by adding the @ManyToOne annotation.

The name specifies the column name used to join the related tables (name = "restaurantId").

Afterword

I don't understand everything yet, but I was able to implement it for the time being.

Recommended Posts

One-to-many relationship
Association (one-to-many relationship)