問題摘要:嵌套@OneToOne - > @OneToMany不填充@OneToMany子對象
我有三個@entities下列關係: CustomerOrder - > @OneToOne - >預訂 - > @OneToMany - > ProductOrder。
當我調用控制器直接獲取CustomerOrder時,會拉動CustomerOrder和Reservation子對象。 List<ProductOrder>
保持爲空。
如果我打電話給控制器直接獲取預訂,則保留下的List<ProductOrder>
已完全填充。
當我打印出Hibernate生成的用於提取List<ProductOrder>
的SQL時,SQL實際上確實會拉取相應的ProductOrder列表,但Spring並未填充列表。
詳情:
我有三個類(略去了getter和setter):
CustomerOrder:
@Entity
public class CustomerOrder {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(
name="id",referencedColumnName="customerOrderId",insertable=false, updatable=false)
private Reservation reservation;
}
預訂
@Entity
public class Reservation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//Tie a reservation back to a customer order
private Long customerOrderId;
@OneToMany
@JoinColumn(name = "reservationId",
referencedColumnName="id", insertable=false, updatable=false)
private List<ProductOrder> productOrderList = new ArrayList<ProductOrder>();
}
產品訂購:
@Entity
public class ProductOrder {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private Long customerOrderId;
private Long reservationId;
}
我也有兩個端點。一個可以直接讀取預約,或者一個可以通過customerOrder取預約:
@RequestMapping(value = "/customer-order/{id}", method = RequestMethod.GET)
public @ResponseBody CustomerOrder findOne(@PathVariable(value="id") Long id) {
return CustomerOrder customerOrder = customerOrderRepository.findOne(id);
}
@RequestMapping(value = "/reservation/{id}", method = RequestMethod.GET)
public @ResponseBody Reservation findOne(@PathVariable(value="id") Long id) {
return reservationService.findOne(id);
}
我使用的彈簧引導版本1.3.5.RELEASE與MySQL 23年6月5日