问题描述
类A实现了生命周期{
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实现了生命周期{
A a;
public boolean onSave(Session session){
a.onUpdate(session);
$ b我修改了一个对象并调用了onUpdate method.but exception每当我调用方法a.onUpdate();
org.hibernate.TransientObjectException:对象引用未保存的瞬态实例 - 保存瞬态在flush之前的实例:com.test.C
我知道上面的异常由于在onUpdate方法中执行查询。请告诉我有没有办法在查询执行期间停止保存未保存的对象。或任何其他建议以上问题是非常有帮助的。
解决方案要严格回答这个问题,默认情况下,Hibernate确实会在执行查询时刷新会话所以你不会得到陈旧的结果。您可以使用自定义 FlushMode
(COMMIT或NEVER)更改此行为。从文档: $ b
但是说实话,我不确定要理解你想做什么(也许这只是一个示例,但是你的HQL查询不正确)。
参考文献
-
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
这篇关于如何在查询执行期间停止会话以保存已保存的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!