Java Spring JPA惰性加载@OneToOne实体不';行不通

Java Spring JPA惰性加载@OneToOne实体不';行不通,java,spring,hibernate,jpa,one-to-one,Java,Spring,Hibernate,Jpa,One To One,我在懒洋洋地加载OrderEntity对象BillingAddress时遇到问题。我看到围绕这个问题提出了很多问题,并按照说明进行了操作,包括添加optional=false,但每当我findByIdanOrderEntity时,仍然会加载BillingAddress 这些是我的实体(为了这个问题而减少): 订单实体 @Entity @Table(name = "orders", schema = "glamitoms") public class Ord

我在懒洋洋地加载
OrderEntity
对象
BillingAddress
时遇到问题。我看到围绕这个问题提出了很多问题,并按照说明进行了操作,包括添加optional=false,但每当我
findById
an
OrderEntity
时,仍然会加载
BillingAddress

这些是我的实体(为了这个问题而减少):

订单实体

@Entity
@Table(name = "orders", schema = "glamitoms")
public class OrderEntity {

    @Id
    @Column(name = "id")
    private int id;

    @OneToOne(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
    private BillingAddressEntity billingAddress;

}
@Entity
@Table(name = "billing_address", schema = "glamitoms")
public class BillingAddressEntity {

    @Id
    @Column(name = "order_id")
    private int id;

    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    private OrderEntity order;
}
BillingAddressEssentialty

@Entity
@Table(name = "orders", schema = "glamitoms")
public class OrderEntity {

    @Id
    @Column(name = "id")
    private int id;

    @OneToOne(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
    private BillingAddressEntity billingAddress;

}
@Entity
@Table(name = "billing_address", schema = "glamitoms")
public class BillingAddressEntity {

    @Id
    @Column(name = "order_id")
    private int id;

    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    private OrderEntity order;
}
TestController

@RestController
public class TestController {

    private OrdersDAO ordersDAO;

    @Autowired
    public TestController(OrdersDAO ordersDAO) {
        this.ordersDAO = ordersDAO;
    }

    @GetMapping("/test")
    public void test() {
        OrderEntity orderEntity = ordersDAO.findById(1).get();
    }
}
OrdersDAO

@Repository
public interface OrdersDAO extends JpaRepository<OrderEntity, Integer> {
}
@存储库
公共接口顺序DAO扩展了JpaRepository{
}

billing\u address
有一个引用订单的FK。我读过一些自相矛盾的答案,说添加
optional=false
应该延迟加载实体,但对我来说,这似乎不起作用。我在这些实体中遗漏了什么吗?

看看弗拉德·米哈尔凯斯的文章

正如这里所描述的,解决方案之一是删除父端的关系

@Transient
private BillingAddressEntity billingAddress;
并使用共享id手动加载
账单地址属性

if (order.billingAddress == null) 
{
   order.billingAddress = entityManager.find(BillingAddressEntity.class, order.id);
}

另一种方法是删除共享密钥,改为使用外键字段,并将关系标记为
@manytone
。这将牺牲OneTONE约束检查

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "billing_address_id")
private BillingAddressEntity billingAddress;


然后还有字节码增强功能,允许您将其设置为
@LazyToOne(LazyToOneOption.NO_PROXY)
关系。不过我帮不了你,因为我自己从来没有这样做过。

看看弗拉德·米哈尔凯斯的文章

正如这里所描述的,解决方案之一是删除父端的关系

@Transient
private BillingAddressEntity billingAddress;
并使用共享id手动加载
账单地址属性

if (order.billingAddress == null) 
{
   order.billingAddress = entityManager.find(BillingAddressEntity.class, order.id);
}

另一种方法是删除共享密钥,改为使用外键字段,并将关系标记为
@manytone
。这将牺牲OneTONE约束检查

@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "billing_address_id")
private BillingAddressEntity billingAddress;


然后还有字节码增强功能,允许您将其设置为
@LazyToOne(LazyToOneOption.NO_PROXY)
关系。不过我不能帮你,因为我自己从来没有这样做过。

你可以使用字节码增强。查看详细答案不确定是否有
延迟加载
,但由于您正在执行
@MapsId
,hibernate正在尝试获取订单。我也遇到过类似的情况,在我的例子中,这是一种循环。我用@JsonIgnore(一个战术补丁)注释了OrderEnity order.@Morgan你用的是什么hibernate版本?@SternK我的hibernate版本是5.4。18@YoManTaMero我不明白。JsonIgnore不是杰克逊的注解吗?这与hibernate有什么关系?您可以使用字节码增强。查看详细答案不确定是否有
延迟加载
,但由于您正在执行
@MapsId
,hibernate正在尝试获取订单。我也遇到过类似的情况,在我的例子中,这是一种循环。我用@JsonIgnore(一个战术补丁)注释了OrderEnity order.@Morgan你用的是什么hibernate版本?@SternK我的hibernate版本是5.4。18@YoManTaMero我不明白。JsonIgnore不是杰克逊的注解吗?这和hibernate有什么关系?