我有2节课。 USER类和ORDER类。 订单类具有与@ManyToOne关系的User对象。 当RestController使用@PostMapping检索Order对象插入后的Order对象时,它将为嵌套的User对象返回空值
$methodname
}
@RestController
public class OrderController {
@PersistenceContext
EntityManager entityManager;
@Transactional
@PostMapping(value = "api/v1/create/order")
public Order createOrder(@RequestBody Order order){
entityManager.persist(order);
return order;
}
@Entity
@Table(name = "booking_order")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Integer orderID;
@Column
String transactionAmount;
@Column
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
Date bookingTimestamp;
@ManyToOne
User user;
public Order(){}
public Integer getOrderID() {
return orderID;
}
public void setOrderID(Integer orderID) {
this.orderID = orderID;
}
public String getTransactionAmount() {
return transactionAmount;
}
public void setTransactionAmount(String transactionAmount) {
this.transactionAmount = transactionAmount;
}
public Date getBookingTimestamp() {
return bookingTimestamp;
}
public void setBookingTimestamp(Date bookingTimestamp) {
this.bookingTimestamp = bookingTimestamp;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
{ “ transactionAmount”:“ 100.50”, “用户”:{ “ id”:1 } }
{ “ orderID”:1 “ transactionAmount”:“ 100.50”, “ bookingTimestamp”:“ 2019-05-15T20:44:43.234 + 0000”, “用户”:{ “ id”:1 “用户名”:null, “密码”:null } }