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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:23:31+00:00 2026-06-06T14:23:31+00:00

I am testing Hibernate here is the situation and code: public static void main(String[]

  • 0

I am testing Hibernate here is the situation and code:

public static void main(String[] args) {
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Tag tag;

    // (case A)    
    Session session = factory.getCurrentSession();
    Transaction tx = session.beginTransaction();
    tag = (Tag) session.get(Tag.class, 1);
    tag.setName("A");
    tx.commit();
    // session is automatically closed since it is current session and I am committing the transaction
    // session.close();     

    //here the tag object should be detached

    //(case B)
    session = factory.getCurrentSession();
    tx = session.beginTransaction();
    // tag = (Tag) session.merge(tag); // I am not merging
    tag.setName("B"); //changing
    // session.update(tag); 
    tx.commit();
    // session.close();
}

It does not updates for case B (tag.setName("B") does not work).

Then I uncomment session.update(tag); in case B, now it is working. It should give error due to object is not merged to case B transaction.

We may say we are using factory.getCurrentSession() that is why no need to merge it, but if replace it with factory.openSession(); and closing session after each case it is still working (with calling update in case B). So in what sense we call an object is detached?

  • 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-06T14:23:33+00:00Added an answer on June 6, 2026 at 2:23 pm

    case A :
    session is not closed, and object tag is in persistent state and it(tag object) is attached with current session.

    case B :
    here session may be same from first transaction, you change value of tag object which is in Persistent state. Persistent state represents existence of object in permanent storage. There will be connection between the object in memory and in database through the identifier. Any change in either of these two will be reflected in other (when transaction is committed). Persistent state is dependent on session object. First, session has to be open (unclosed)[this is true in your case], and second, the object has to be connected to the session. If either of these two is not true, then the object moves into either transient state or detached stage.

    Object is in detached state in following case :
    Detached state arises when a persistent state object is not connected to a session object. No connection may be because the session itself is closed or the object is moved out of session.

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

Sidebar

Related Questions

I'm using DBUnit for testing DAOs implemented with Hibernate and Spring. I put a
Testing out someone elses code, I noticed a few JSP pages printing funky non-ASCII
I am attempting to do unit testing of my DAO (using Spring and Hibernate).
I'm writing an application that uses JPA for persistence. Currently I'm testing with Hibernate
I have a problem using Hibernate and PostgreSQL for production and HSQLDB for testing.
I followed the instructions at Unit Testing with Junit, Spring and Hibernate – Part
im using JPA2 with Hibernate 3.6.x I have made a simple testing on the
Following on from a question on unit testing my POJO: public class MyPojo{ private
I use Hibernate to generate my database automatically for testing, and I have some
We're running a java/hibernate app going against ORACLE 10g in TESTING. Once in a

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.