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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:44:59+00:00 2026-05-14T08:44:59+00:00

i am experimenting using Hibernate. I am trying to get a User by id

  • 0

i am experimenting using Hibernate.

I am trying to get a User by id this is what i do

public User get(DetachedCriteria dc){
    List<User> users = getHibernateTemplate().findByCriteria(dc);
    if(users != null)
    {
        return users.get(0);
    }
    else return null;
}

but it fails when the user is not in the database. Could you help me to understand how to achieve this?

Thanks

  • 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-14T08:45:00+00:00Added an answer on May 14, 2026 at 8:45 am

    If you want to load something by ID you should really be using either Load or Get. Ayende has a good post on the difference between The difference between Get, Load and querying by id.

    As far as why your query is failing when the user isn’t in the database. It’s because the query will return an empty list, which is different from null. So when you try to access the 0th element of a list that has nothing in it, you’re probably getting an index out of bounds exception.

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

Sidebar

Related Questions

I am new to C++ and experimenting with using pointers to return a values.
I am experimenting with using fragments in my application. I am trying to find
I'm experimenting with using Heroku for Perl, but this question is not specific to
This week I've been trying to learn Spring, JBoss, Maven, JPA and Hibernate and
I am currently using/experimenting with autofac as my IoC controller. Previously to this I
I have been experimenting a little with using UITableView's deselectRowAtIndexPath: to help the user
We use SVN for our source-code revision control and are experimenting using it for
I've been experimenting with using MS Excel 2007 to edit tabular data stored in
I'm experimenting with using html5 and css counters to number the figures in a
I've been experimenting with using Javascript object literal notation vs functions with prototypes 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.