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

The Archive Base Latest Questions

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

I’ve been using my own object cache which works more or less: get objet

  • 0

I’ve been using my own object cache which works more or less:

  1. get objet by an fixed criteria (for example use by login name)
  2. store that object in a hashmap as it is used over and over again
  3. if in another session the same object is requested again i use the same object again.
  4. My objects are changed very rarly, and used extremly often (read only). I have hooks that clear my object cache if anything gets changed in the database.

Now this works pretty well in most situations, but it causes some unwanted database hits in some cases. I’ve identified the following situation:

  • I create an object (new Car())
  • assign a cached object to this object: car.setOwner(cache.lookup(“Pete”)) (this object was fetched from the database in another session 20 minutes ago)
  • then I save the object

At this point hibernate realizes, that that object is ‘dettached’ and during the nullify check it fetches that object from the database again.

Is there a way to avoid that? Can I reattach an object to a session without hitting the database? “Trust me hiberante, this is a valid object, please remember it”.

Below I attach the stacktrace of this situation.

Hibernate Version: 3.3.2.GA

Cheers
Reto

org.hibernate.jdbc.util.SQLStatementLogger.logStatement(SQLStatementLogger.java:115)
org.hibernate.jdbc.AbstractBatcher.log(AbstractBatcher.java:444)
org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:511)
org.hibernate.jdbc.AbstractBatcher.prepareSelectStatement(AbstractBatcher.java:145)
org.hibernate.persister.entity.AbstractEntityPersister.getDatabaseSnapshot(AbstractEntityPersister.java:1034)
org.hibernate.engine.StatefulPersistenceContext.getDatabaseSnapshot(StatefulPersistenceContext.java:269)
org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:212)
org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:160)
org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:92)
org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:70)
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:311)
org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
sun.reflect.GeneratedMethodAccessor271.invoke (Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
  • 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-31T13:22:04+00:00Added an answer on May 31, 2026 at 1:22 pm

    I was able to avoid that unnecessarry database hit during the isNullifiable/isTransient using the following ugly hack.

    Please do not use this without heavily research on the topic / parts involved. In our case adding the empty List to the snapshotHash was no problem, but this can be completly different in another case.

    /* lets say I know that this oject was from another session */
    Object object = ... 
    
    /* extract SessionImpl from session */
    
    InvocationHandler invocationHandler = Proxy.getInvocationHandler(rawSess);
    Field f = invocationHandler.getClass().getDeclaredField("realSession");
    f.setAccessible(true);
    SessionImpl sessImpl = (SessionImpl) f.get(invocationHandler);
    
    /* now extract the entitySnapshotsByKey from the used persistence context */
    PersistenceContext pc = sessImpl.getPersistenceContext();
    
      Field field = StatefulPersistenceContext.class.getDeclaredField("entitySnapshotsByKey");
    field.setAccessible(true);
    
    Map<Object, Object> entitySnapshotsByKey = (Map) field.get(pc);
    
    /* get the internal metadata about that entity */
    String entityName = object.getClass().toString();
    EntityPersister persister = sessImpl.getEntityPersister(entityName, object);
    Serializable id = persister.getIdentifier( object, sessImpl.getEntityMode());
    
    EntityKey key = new EntityKey( id, persister, sessImpl.getEntityMode() );
    
    /* add an empty list, as the content isnt used in our case */
    entitySnapshotsByKey.put(key, new Object[0]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
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
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I would like to run a str_replace or preg_replace which looks for certain words
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.