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