2007-06-22
Spring+Hibernate的saveOrUpdate问题
关键字: Spring Hibernate saveOrUpdate merge| 1.Spring+Hibernate的saveOrUpdate问题 | Copy to clipboard |
| Posted by: LostParadise Posted on: 2005-06-01 17:06 环境:Spring1.2,Hibernate3.0.5 测试不通过的Test case: public class ProductDaoImplTest extends MDTDAOBaseTestCase { public void testCreateOrUpdateProduct() { ProductDAO dao = (ProductDAO) applicationContext.getBean("productDAO"); Product product = createNewProduct(); product.setId("654321"); dao.createOrUpdateProduct(product); setComplete(); } public Product createNewProduct() { Product vo = new Product(); vo.setComments("test"); vo.setCreationTime(new Date()); vo.setLastUpdateByUser("TestCase"); vo.setLastUpdatedTime(new Date()); vo.setMediaAvailable(new Integer(1)); vo.setStatus(new Integer(1)); vo.setVendorProductID("dgdgfegsfd"); vo.setVendorCode("TS"); vo.setUpc("hssrerew"); return vo; } } 对应的DAO: public class ProductDAOImpl extends org.springframework.orm.hibernate3.support.HibernateDaoSupport implements ProductDAO { public void createProduct(Product product) { getHibernateTemplate().save(product); } public void createOrUpdateProduct(Product product) { getHibernateTemplate().saveOrUpdate(product); } } 说明:update,save等测试过都没有问题的,就是saveOrUpdate有问题,不知道是Spring的问题还是Hibernate的问题。而且在Test case那里只要把dao.createOrUpdateProduct(product);改为dao.createProduct(product);得到正确的结果。我查看错误信息,发现saveOrUpdate一直使用update语句而不是insert(在我的测试里面应该使用insert的)。 由于我对Spring+Hibernate还不是很熟悉,不知道各位碰到到类似的情况或者知道解决方案吗?多谢先! |
|
| 2.Re:Spring+Hibernate的saveOrUpdate问题 [Re: LostParadise] | Copy to clipboard |
| Posted by: jianhua23 Posted on: 2005-06-02 09:20 你已经setId()了,那当然saveOrUpdate会产生相应的update sql语句咯,把setId()去掉,就可以了。 当然你要使用saveOrUpdate的update方法时,必须提供唯一标识符(ID),这样hibernate才能定位数据,进行相应的update操作。 |
|
| 3.Re:Spring+Hibernate的saveOrUpdate问题 [Re: LostParadise] | Copy to clipboard |
| Posted by: LostParadise Posted on: 2005-06-02 12:25 可能不是这个原因,我研究Spring的sample,发现他用的是merge,我试用这个,的确可以(同样我也用了setID的,因为我的ID是策略是assign的)。就是不知道saveOrUpdate为什么不可用,另外merge和saveOrUpdate有什么区别吗? |
|
| 4.Re:Spring+Hibernate的saveOrUpdate问题 [Re: LostParadise] | Copy to clipboard |
| Posted by: jianhua23 Posted on: 2005-06-02 14:11 你改一下Hibernate的*.hbm.xml中ID的设置,改成unsave-value="0",然后再试一下,应该可以的。 关于unsave-value的意义你可以参看Hibernate的Reference,我在这里就不多罗嗦了。 |
|
| 5.Re:Spring+Hibernate的saveOrUpdate问题 [Re: jianhua23] | Copy to clipboard |
| Posted by: think Posted on: 2005-06-02 17:41 对于assigned Id,unsaved-value是没有意义的,可以使用Interceptor.isUnsaved()来区分,对于版本化的PO也可以使用版本区分。 否则的话,saveOrUpdate()方法无法正确区分save or update。 这是2.x的行为;3.x的发生了变化,unsaved-value已不需要,好像不应该出问题的。 |
|
| 6.Re:Spring+Hibernate的saveOrUpdate问题 [Re: LostParadise] | Copy to clipboard |
| Posted by: LostParadise Posted on: 2005-06-03 09:53 嗯,我测试了一下,改成unsave-value="0"后还是有这个问题。 |
|
| 7.Re:Spring+Hibernate的saveOrUpdate问题 [Re: LostParadise] | Copy to clipboard |
| Posted by: jigsaw Posted on: 2005-06-03 14:31 我什么都不懂。。。不过这段抄自refrence里面的话对你可能有点用。。 10.7. Automatic state detection The usage and semantics of saveOrUpdate() seems to be confusing for new users. Firstly, so long as you are not trying to use instances from one session in another new session, you should not need to use update(), saveOrUpdate(), or merge(). Some whole applications will never use either of these methods. Usually update() or saveOrUpdate() are used in the following scenario: the application loads an object in the first session the object is passed up to the UI tier some modifications are made to the object the object is passed back down to the business logic tier the application persists these modifications by calling update() in a second session saveOrUpdate() does the following: if the object is already persistent in this session, do nothing if another object associated with the session has the same identifier, throw an exception if the object has no identifier property, save() it if the object's identifier has the value assigned to a newly instantiated object, save() it if the object is versioned (by a otherwise update() the object and merge() is very different: if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance the persistent instance is returned the given instance does not become associated with the session, it remains detached |
|
| 8.Re:Spring+Hibernate的saveOrUpdate问题 [Re: LostParadise] | Copy to clipboard |
| Posted by: LostParadise Posted on: 2005-06-03 15:35 if the object's identifier has the value assigned to a newly instantiated object, save() it.这句有点不好理解。恰好就是我的问题所在。 |
|
发表评论
- 浏览: 42980 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
最近加入圈子
链接
- Hibernate Tag 参考手册(中文翻译版1.0)
- 闲聊日语张宏
- 《NeoSpeech语音合成英、中、韩、日朗读引擎》
- 《新版中日交流标准日本语广播讲座初级》同步更新,2006年6月5日,更新全部ISO文件,最后更新[MP3!]
- 《番茄花园 Windows All In One DVD 特别版》[ISO]
- 中 国 DOS 联 盟
- 新DOS时代
- 《虚拟工作站 6.0.1 Build 55017》(VMware Workstation 6.0.1 Build 55017)VMware Workstation 6.0.1 Build 55017
- 《经典2D即时战略游戏集》(Classic 2D-RTS Games Collection)完美硬盘版
- 标题: 关于DOS游戏在XP系统下运行的简单总结!, 新版VDMSound和DOSBox已经出炉啦!
- DOSBox / VDMSound常見FAQ
- D - F e n d 簡 單 教 學
- Get It Done with MySQL 5
- 《沪江日语原创作品资料集(新增交响情人梦)》(jp.hjenglish.com)合辑
- SQL语句教程 - 由范例学习SQL语句
- 飞奔·慢行
- 地铁换乘/时间/价钱
最新评论
-
Hibernate3.x调用存储过程 ...
您好!hibernate提供的在*.hbm.xml中配置调用存储过程,关于< ...
-- by cheatsky -
KMP字符串模式匹配详解
我虽然曾经学过,看来一下感觉有些时候还是比较难理解!
-- by zhoujj303030 -
一个老IT人的自白:看十年 ...
我不认同三星有多牛,说白了,我认为高丽棒子做的东西真的一般般
-- by xbwolf -
JavaTiger(Java5.0) 新特 ...
感谢分享,解决了我的困惑
-- by onwulc -
磨刀不误砍柴功—难忘的Ja ...
...
-- by wcj10051891






评论排行榜