额外的由Hibernate生成的sql [英] Extra sql generated by Hibernate

查看:93
本文介绍了额外的由Hibernate生成的sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  beginTransaction(); 

使用session.save保存对象如下:
sessionFactory.getCurrentSession()。save(entity);
entity.setName(Entity+ Math.random());
commitTransaction();

生成以下sql:

 


为什么它应该生成单独的更新sql,即使在更改后尽快更新? 解决方案

这是预期的Hibernate行为正如Gavin(其中一位hibernate开发人员)这里所解释的那样:


预期的Hibernate行为(我们争论这是否真正正确!)是INSERT语句将精确地插入保存时设置的数据) 被称为。这给了用户一个更细粒度的控制,特别是在有触发器等情况下。但是,如果你不小心,它可能表现不好。



为了避免INSERT-then-UPDATE,在保存之前完全初始化对象。


但是,如果您不满意这种情况在使用扩展持久化上下文时真的很烦人):Hibernate有一个补丁3.5.6_final 这里来改变这种行为。


I am trying to save an object using session.save as below:

beginTransaction();
sessionFactory.getCurrentSession().save(entity);
entity.setName("Entity " + Math.random());
commitTransaction();

The following sql are generated :

1)insert into entity
2)update set name = 

Why should it generate separate update sql even when changes are flushed as late as possible ?

解决方案

It's the expected Hibernate behavior as explained by Gavin (one of the hibernate developers) here:

Expected Hibernate behavior (we have debated whether this is really correct or not!) is that the INSERT statement will insert exactly the data that was set when save() was called. This gives the user a bit finer grained control, especially in a context where there are triggers etc. However, if you are not careful, it could perform badly.

To avoid the INSERT-then-UPDATE, simply fully initialize the object before saving it.

However, if you are unhappy with that (in my case it was really annoying when using extended persistence context): there is a patch for Hibernate 3.5.6_final here to change this behavior.

这篇关于额外的由Hibernate生成的sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆