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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:12:46+00:00 2026-05-16T15:12:46+00:00

I have problem with session and query execution, please see code below. class A

  • 0

I have problem with session and query execution, please see code below.

class A implements Lifecycle{
    public boolean onUpdate(Session session){
        Query test=session.createQuery("select * from Unknown");
        List list=test.list();
        B b=new B();
        session.save(b);
    }
}
class B{
    C c;
    public B(){
        c=new C();
        c.a=new A();
    }
}
class C implements Lifecycle{
    A a;
    public boolean onSave(Session session){
        a.onUpdate(session);
    }
}

I modified A object and called onUpdate method.but exception whenever I call method a.onUpdate();

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.test.C

I know that above exception due to query execution in onUpdate method. please suggest me is there is way to stop to save unsaved objects during queries execution. or any other suggestion to above problem is great helpful.

  • 1 1 Answer
  • 2 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-16T15:12:47+00:00Added an answer on May 16, 2026 at 3:12 pm

    please suggest me is there is way to stop to save unsaved objects during queries execution

    To strictly answer this question, by default Hibernate will indeed flush the session when performing a query so that you don’t get stale results. You can change this behavior using a custom FlushMode (COMMIT or NEVER). From the documentation:

    10.10. Flushing the Session

    Sometimes the Session will execute the
    SQL statements needed to synchronize
    the JDBC connection’s state with the
    state of objects held in memory. This
    process, called flush, occurs by
    default at the following points:

    • before some query executions
    • from org.hibernate.Transaction.commit()
    • from Session.flush()

    The SQL statements are issued in the
    following order:

    • all entity insertions in the same order the corresponding objects were
      saved using Session.save()
    • all entity updates
    • all collection deletions
    • all collection element deletions, updates and insertions
    • all collection insertions
    • all entity deletions in the same order the corresponding objects were
      deleted using Session.delete()

    An exception is that objects using
    native ID generation are inserted when
    they are saved.

    Except when you explicitly flush(),
    there are absolutely no guarantees
    about when the Session executes the
    JDBC calls, only the order in which
    they are executed. However, Hibernate
    does guarantee that the
    Query.list(..) will never return
    stale or incorrect data.

    It is possible to change the default
    behavior so that flush occurs less
    frequently.
    The FlushMode class
    defines three different modes: only
    flush at commit time when the
    Hibernate Transaction API is used,
    flush automatically using the
    explained routine, or never flush
    unless flush() is called explicitly.
    The last mode is useful for long
    running units of work, where a Session
    is kept open and disconnected for a
    long time (see Section 11.3.2,
    “Extended session and automatic
    versioning”
    ).

    sess = sf.openSession();
    Transaction tx = sess.beginTransaction();
    sess.setFlushMode(FlushMode.COMMIT); // allow queries to return stale state
    
    Cat izi = (Cat) sess.load(Cat.class, id);
    izi.setName(iznizi);
    
    // might return stale data
    sess.find("from Cat as cat left outer join cat.kittens kitten");
    
    // change to izi is not flushed!
    ...
    tx.commit(); // flush occurs
    sess.close();
    

    During flush, an exception might occur
    (e.g. if a DML operation violates a
    constraint). Since handling exceptions
    involves some understanding of
    Hibernate’s transactional behavior, we
    discuss it in Chapter 11,
    Transactions and Concurrency
    .

    But honestly, I’m not sure to understand what you’re trying to do (and maybe it’s just a sample but your HQL query is not correct).

    References

    • 10.10. Flushing the Session
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with session and query execution,please see code below. class A implements
I have a nHibernate query like this ICriteria query = session.CreateCriteria(typeof(MyResult)) .Add(Expression.Eq(ResultTypeId, myResult.ResultTypeId)) Problem
i have below code snippet. It throws the exception at line 3 but query
I have problem with session in cakephp.I have one file chat.php that is in
i have a problem with session in my webapplication (asp.net mvc rc2). the application
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I have a bizarre problem. I use a lot of session variables so I
Weired problem! ASP.NET Session expires instantly. In my web.config I have this session settings:
wonder if someone could help me with a little problem. I have session values
I have a problem where a bitwise operation on a query like the following

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.