• 修改脱管(Detached)对象


    修改脱管(Detached)对象

    很多程序需要在某个事务中获取对象,然后将对象发送到界面层去操作,最后在一个新的事务保存所做的修改。 在高并发访问的环境中使用这种方式,通常使用附带版本信息的数据来保证这些“长“工作单元之间的隔离。

    Hibernate通过提供Session.update()Session.merge() 重新关联脱管实例的办法来支持这种模型。

    // in the first session
    Cat cat = (Cat) firstSession.load(Cat.class, catId);
    Cat potentialMate 
    = new Cat();
    firstSession.save(potentialMate);

    // in a higher layer of the application
    cat.setMate(potentialMate);

    // later, in a new session
    secondSession.update(cat);  // update cat
    secondSession.update(mate); // update mate

    如果具有catId持久化标识的Cat之前已经被另一Session(secondSession)装载了, 应用程序进行重关联操作(reattach)的时候会抛出一个异常。

    如果你确定当前session没有包含与之具有相同持久化标识的持久实例,使用update()。 如果想随时合并你的的改动而不考虑session的状态,使用merge()换句话说,在一个新session中通常第一个调用的是update()方法,以便保证重新关联脱管(detached)对象的操作首先被执行。

    like语句


      
    public Collection findOwners(String lastName) throws DataAccessException {
            
    return getHibernateTemplate().find("from Owner owner where owner.lastName like ?", lastName + "%");
        }



    getHibernateTemplate().find(
    "from Owner owner where owner.lastName like ?", lastName + "%");


     

  • 相关阅读:
    连载日记
    自我介绍
    test0710 二分专题
    test0709 搜索专题
    test0705
    test0704
    [题解] [HNOI2015]落忆枫音
    test0606
    test0523
    备份
  • 原文地址:https://www.cnblogs.com/fjchenq/p/897480.html
Copyright © 2020-2023  润新知