注释mappedBy理想情况下应始终用于双向关系的Parent侧(Cities类),在这种情况下,它应该在Cities类中指向Child类的成员变量'city'(Theater类)
注释@JoinColumn用于指定用于连接实体关联的映射列,此注释可以在任何类(父或子)中使用,但理想情况下,它应仅在一侧使用(在父类或在在这种情况下,子类不在这两种情况下我在双向关系的子级(Branch类)中使用它,表示Branch类中的外键。
所以当我通过PostMan点击服务来检查我的帖子请求是否正常工作。我看到外键列没有正确更新。我的意思是将值设置为null。如果你们想看一下,我已经给出了数据库的图像。
城市:
@Entity
public class Cities {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
Long cityId;
String cityName;
@OneToMany(cascade = CascadeType.ALL, mappedBy="city", fetch = FetchType.LAZY)
List<Threatres> movies = new ArrayList<Threatres>();
public Cities() {
super();
}
public List<Threatres> getMovies() {
return movies;
}
public void setMovies(List<Threatres> movies) {
this.movies = movies;
}
public Long getCityId() {
return cityId;
}
public void setCityId(Long cityId) {
this.cityId = cityId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
}
Threatre :
@Entity
public class Threatres {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long threatreId;
String nameOfThreatres;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "cityId")
private Cities city;
public Threatres() {
super();
}
public Long getThreatreId() {
return threatreId;
}
public void setThreatreId(Long threatreId) {
this.threatreId = threatreId;
}
public Cities getCity() {
return city;
}
public void setCity(Cities city) {
this.city = city;
}
public Long getThreatres() {
return threatreId;
}
public void setThreatres(Long threatreId) {
this.threatreId = threatreId;
}
public String getNameOfThreatres() {
return nameOfThreatres;
}
public void setNameOfThreatres(String nameOfThreatres) {
this.nameOfThreatres = nameOfThreatres;
}
}
MyServiceClass
@Service
public class MasterService implements CityService, MoviesService, ThreatresService {
@Autowired
CitiesRepository cityRepository;
@Autowired
MoviesRepository moviesRepository;
@Autowired
ThreatresRepository threatresRepository;
@Override
public void saveCities(Cities cities) {
cityRepository.save(cities);
}
@Override
public void saveMovies(Movies movies) {
moviesRepository.save(movies);
}
@Override
public void saveThreatres(Threatres threatres) {
threatresRepository.save(threatres);
}
@Override
public List<Cities> findAllCities() {
return (List<Cities>) cityRepository.findAll();
}
@Override
public Cities findAllCityById(Long id) {
return cityRepository.findByCityId(id);
}
}
控制器类
`
@RestController
public class HomeController {
@Autowired
CityService cityService;
@Autowired
MoviesService moviesService;
@Autowired
ThreatresService threatresService;
@GetMapping("/")
public void getLoginPage() {
}
public void getAllCitites() {
}
@PostMapping(value = "/saveCities")
public void saveCities(@RequestBody Cities cities) {
cityService.saveCities(cities);
}
@PostMapping(value = "/saveMovies")
public void saveMovies(@RequestBody Movies movies) {
moviesService.saveMovies(movies);
}
@PostMapping(value = "/saveThreatres")
public void saveThreatres(@RequestBody Threatres threatres) {
threatresService.saveThreatres(threatres);
}
@GetMapping("/finaAllCities")
public List<Cities> getAllCites() {
return cityService.findAllCities();
}
@GetMapping("/finaAllCitiesbyId/{id}")
public Cities getCityById(@PathVariable Long id) {
return cityService.findAllCityById(id);
}
}`
请参阅下面的图片,查看邮寄申请的PostMan数据。
请求
Database Image: You can see the CityId in the table threatres is getting populated with null