Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate session.contains( Class clazz, Serializable id )

I want to be able to check whether a session contains an entity of a given class/identifier. I can't see a way to do this at the moment.

  • contains() takes an entity object not class + key
  • get() queries the database if the entity is not present which I don't want to do
  • load() never returns null as a proxy is always created so I can't use this method

Is it possible to do the above with no side-effects/queries to DB?

like image 888
Mike Q Avatar asked Sep 19 '11 15:09

Mike Q


1 Answers

This works:

public boolean isIdLoaded(Serializable id)
{
    for (Object key : getSession().getStatistics().getEntityKeys())
    {
        if (((EntityKey) key).getIdentifier().equals(id))
        {
            return true;
        }
    }

    return false;
}
like image 63
TheReincarnator Avatar answered Oct 23 '22 05:10

TheReincarnator