这个关系是一个@OneToMany实例吗?

huangapple go评论73阅读模式
英文:

Is this relationship a @OneToMany instance?

问题

我已经有两个表格:

zip_codes_germany

travel_agencies

填充了一些数据。其中travel_agencies具有从列namecity组成的复合主键,而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:

zip_codes_germany

and

travel_agencies

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 = &quot;travel_agencies&quot;)
public class TravelAgency implements Serializable {

	private static final long serialVersionUID = -5215122407119218666L;
	
	@Id
	@Column(name = &quot;name&quot;)
	private String name;
	@Id
	@Column(name = &quot;city&quot;)
	private String city;
	@Column(name = &quot;ceo&quot;)
	private String ceo;
	@OneToMany
	@JoinColumn(name = &quot;city&quot;)
	private Set&lt;ZipCodeArea&gt; 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.

huangapple
  • 本文由 发表于 2020年8月11日 23:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63361013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定