`

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.这句有点不好理解。恰好就是我的问题所在。
分享到:
评论

相关推荐

    第24次课-1 Spring与Hibernate的整合

    Spring提供了很多IoC特性的支持,方便处理大部分典型的Hibernate整合问题。 如:SessionFactory的注入、HibernateTemplate的简化操作、DAO的支持等。 为了更好地与持久层框架整合,Spring还提供了统一的异常处理体系...

    Java求职面试宝典各大公司常考知识点

    Struts+Hibernate+Spring面试题合集及答案 Struts+Hibernate+Spring面试题合集 1 1. Hibernate部分 2 1.1. Hibernate工作原理 2 1.2. 什么是Hibernate的并发机制?怎么处理并发问题? 2 1.3. Hibernate自带的分页...

    Hibernate使用技巧汇总

    HibernateTemplate对Hibernate Session操作进行了封装,而 HibernateTemplate.execute方法则是一封装机制的核心 *在spring的配置文件里,移植了整个hibernate.cfg.xml的内容。

    低清版 大型门户网站是这样炼成的.pdf

    (Struts 2+Spring 2+Hibernate 3).pdf(完整版) 网上有高清版350M的。可以去下 http://115.com/file/be5gwid8 请于下载后 24H 内及时删除!请抱着学习的态度下载此资料。 总共900多页!!!!!!! 第1篇 ...

    Java面试宝典2010版

    9、hibernate中的update()和saveOrUpdate()的区别,session的load()和get()的区别。 10、简述 Hibernate 和 JDBC 的优缺点? 如何书写一个 one to many 配置文件. 11、iBatis与Hibernate有什么不同? 12、写...

    最新Java面试宝典pdf版

    9、hibernate中的update()和saveOrUpdate()的区别,session的load()和get()的区别。 122 10、简述 Hibernate 和 JDBC 的优缺点? 如何书写一个 one to many 配置文件. 122 11、iBatis与Hibernate有什么不同? 122 12、...

    Java面试笔试资料大全

    9、hibernate中的update()和saveOrUpdate()的区别,session的load()和get()的区别。 122 10、简述 Hibernate 和 JDBC 的优缺点? 如何书写一个 one to many 配置文件. 122 11、iBatis与Hibernate有什么不同? 122 12、...

    JAVA面试宝典2010

    9、hibernate中的update()和saveOrUpdate()的区别,session的load()和get()的区别。 122 10、简述 Hibernate 和 JDBC 的优缺点? 如何书写一个 one to many 配置文件. 122 11、iBatis与Hibernate有什么不同? 122 12、...

    Java面试宝典-经典

    9、hibernate中的update()和saveOrUpdate()的区别,session的load()和get()的区别。 122 10、简述 Hibernate 和 JDBC 的优缺点? 如何书写一个 one to many 配置文件. 122 11、iBatis与Hibernate有什么不同? 122 12、...

    java面试题大全(2012版)

    9、hibernate中的update()和saveOrUpdate()的区别,session的load()和get()的区别。 122 10、简述 Hibernate 和 JDBC 的优缺点? 如何书写一个 one to many 配置文件. 122 11、iBatis与Hibernate有什么不同? 122 12、...

    Java面试宝典2012版

    9、hibernate中的update()和saveOrUpdate()的区别,session的load()和get()的区别。 122 10、简述 Hibernate 和 JDBC 的优缺点? 如何书写一个 one to many 配置文件. 122 11、iBatis与Hibernate有什么不同? 122 ...

    java面试宝典2012

    9、hibernate中的update()和saveOrUpdate()的区别,session的load()和get()的区别。 133 10、简述 Hibernate 和 JDBC 的优缺点? 如何书写一个 one to many 配置文件. 133 11、iBatis与Hibernate有什么不同? 133 12、...

    Java面试宝典2012新版

    9、hibernate中的update()和saveOrUpdate()的区别,session的load()和get()的区别。 122 10、简述 Hibernate 和 JDBC 的优缺点? 如何书写一个 one to many 配置文件. 122 11、iBatis与Hibernate有什么不同? 122 12、...

    支持多数据库的ORM框架ef-orm.zip

    事实上针对单个对象的get/load/persist/save/update/merge/saveOrUpdate API和Criteria API本来就为一体,只不过是历史的原因被人为割裂成为两套数据库操作API罢了。  因此,对于关系型数据库而言——Entity和...

    Java 面试宝典

    一. Java 基础部分..................................................................................................................... 7 1、一个".java"源文件中是否可以包括多个类(不是内部类)?...

Global site tag (gtag.js) - Google Analytics