【发布时间】:2021-06-15 15:02:22
【问题描述】:
我有课 -> 国家和城市。
- 我想创建作品请求,当我调用获取所有国家/地区时,我将获取所有国家/地区,以及城市。
- 当我调用获取所有城市时,我会从 Country 模型中获取仅包含国家/地区的所有城市。
- 我想添加与国家/地区相关的新城市。
我的国家模型班:
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;
@NotEmpty(message = "Name of country is mandatory.")
@Column(unique = true)
private String nameOfCountry;
@NotBlank(message = "Name of capital is mandatory.")
private String capital;
@Max(value = 17098242L, message = "Maximum value for population = 10000000000")
private Long surface;
private String countryCode;
private String telephoneCode;
@OneToMany(cascade = CascadeType.PERSIST)
@JoinColumn(name = "country_Id", updatable = false, insertable = false)
private List<CityModelDao> cityModelDao;
我的城市模型类:
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;
@NotEmpty(message = "Name of country is mandatory.")
@NotNull
@Column(nullable = false, unique = true)
private String nameOfCity;
我知道我这里没有@ManyToOne,但我仍然做错了,现在我没有更多的想法。
这就是我想要的。
很遗憾,我没有得到有关国家/地区的信息。
你能帮我处理一下工作关系吗?我试过类似的东西:
城市模型:
@ManyToOne()
@JoinColumn(name = "country_Id")
private CountryModelDao countryId;
国家型号:
@OneToMany(mappedBy = "countryId", orphanRemoval = true, cascade = CascadeType.PERSIST)
private List<CityModelDao> cityModelDao;
但这是错误的。当我尝试使用上述关系城市时,我得到了错误。
您能告诉我在这种情况下如何正确处理@ManyToOne 吗?我做错了什么?
谢谢
【问题讨论】:
标签: spring-boot hibernate jpa h2