今天有网友问到关于Liferay的updateXxxx(entity)方法比较怪异的问题,比如UserPersistanceImpl.update(user, false).
光看源码,好像根本就没有执行save or saveOrUpdate或者merge等Hibernate的方法,那又是如何保存的呢?
分析了一下,做了一个简单的research,发现还真是很有意思的。
http://www.liferay.com/web/guest/community/forums/-/message_boards/message/569279
其中Ray提到:smilingleo原创, www.smilingleo.cn
Not true! We use the parameterless call everywhere in our core
services... and the reason is DOES work is because the object IS in the
hibernate session and flushing the changes is handled by hibernate...
using merge() basically tell hibernate NOT TO WAIT for it's natural
Flush to be invoked. merge() forces a cache flush...
也就是说,只要执行一个flush()方法就可以实现物理层与内存中的同步了,而不再需要更加耗费资源的merge方法了。smilingleo原创, www.smilingleo.cn
此外,还涉及到一些Spring的事务的知识,可以查阅一下《Spring in Action》第5章内容,或者网上一些文章,比如:
http://blog.csdn.net/luying777/archive/2008/04/29/2344557.aspx
http://itfuture.javaeye.com/blog/182201
这里面还真的很有研究的必要的。
在Liferay中,一般对一个entry进行update操作时,都是通过XXXLocalServiceUtil.getEntry(entryId)获取到entry的实例,这个时候entry是persistent的。所以,在更新时不再需要saveOrUpdate或者merge,通过flush就可以实现更新了。