我有两个表Stock和StockLocation。因为每个股票都有多个StockLocation,而每个StockLocation只有一个股票。
当我从StockLocation获取库存时,我得到了空指针异常
@Entity(name = "stock")
public class Stock {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy="stock")
@JsonManagedReference
private List<StockLocation> stockLocations;
}
@Entity(name = "stock_location")
public class StockLocation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "stock_id")
@JsonBackReference
private Stock stock;
}
现在,当我尝试从StockLocation获取库存时,将出现Null指针异常。
StockLocation sl = stockLocRep.findById(id).get();
sl.getStock();