Hibernate: persistence optimization of lengthy ArrayLists

我的未来我决定 提交于 2020-03-23 09:53:18

问题


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 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 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.

Thanks !


回答1:


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.

Take a look at this explanation from a Hibernate developer. You can also see this SO question for alternative using batches.



来源:https://stackoverflow.com/questions/60616661/hibernate-persistence-optimization-of-lengthy-arraylists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!