How @BatchSize works in Hibernate deep explanation?

I read related topics about BatchSize from the Net and from the forum but I still do not understand some parts. So lets describe what I understand and what do not understand. Batch fetching: an optimization strategy for select fetching. Hibernate retrieves a batch of entity instances or collections in a single SELECT by specifying a list of primary or foreign keys. And lets have JPA 2.0, with Hibernate implementation. And these entities: <code> @Entity public class Product{ @ManyToOne(optional = false, fetch = FetchType.LAZY) @BatchSize(size = 50) private Manufacturer manufacturer; ... } </code> <code> @Entity public class Manufacturer { @Id private long id; private String name; ... } </code> So I have Lazy fetching on Manufacturer in the Product. So when I execute select fetching is done. So I have One-Many <=>Manufacturer - Product relation. And I want to find all Manufacturer which names contains "oak".SO I write: <code> SELECT * FROM product as p join manufacturer as m on where like '%oak%' </code> So I do not understand how batchsize helps me. Will Hibernate generate 100 selects or just one(that I wrote)? Or 2-to with select TOP 50 items in it?
1 answer

@BatchSize explaination

first of all. your @BatchSize annotation must be placed on the Manufacturer-Entity. This annotation can only be placed on Collection-fields. assuming that you corrected that ...

if you take this jpql query

SELECT p FROM product as p where like :name

hibernate will execute 1 select for the products and as soon as you access the first manufacturer it will make a select for up to 50 different manufacturers.

select ... from manufacturere where ID in (?, ?, ?, ?, ...)

if your result has more than 50 different manufactuers and you iterate over it the next query with such an in clause will be executed as soon as you try to access a manufactorer that was not retrived in a previous batch-select.