使用@OneToMany 映射时数据库中出现额外的列
Extra column coming in database when using @OneToMany mapping
我正在创建三个 table 实体,学生、学院和地址。当我尝试将数据插入地址 table 时,我只在数据库的地址 table 中得到一个额外的列(student_STUDENT_ID)。请查找随附的屏幕截图。我无法弄清楚为什么这个字段会出现?可以删除吗?下面是映射这三个实体的代码。
地址table-
@ManyToOne
private Student student;
//setters and getters
学生table-
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="COLLEGE_ID")
private College college;
//getters and setters
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="STUDENT_ID")
private Set<Address> addr;
//getters and setters
大学table-
@OneToOne(mappedBy="college",cascade=CascadeType.ALL)
private Student student;
尝试将@JoinColumn 移动到地址class
@ManyToOne
@JoinColumn(name = "STUDENT_ID")
private Student student;
然后在你的 Student Table 中,将 mappedBy 添加到 @OneToMany
@OneToMany(cascade=CascadeType.ALL, mappedBy="student")
private Set<Address> addr;
真正的配置应该是这样的-
地址 class -
@ManyToOne
@JoinColumn(name="STUDENT_ID")
private Student student;
//getters and setters
学生class-
@OneToMany(cascade=CascadeType.ALL, mappedBy="student")
private Set<Address> addr;
//getters and setters
大学没有变化class。
地址table-
@ManyToOne
private Student student;
//setters and getters
学生table-
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="COLLEGE_ID")
private College college;
//getters and setters
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="STUDENT_ID")
private Set<Address> addr;
//getters and setters
大学table-
@OneToOne(mappedBy="college",cascade=CascadeType.ALL)
private Student student;
尝试将@JoinColumn 移动到地址class
@ManyToOne
@JoinColumn(name = "STUDENT_ID")
private Student student;
然后在你的 Student Table 中,将 mappedBy 添加到 @OneToMany
@OneToMany(cascade=CascadeType.ALL, mappedBy="student")
private Set<Address> addr;
真正的配置应该是这样的-
地址 class -
@ManyToOne
@JoinColumn(name="STUDENT_ID")
private Student student;
//getters and setters
学生class-
@OneToMany(cascade=CascadeType.ALL, mappedBy="student")
private Set<Address> addr;
//getters and setters
大学没有变化class。