Hibernate:冗长的ArrayLists的持久性优化 [英] Hibernate: persistence optimization of lengthy ArrayLists
问题描述
我在数据库使用中遇到了多个瓶颈.我正在使用通过Hibernate ORM访问并用Java 8编写的PostgreSQL.
I've come across multiple bottlenecks in my database usage. I'm using PostgreSQL accessed with Hibernate ORM and written in Java 8.
这里是需要优化的类的示例:
Here's a sample of the class that needs optimization :
public class RightEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ElementCollection
@LazyCollection(LazyCollectionOption.FALSE)
private List<Long> readableEntity = new ArrayList<Long>();
}
RightEntity是允许访问Ids列表(Long列表)的对象.问题是,每当我加载此实体时,包含数百个元素的列表可读实体可能需要花费几秒钟的时间才能同时加载.这是一个巨大的瓶颈,因为几乎在我的服务器的每个API和管理器中都使用了它.
RightEntity is an object which allows the access of a list of Ids (List of Long). The problem is whenever I load this entity, the list readableEntity, which contains hundreds of elements, can take a few seconds to load at the same time. This is a huge bottleneck because it is used in almost every API and managers of my server.
我的问题是:如何优化此代码以使其运行更流畅?
My question is : how can I optimize this code to make it run smoother ?
LazyCollectionOption.FALSE与FetchType.Eager相似 我尝试过懒地加载它,它运行起来快得多,但是每当尝试加载列表时,我都会得到一个LazyCollection,所以我在探索替代方法或修复方法.
LazyCollectionOption.FALSE is similar to FetchType.Eager I've tried to load it lazily, it runs a lot faster, but I end up with a LazyCollection whenever I try to load the list, so I'm exploring alternatives or a fix.
谢谢!
推荐答案
有一种JOIN FETCH
机制可以正确访问惰性集合.您可以在JPQL查询中使用它来填充惰性集合.
There is a JOIN FETCH
mechanism for accessing a lazy collection properly. You can use it in your JPQL query to populate a lazy collection.
看看这的解释休眠开发人员.您还可以看到此 SO问题以供替代使用批次.
Take a look at this explanation from a Hibernate developer. You can also see this SO question for alternative using batches.
这篇关于Hibernate:冗长的ArrayLists的持久性优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!