Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6828521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:23:35+00:00 2026-05-26T22:23:35+00:00

I use JPA with Hibernate implementation. My question is probably basic for experienced hibernate

  • 0

I use JPA with Hibernate implementation. My question is probably basic for experienced hibernate users: what is the most efficient way to write queries based on Hibernate first level cache?

For example, I have entity A and entity B:

@Entity
class A{
      private int ida;
      private int x;
      private String s;
    @OneToMany(mappedBy = "ida", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      private Set<B> Bset;
    }

@Entity
class B{
      private int ida;
      private String s2;
    }

Suppose I have several flows that may happen in the same session:

  1. get A.x
  2. get the whole A entity
  3. check if A contains B with s2="...";

For every one of these queries I can

  • write a specific query that gets A.x / B by ida and s2
    or
  • suppose that Hibernate saves cache and always get A object, or A.Bset() and then java loops to get the needed B inside Bset()

What is the most efficient way?

thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:23:35+00:00Added an answer on May 26, 2026 at 10:23 pm

    For use cases 1 and 2 I would just load the whole entity. Second level cache will work equally well. Unless your A object is really big (has a lot of properties) you won’t see see any difference compared to:

    SELECT a.x FROM A a WHERE a.id = :id
    

    Even worse, using the query above will not take advantage of L2 cache.

    Third use case is more interesting. It heavily depends on your requirements, but the reasonable balance would be to use a query like this:

    SELECT B b
    WHERE b.s2 == :s2
      AND b.a = :a
    

    If the query returns something it means a contains b with given s2. This should be much faster than lazily loading Bset and iterating over it. Consider enabling query cache.

    However if Bset is typically small and you use eager fetching, simple filtering in Java might be better. It really depends on your architecture.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm asking this question given my chosen development frameworks of JPA (Hibernate implementation of),
I use JPA, and Hibernate as its implementation. What maven2 dependencies I need to
I worked a lot with Hibernate as my JPA implementation. In most cases it
Suppose you use JPA with Spring, with Hibernate as JPA implementation. JPA transaction mode
I'm using Hibernate's implementation of JPA. My Maven pom.xml references hibernate-entitymanager . My question
I'm using Hibernate's JPA implementation with MySQL 5.0.67. MySQL is configured to use InnoDB.
I just started reading about JPA and its implementation in Hibernate. To understand the
I'm struggling with real-world use of JPA (Hibernate, EclipseLink, etc) in a Swing desktop
When we write java code we use JPA interfaces as its a java specification
I use JPA and Hibernate for my project. I have two classes with same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.