Skip to content
Advertisement

Batch fetching into @ElementCollection

I have following entity with its persistent collection

JavaScript

After loading 10k rows A entities, I want to load its collection as well

JavaScript

And select statement generated quite a lot (~10k).

Very poor performance here!

Any idea to work with batch select here?

Advertisement

Answer

I have solved this!

IDEA

Hibernate will take care of the sql statement and mapping to entity list value when using @ElementCollection. That’s comfortable to use but we have a trade off. The more parent results we have, the worse performance we got. If we have 10k records parent, Hibernate will do selecting 10k times to fetch its children relation.

Instead of loading children for every single parent. Create native query to load everything.

we got the results like this:

JavaScript

then implementing Hibernate transformer to convert these raw database objects to DTO.

Code example.

Create DTO

JavaScript

And transformer

JavaScript

DAO layer

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement