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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:17:38+00:00 2026-05-22T14:17:38+00:00

is it possible to mix a session.load(class, id) with a flexible criteria (criteria api)?

  • 0

is it possible to mix a session.load(class, id) with a flexible criteria (criteria api)?

I need something like :

select * from entity where id = 1 AND name = 'miller'

I use the generic approach for the find by id:

public T findById(ID id, boolean lock) {
    T entity;
    if (lock)
        entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
    else
        entity = (T) getSession().load(getPersistentClass(), id);

    return entity;
}

and would like to extend this by

    Criteria criteria = session.createCriteria(getPersistentClass());
    criteria.add( Restrictions.eq("name", "miller"));

where ("name", "miller") will be replaced by something generic.

  • 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-22T14:17:39+00:00Added an answer on May 22, 2026 at 2:17 pm

    Load and criteria can’t be mixed. Load creates a proxy if the instance isn’t in cache yet. The purpose of load (and get) is that its arguments are the keys for the cache (class and id) and it doesn’t need to flush the session before. Criteria is a kind of query. When you already know the id, you don’t need a query anymore. I mean, does id = 1 AND name = 'miller' make any sense?


    Just write you own “Get”:

    public TEntity secureGet<TEntity>(int id, string user)
    {
      // avoid flushing, only when you never expect that the user name
      // in the object to be loaded has changed within the current session.
      // this is very performance relevant.
      session.flushMode = FlushMode.Never;
      return session
        .createCriteria(typeof(TEntity))
        .add( Restrictions.idEq(id))
        .add( Restrictions.eq("name", user))
        .UniqueResult<TEntity>();
      session.flushMode = FlushMode.Auto;
    }
    

    (Sorry for possible syntactic errors, I’m not a java programmer)

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

Sidebar

Related Questions

Is it possible to mix HQL and the Criteria API? I have a String
I'm not sure if something like this is even possible in Python, but if
Is it possible to mix all these access controls in one site? I have
Can anyone tell me if it is possible to mix views arguments with static
Is it possible for ASP.NET to mix up which user is associated with which
Possible Duplicate: How do you send email from a Java app using Gmail? How
Is it possible to mix the concept of Unobtrusive JavaScript with the event model
Is there any possible way to mix Anonymous Authentication with Windows Authentication, or even
Is it possible to have a mix of RIA WCF services and custom WCF
Is is possible to mix C# and VB.NET files in one project when using

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.