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 4097394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:10:06+00:00 2026-05-20T20:10:06+00:00

I have many to one relation, let’s say User has a Company. Company entity

  • 0

I have many to one relation, let’s say User has a Company. Company entity does not have any other relations.

If I enable second level cache for findAll hibernate query of Company, the second time I reload page (which means that User is loaded and then also list of all Companies is loaded) I get select for each existing company (select … from company where id=?) in hibernate output.
This happens when calling findAll for Company, it looks like this (this is generic method in class which is extended with appropriate type):

return (List<T>) getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                Criteria c = session.createCriteria(persistentClass);
                c.setCacheable(cacheFindAll);

                return c.list();
            }
        });

I am using Jboss TreeCacheProvider in read-only strategy. If I use setCacheable(false) there are no “unwanted” selects.

Why is this happening and how can I eliminate this behavior?

  • 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-20T20:10:07+00:00Added an answer on May 20, 2026 at 8:10 pm

    The call setCacheable() is for enabling the query cache, see javadoc:

    Enable caching of this query result, provided query caching is enabled for the underlying session

    What happens is that the ids of the companies are cached but not the objects itself. If you enable the second level cache these individual company queries will be handled by the second level cache.

    In order to enable second level cache you need to set the hibernate.cache.use_second_level_cache=true in the persistence.xml. Also you need to annotate you’re relations and entities you like to cache in the second level cache.

    @OneToMany
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
    private List<Company> companies;
    

    And entity caching:

    @Entity
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
    public class Company {
        ...
    }
    

    BTW the second level cache will only cache relations and entities you find on id. Queries can’t be cached by the second level cache and will always go to the database (or query cache).

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

Sidebar

Related Questions

i have an entity Entity1 that have one to many relation with Entity2 as
Let's say I have an object with two different one-to-many relations. Much like: Customer
Actually I have a Project entity with a one-to-many relation on ProjectsWorkgroups entity which
Lets say I have two tables - child and parent with many-to-one relation. What
I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
I have a question about Criteria method, one-to-many relation to the database, 'one' is
I have one to many association Student entity to Subject entity. Student entity association
I have a one-many relation: Object ReportCategory: Table("RCA_REPORT_CATEGORY"); Id(r => r.Id, "RCA_ID"); Map(r =>
here are my models. There's keyInfo in many-to-one relation with userInfo. @Entity @Table(name =
I have the below unidirectional Many To One mapping @Entity public class Item implements

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.