简体   繁体   中英

How to test-cover a solution of "N+1" problem of ORM?

I've solved N+1 problem in my code by adding lots of annotations (such as @NamedEntityGraph and @Fetch(FetchMode.JOIN) , etc) on many entities, and also properties for Hibernate (such as MAX_FETCH_DEPTH).

How to write a junit test for this new code? I'm using H2 for all other tests involving JPA.

Load the entities, close the session, then assert that accessing state doesn't blow up.

Having said that, this is a perfect use case forBlaze-Persistence Entity Views .

I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.

You don't need to think about entity graphs or fetch strategies on entity level which might be hard anyways if you have multiple use cases, but can focus on creating proper DTOs that expose exactly that state, that you really need.

The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM