JPA:如何避免简单地加载对象,以便将其ID存储在数据库中?

JPA:如何避免简单地加载对象,以便将其ID存储在数据库中?,第1张

JPA:如何避免简单地加载对象,以便将其ID存储在数据库中?

首先,您应注意加载Owner实体的方法。

如果您使用的是Hibernate

Session

// will return the persistent instance and never returns an uninitialized instancesession.get(Owner.class, id);// might return a proxied instance that is initialized on-demandsession.load(Owner.class, id);

如果您使用的是

EntityManager

// will return the persistent instance and never returns an uninitialized instanceem.find(Owner.class, id);// might return a proxied instance that is initialized on-demandem.getReference(Owner.class, id);

因此,您应该延迟加载Owner实体,以避免对高速缓存或数据库造成某些损失。

顺便说一句,我建议您反转

Owner
和之间的关系
Cat

例如 :

Owner owner = ownerRepository.load(Owner.class, id);owner.addCat(myCat);


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5675991.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存