在 Hibernate 中获取实体但空值属性

Getting Entity But null value properties in Hibernate

我正在通过代码获取实体并使用 Hibernate 从 Db 获取结果。 该值存在,但所有属性都显示为 null,如果我尝试使用返回的实体对象属性值的属性,这些属性将变为 NULL。但价值确实存在。

订单和购物车扩展了一个具有共同属性的抽象class。 下面是实体和我的代码。

@MappedSuperclass
@Getter @Setter
 public class AbstractOrder implements Serializable {

  @OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST)
  @JoinColumn(name = "DeliveryAddressID")
  private Address deliveryAddress;

  @OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST)
  @JoinColumn(name = "BillingAddressID")
  private Address billingAddress;
  ....
  ....

 }

 public Order cloneCartToOrder(String cartCode,String sellerId) {
    Cart cart = cartDao.findByCode(cartCode);
    Order order=getOrderDetails(cartCode);
    List<PromotionResult> promotionResults = null;
    if(order==null) {
        order = modelMapper.map(cart, Order.class);
        order.setOrderDate(new Date());
        order.setCode(cart.getCode());
        order.setStatus("Open");
        order.setHowWasOrderConfirmed(HowOrderConfirmStatus.ESALE);
        setOrderAddress(order, cart);
        promotionResults = order.getPromotionResults().stream().map(promotionResult -> {
   ..........
   ..........
   ..........

}

private void setOrderAddress(Order order, Cart cart) {
    Address address = addressDao.getAddressById(cart.getBillingAddress().getId());
    if (order.getShippingSameAsInvoice()) {
        Address billAddress = createNewAddressForOrder(address);
        order.setBillingAddress(billAddress);
        order.setDeliveryAddress(billAddress);
    } else {
        Address billAddress = createNewAddressForOrder(address);
        Address deliveryAddress = createNewAddressForOrder(addressDao.getAddressById(cart.getDeliveryAddress().getId()));
        order.setBillingAddress(billAddress);
        order.setDeliveryAddress(deliveryAddress);
    }
}

private Address createNewAddressForOrder(Address address ) {
    Address newAddress = new Address();
    newAddress.setBusinessPhone(address.getBusinessPhone());
    newAddress.setCountry(address.getCountry());
    newAddress.setCompany(address.getCompany());
    newAddress.setEmail(address.getEmail());
    newAddress.setFirstName(address.getFirstName());
    newAddress.setLastName(address.getLastName());
    newAddress.setHomePhone(address.getHomePhone());
    newAddress.setIsBillingAddress(address.getIsBillingAddress());
    newAddress.setIsShippingAddress(address.getIsShippingAddress());
    newAddress.setLatitude(address.getLatitude());
    newAddress.setLongitude(address.getLongitude());
    newAddress.setMobile(address.getMobile());
    newAddress.setPostCode(address.getPostCode());
    newAddress.setState(address.getState());
    newAddress.setStreetAddress1(address.getStreetAddress1());
    newAddress.setStreetAddress2(address.getStreetAddress2());
    newAddress.setSuburb(address.getSuburb());
    newAddress.setTitle(address.getTitle());
    newAddress.setZone(address.getZone());
    return newAddress;
}

所以如果你看到这里我从第一个方法调用这个 setOrderAddress(order, cart); 方法。然后将地址填充到新对象以将其保存为新地址。但是保存后我可以看到我新创建的所有值都保存为 Null 只有用 ID 创建的地址。

请在 IDE 检查时查看 PFA。

实体是延迟获取的(FetchType.LAZY),你必须让它们急切获取(FetchType.EAGER),或者,如果你关心这种情况下的性能,你可以编写自己的查询到将它们与父实体一起获取。