OneToMany关系的连接列为空

时间:2018-12-19 04:17:57

标签: spring hibernate spring-boot

我正在尝试创建“酒店房间”关系,在电话中,我试图通过“房间数组”。希望将房间保存在控制器中。 我现在面临的问题是,当我致电/ newhotel时,将保存一个旅馆,并保存与之关联的房间。但是这些房间的hotel_id为NULL。为什么在保存房间时未创建关联?

public class Hotel {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @OneToMany(mappedBy = "hotel", cascade = CascadeType.ALL)
    private List<Room> rooms;

}

public class Room {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "hotel_id")
    private Hotel hotel;

    @Enumerated(EnumType.STRING)
    private RoomType roomType;
}

@RestController
public class HotelController {
    @Autowired
    HotelRepository hotelRepository;
    @Autowired
    RoomRepository roomRepository;
    @CrossOrigin
    @PostMapping(value = "/newhotel")
    public Hotel addHotel(@RequestBody Hotel hotel) {
        return hotelRepository.save(hotel);
    }
}

下面是我的求职请求正文:

{
    "name" : "hotel1",
    "rooms" : [
              {
                "roomType" : "SingleBed"
              } 
    ]
}

0 个答案:

没有答案