英文:
Is this relationship a @OneToMany instance?
问题
我已经有两个表格:
和
填充了一些数据。其中travel_agencies具有从列name和city组成的复合主键,而zip_codes_germany具有主键zip_code。
让条件满足,即两个表格中的城市名称相同。现在一个城市映射到多个邮政编码。
在使用Hibernate加载TravelAgency对象时,使这种关系清晰以获取正确的邮政编码列表的最佳方法是什么?
我考虑的是像这样的东西:
@Entity
@Table(name = "travel_agencies")
public class TravelAgency implements Serializable {
private static final long serialVersionUID = -5215122407119218666L;
@Id
@Column(name = "name")
private String name;
@Id
@Column(name = "city")
private String city;
@Column(name = "ceo")
private String ceo;
@OneToMany
@JoinColumn(name = "city")
private Set<ZipCodeArea> zipCodeAreas;
}
我对Hibernate和与数据库一般工作都很陌生,所以我不太确定它是否是获得我所要求的内容的正确工具,还是我最好选择其他东西?
英文:
I already have two tables:
and
filled with some data. Where travel_agencies has a composite key from columns name and city and zip_codes_germany has the primary key zip_code.
Let the condition be fulfilled that the city names are the same in both tables. Now one city maps to multiple zip_codes.
What is the best way to make this relationship clear in order to receive the correct list of zip codes when loading a TravelAgency object using hibernate?
I'm thinking of something like this:
@Entity
@Table(name = "travel_agencies")
public class TravelAgency implements Serializable {
private static final long serialVersionUID = -5215122407119218666L;
@Id
@Column(name = "name")
private String name;
@Id
@Column(name = "city")
private String city;
@Column(name = "ceo")
private String ceo;
@OneToMany
@JoinColumn(name = "city")
private Set<ZipCodeArea> zipCodeAreas;
I'm new to hibernate and working with databases in general so I'm not quite sure if it is the correct tool to get what I'm asking for or if I'd better go with something else?
答案1
得分: 0
如OH GOD SPIDERS所评论的,我在这里确实有一个多对多的关系。
英文:
As commented by OH GOD SPIDERS, I do have a many to many relationship here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论