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

  • Home
  • SEARCH
  • 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 9145483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:31:31+00:00 2026-06-17T10:31:31+00:00

I’m now using the JPA2 @Cacheable annotation on my Entities, and all is working

  • 0

I’m now using the JPA2 @Cacheable annotation on my Entities, and all is working well.

I now need to cache a ManyToOne association.

In classic Hibernate, it was necessary to annotate the association with @Cache.

@org.hibernate.annotations.Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Student> supervisionGroup;

And this works. But it seems we cannot use the JPA2 @Cacheable annotation on anything other than the entity itself.

Am I missing something, or were the JPA committee too dim to realise that associations have to be cached as well as entities? Surely it can’t be the dim option??

  • 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-06-17T10:31:32+00:00Added an answer on June 17, 2026 at 10:31 am

    I think after some research I’m able to answer my own question. The key is that Hibernate dehydrates objects in the 2LC and this will influence whether you need to explicitly cache the associations.

    Please add more details if I’ve missed anything or got any detail wrong.

    Here’s a simplified class structure:

    @Entity
    @Cacheable
    public class Tutor
    {
       @OneToMany(mappedBy="tutor")
       private Set<Student> students;
    }
    
    @Entity
    @Cacheable
    public class Student
    {
       @ManyToOne(fetch=FetchType.LAZY)
       @JoinColumn(name="TUTOR_FK")
       private Tutor tutor;
    }
    

    For Hibernate

    The Second Level Cache (2LC) stores entities in a dehydrated form, eg:

    {1=CacheEntry(Tutor)[1, Jack Daw]}
    {1=CacheEntry(Student)[1, Jane Smith, 1]}    
    

    The final 1 in the Student data is the foreign key to tutor.

    So if you have a reference to Student id 1 and follow the reference to the tutor, we get a cache hit because the foreign key is in the cache. No extra select is needed.

    However, if you go the other way, calling getStudents() on the Tutor, there is no foreign key in the 2LC so a select is needed. (once the select completes, hibernate has the ids and can start hitting the 2LC).

    To avoid this, you will need to add the old org.hibernate.annotations.Cache annotation to the @OneToMany relationship.

    For EclipseLink

    I don’t have an EclipseLink installation to hand so I can’t test this but I understand that EclipseLink stores the 2LC data in the form of the original object graph, so the above isn’t relevant. As per Chris’ answer, the entity is cached with its references so further annotating isn’t needed.

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

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.