Spring+Hibernate的saveOrUpdate问题

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 <version></version>or <timestamp></timestamp>), and the version property value is the same value assigned to a newly instantiated object, save() it

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.这句有点不好理解。恰好就是我的问题所在。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值