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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:20:10+00:00 2026-06-17T21:20:10+00:00

I tried below code: Student s1 = (Student) s.load(Student.class, 5); // this records not

  • 0

I tried below code:

Student s1 = (Student) s.load(Student.class, 5); // this records not there in DB
 try {
    s1.setName("Kaushik1");
} catch (ObjectNotFoundException e) {
    s1 = new Student();
    s1.setId(5);
    s1.setName("trying1");
}
s.saveOrUpdate(s1);

Student s2 = (Student) s.load(Student.class, 5);
try {
    s2.setName("Kaushik2");
} catch (ObjectNotFoundException e) {
    //Why should it throw ObjectNotFoundException now
    s2 = new Student();
    s2.setId(5);
    s2.setName("trying2");
}
s.saveOrUpdate(s2); //Why it throws NonUniqueObjectException

I used load to fetch record from DB with ID#5. Record does not exist. Then I tried calling setter on object, and it threw an exception. Agreed !!

Since the record did not exist I created a new object and called saveOrUpdate(). So now I assume that object with ID#5 is in session cache.

Now I try again to call load() method for ID#5 and call its setter. It throws ObjectNotFoundException again.

Question 1

Why it can not pick record from Session cache ?

When I create new object and try to call saveOrUpdate() it gives a NonUniqueObjectException

org.hibernate.NonUniqueObjectException: a different object with the
same identifier value was already associated with the session:
[com.cts.closr.Student#5]

It threw ObjectNotFoundException and now says “object already associated with the session”. Is that not contradictory?

Question 2

Does that mean load() method never checks the session context? It always fetches from DB?
Then in such case I must use get() method?

  • 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-06-17T21:20:12+00:00Added an answer on June 17, 2026 at 9:20 pm

    The documentation says:

    If the Session throws an exception, including any SQLException, immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable.

    So using Session.load(), catching an exception, and continuing using the session is a big no-no. Session.load() must be used when you consider that the object MUST exist in the database, and an exception must be thrown if it doesn’t. If you’re not sure that the object exists, use Session.get(), test if the returned object is null, and act accordingly.

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

Sidebar

Related Questions

When i tried using the below code...the td tag is returned and not the
I have tried rectifying the code below. But I am not able to find
I tried below code for cheking SP is alredy exist or not. if not
This is an Extension to the Question . I have tried below code to
HI I wanna timeout session from client page,i tried below code but not able
I tried below code in .htaccess with my Joomla site but it is not
I tried below code but as I run it gives a black screen image
I tried the below code to check whether my mobile is connected to a
I want to send data through URL.I've tried the below code: Intent browserIntent =
I am just learning Perl's comparison operators. I tried the below code :- $foo=291;

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.