OneToMany & ManyToOne mapping Spring Boot / Hibernate

后端 未结 4 1909
生来不讨喜
生来不讨喜 2020-12-18 05:17

I have two tables with foreign key relations. I\'ve tried searching on how to do it and it always leads to OneToMany & ManyToOne mapping. I have these two tables.

<
4条回答
  •  萌比男神i
    2020-12-18 05:58

    You have to annotate the Java fields not the getter like:

    @OneToMany(targetEntity=User.class, mappedBy="userRole",cascade=CascadeType.ALL, fetch = FetchType.LAZY)
    private List user;
    

    and for User.class

    @ManyToOne
    @JoinColumn(name="role_id")
    private UserRole userRole;
    

    In addition, do you have defined the role_id column in the user table? I can't see this in the screenshot

提交回复
热议问题