我使用 Hibernate 并且我有实体:
@Data
@Entity
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "country_nm")
private String countryName;
}
@Data
@Entity
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "city_nm")
private String cityName;
}
国家可以有很多城市,城市只有一个国家。关联这些实体的最佳方式是什么?
1)在类中添加City city字段并在其上方添加和注释?结果,我们将有两个表:并且,表将具有 country_id 列。City@ManyToOne@JoinColumncountrycitycity
2)Country country在Country类及其@OneToMany(mappedBy='country')上方添加City city字段并在City类中添加字段并在其上方添加@ManyToOne注释?在这种情况下,将有三个表:country和city组合表country_city