还是之前举的 城市 和 学校之间的例子
    一座城市可以有多个学校
    而每个学校都只能在一个城市

    City.java

    1. package com.ctguyxr.satokendemo.entity;
    2. import com.fasterxml.jackson.annotation.JsonBackReference;
    3. import com.fasterxml.jackson.annotation.JsonManagedReference;
    4. import lombok.Data;
    5. import javax.persistence.*;
    6. import java.util.List;
    7. /**
    8. * Created By Intellij IDEA
    9. *
    10. * @author Xinrui Yu
    11. * @date 2021/11/28 20:59 星期日
    12. */
    13. @Entity
    14. @Data
    15. @Table(name = "t_city")
    16. public class City {
    17. @Id
    18. @GeneratedValue(strategy = GenerationType.IDENTITY)
    19. private Integer id;
    20. private String name;
    21. @JsonManagedReference
    22. @OneToMany(cascade = CascadeType.ALL)
    23. @JoinColumn(name = "city_id")
    24. private List<College> colleges;
    25. }

    College.java

    1. package com.ctguyxr.satokendemo.entity;
    2. import com.fasterxml.jackson.annotation.JsonBackReference;
    3. import com.fasterxml.jackson.annotation.JsonIgnore;
    4. import com.fasterxml.jackson.annotation.JsonManagedReference;
    5. import lombok.Data;
    6. import javax.persistence.*;
    7. import javax.persistence.criteria.CriteriaBuilder;
    8. import java.util.List;
    9. /**
    10. * Created By Intellij IDEA
    11. *
    12. * @author Xinrui Yu
    13. * @date 2021/11/28 21:00 星期日
    14. */
    15. @Entity
    16. @Data
    17. @Table(name = "t_colleges")
    18. public class College {
    19. @Id
    20. @GeneratedValue(strategy = GenerationType.IDENTITY)
    21. private Integer id;
    22. private String name;
    23. @JsonBackReference
    24. @ManyToOne(cascade = CascadeType.ALL)
    25. private City city;
    26. }

    最后生成的表结构
    image.png
    image.png