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.
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: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