简体   繁体   English

以编程方式获取惰性JPA字段

[英]Programmatically Fetching a Lazy JPA Field

I have a lazily fetched field in my entity 我的实体中有一个延迟获取的字段

@ElementCollection(fetch = LAZY)
private List<String> emails;

And my transaction boundary stops at service class, I don't want to keep it open while rendering the view. 而且我的事务边界在服务类处停止,我不想在渲染视图时使其保持打开状态。 I want my service classes to return detached entities. 我希望我的服务类返回分离的实体。

In my service class I tried calling the getters but that seem to be erased by the compiler -- maybe it's an optimization for a statement that appear to do nothing 在我的服务类中,我尝试调用getter,但似乎已被编译器抹去了-也许这是对似乎什么都不做的语句的优化

/* User Service class */
@Transactional
public List<User> getAllUsers() {
  List<User> users = new ArrayList();
  for(User u : userRepo.findAll()) {
    u.getEmails(); // <-- this seem to be erased by the compiler optimization.
    users.add(u);
  }
  return users;
}

Hence I'm forced to print the lazy field into TRACE log so that it won't clutter the production logs. 因此,我不得不将惰性字段打印到TRACE日志中,以免混乱生产日志。 Doing this will ensure the lazy field is pre-populated before the entities are detached: 这样做将确保在分离实体之前预先填充惰性字段:

LOG.trace(u.getEmails().toString());

However this solution isn't pretty at all. 但是,这种解决方案一点也不漂亮。 Is there any better way to do this? 有什么更好的方法吗?

I don't want to mark the field as EAGER because I have another service method that purposely skips the relationship for efficiency. 我不想将字段标记为EAGER,因为我有另一种服务方法有意跳过关系以提高效率。

Hibernate.initialize(u.getEmails())

Since you are using Hibernate, this is probably going to have to be specific. 由于您正在使用Hibernate,因此这可能必须是特定的。 I'm not aware of any JPA functionality that does this. 我不知道执行此操作的任何JPA功能。 According to the Hibernate Documentation : 根据休眠文档

The static methods Hibernate.initialize() and Hibernate.isInitialized() , provide the application with a convenient way of working with lazily initialized collections or proxies. 静态方法Hibernate.initialize()Hibernate.isInitialized()为应用程序提供了处理延迟初始化的集合或代理的便捷方法。 Hibernate.initialize(cat) will force the initialization of a proxy, cat, as long as its Session is still open. 只要其Session仍处于打开状态, Hibernate.initialize(cat)将强制初始化代理cat。 Hibernate.initialize( cat.getKittens() ) has a similar effect for the collection of kittens. Hibernate.initialize( cat.getKittens() )对于收集小猫具有类似的效果。

This should prevent the compiler from erasing that call, and remove the need for you to do some kind of work with the return value to trick the compiler. 这应该防止编译器擦除该调用,并消除您需要对返回值进行某种工作来欺骗编译器的需要。 So Hibernate.initialize(u.getEmails()) should work for you. 因此, Hibernate.initialize(u.getEmails())应该适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么延迟获取无法正常工作JPA - Why Lazy Fetching is not working JPA 休眠和jpa怪异; 意外的懒惰获取 - hibernate and jpa weirdness; unexpected lazy fetching Jackson 在序列化时触发 JPA Lazy Fetching - Jackson triggering JPA Lazy Fetching on serialization 如何将JPA默认获取更改为LAZY - How to change JPA default fetching to LAZY 如果没有OpenEntityManagerInViewFilter,则无法从JPA2存储库中获取惰性实体 - Fetching lazy entities from JPA2 repository fails without OpenEntityManagerInViewFilter SpringBoot + Hibernate JPA Lazy fetching 模式仍然查询实体中的列表 - SpringBoot + Hibernate JPA Lazy fetching mode still queries lists in an entity JPA - 以编程方式通过序列递增数字字段 - JPA - Increment a numeric field through a sequence programmatically 使用 Spring 数据 JPA EntityGraph with LAZY load mode for NamedAttributeNode field - Using Spring data JPA EntityGraph with LAZY load mode for NamedAttributeNode field JPA,我怎么能有两个查询,一个使用惰性,一个使用渴望获取? - JPA, how can i have two queries, one use lazy and one use eager for fetching? 如何通过jpa注释确定嵌套对象List的运行时提取类型(Eager / Lazy), - How to decide runtime fetching type(Eager/Lazy) of nested object List by jpa annotation,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM