我有两个表,第一个称为区域,它有"region_id"
,"region_name"
,第二个表称为城市,它有"city_id"
,"city_name"
, "region_id"
为FK,
现在,当我试图拉开所有城市时,我点击了
/城市
//CityController
@RestController
public class City {
@Autowired
private CityService cityService;
@GetMapping(value = "/cities")
public @ResponseBody
List<CityEntity> getCities() {
List<CityEntity> list = cityService.getCities();
return list;
}
}
CityEntity是:
@Entity
@Table(name = "cities")
@Getter
@Setter
public class CityEntity {
@Id
@Column(name = "city_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "city_name", unique = true)
private String cityName;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "region_id")
private RegionEntity region;
}
和RegionEntity是:
@Entity
@Table(name = "regions")
@Getter
@Setter
public class RegionEntity {
@Id
@Column(name = "region_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "region_name", unique = true)
private String regionName;
}
,回复是
[ { “id”:1, “cityName”:“amman”, “region”:{ “id”:1, “regionName”:“part1” } }, { “id”:2, “cityName”:“zarqa”, “region”:{ “id”:1, “regionName”:“part1” } }]
到目前为止一切顺利
我想要另一个API,它可以拉动所有地区的城市,所以我做了像cityEntity这样的东西,我将下面的方法添加到reagionEntity
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "city_id")
private List<CityEntity> cities;
但令人惊讶的是,当我重新开始时
似乎它重复了我自己不知道为什么,并且没有任何帮助我在spring.io网站/城市 我得到
这样的对象Unexpected ''
但是当我在Chrome浏览器中点击它时,列出了很多像[{"id":1,"regionName":"part1","cities":[{"id":1,"cityName":"amman","region":{"id":1,"regionName":"part1","cities":[{"id":1,"cityName":"amman","region":{"id":1,"regionName":"part1","cities":
我的存储库正在扩展JPARepository,我使用了findAll()
方法。
请指教。
提前致谢