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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:03:31+00:00 2026-05-27T00:03:31+00:00

I have an entity (JPA annotations with Hibernate query interface) which has a composite

  • 0

I have an entity (JPA annotations with Hibernate query interface) which has a composite business key of two properties (an external-id and a subsidiary-id [that’s a foreign key]) and a primary db key.

I have a JavaSE multi-threaded process which runs “with respect to a subsidiary” and needs to query/update/insert those entities using that composite key (it gets datasets of those external-ids).

I can guarantee that once such a process loads no other process will try to insert/update/remove that subsidiary.

What I want is to load all existing entities of that subsidiary on startup and then have every query for a pair of [externalId,subsidiaryId] to go through the cache and only if there’s a miss to go through the db. In case one of the threads from the process inserts an entity I of course want that to be added to that cache.

What’s the best course of action?

I know there’s query caching but from what I can gather that will still be a miss the first time of every [externalId,subsidiaryId] pair.

Thanks and if there’s anything which I’m not clear about please ask

Update
I had to stop investigating this issue but now that I’ve come back to it I think that it’s seems reasonable that only the query cache and JB Nizet’s answer are the only ones applicable.
I’ll accept JB Nizet’s answer as it is interesting and I might use it (not sure yet).

  • 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-27T00:03:31+00:00Added an answer on May 27, 2026 at 12:03 am

    I would use a second-level cache for the entity, query for all the subsidiry’s entities at startup (this will fill the second-level cache), and initialize an application cache which would just map [externalId, subsidiaryId] to [id].

    Then, each time you’re searching for an entity with a given [externalId, subsidiaryId], first get its ID from the application cache, then

    • if the ID is not in the cache, execute the query, update the application cache, and return the found entity
    • if the ID is in the cache, get the entity using its ID, which will go to the second-level cache and avoid hitting the DB
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JPA Entity StatsEntity which has a composite primary key that is
I have a JPA object which has a many-to-many relationship like this: @Entity public
In my business model I have Entity class (IPoint interface ) that has a
I have an application using hibernate 3.1 and JPA annotations. It has a few
A have a JPA entity that has timestamp field and is distinguished by a
We have a project that uses JPA/Hibernate on the server side, the mapped entity
I have an entity that maps to an external oracle table which is one
im new in hibernate and JPA and i have some problems with annotations. My
I've mapped two entities using JPA (specifically Hibernate). Those entities have a one-to-many relationship
In my application I have several entity POJOs annotated with JPA annotations. Also hbm2ddl

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.