Java.Hibernate.Middle.What means proxies, when we speak of lazy loading?

when I say “proxies,” I do mean entities, but specifically lazy-loaded entities or collections that Hibernate wraps in a proxy object.

Here’s what that means:


🔎 What is a proxy in Hibernate?

  • A proxy is a lightweight placeholder object Hibernate creates instead of immediately loading an entity or collection.
  • When you mark an association as FetchType.LAZY, Hibernate doesn’t load the actual entity right away. Instead, it creates a proxy object that:
    • Looks like your entity in Java (same class or a subclass generated by Hibernate).
    • Only fetches the real data when you first call a getter or otherwise access the proxy.

Example:

@Entity
public class Author {
    @OneToMany(mappedBy = "author", fetch = FetchType.LAZY)
    private List<Book> books; // Hibernate will create a proxy for this collection
}

When you load an Author but don’t touch author.getBooks(), Hibernate just gives you a proxy collection representing books.
When you later access getBooks(), Hibernate triggers a SQL query to load the books.

🔹 So when I say:

Batch fetching works by grouping multiple proxies of the same type waiting for initialization…

I mean:

  • The placeholder proxy objects Hibernate gives you instead of real entities or collections.
  • When you access multiple of these proxies in the same session, Hibernate batches their SQL queries.

🚨 Key point:

  • All lazy-loaded entities or collections are proxies until you access them.
  • If you eagerly load entities, they’re real objects immediately → no proxies → batch fetching has no work to do.
This entry was posted in Без рубрики. Bookmark the permalink.