JDO makePersistent 的一些补充

来源:互联网 发布:visual studio支持php 编辑:程序博客网 时间:2024/06/06 07:41
public void makePersistent(java.lang.Object pc)
Parameters:
pc - a transient instance of a Class that implements PersistenceCapable

这是JDO-javadoc里的对makePersistent的一段定义,PC,是一个瞬时化实例对象.

其实,一个Persistent new 实例也时可以makePersistent的,而一个Persistent Dirty实例则不行

测试代码:

1.持久化Persistent new 对象

 PersistenceManager pm = PersistenceManagerSource.distinctPM();
        Transaction t = pm.currentTransaction();
        t.begin();
        userDAO.setPoClass(User.class);
        try {
            user = (User) userDAO.getObjectById(pm, id);//从DAO里得到Persistent new User对象
         
            pm.makePersistent(user);//持久化操作,本操作不出错

            pm.makeTransient(user);

        } catch (DAOException e) {
            if(t.isActive())
                t.rollback();
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(t.isActive())
                t.commit();
            if (pm != null)
                pm.close();
        }
        return user;

 

2.持久化Persistent dirty对象

PersistenceManager pm = PersistenceManagerSource.distinctPM();
        Transaction t = pm.currentTransaction();
        t.begin();
        userDAO.setPoClass(User.class);
        try {
            user = (User) userDAO.getObjectById(pm, id);//从DAO里得到Persistent new User对象
         

            user.setUserName(“JJYAO“);
            pm.makePersistent(user);//持久化操作,本操作出错           

            pm.makeTransient(user);

        } catch (DAOException e) {
            if(t.isActive())
                t.rollback();
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(t.isActive())
                t.commit();
            if (pm != null)
                pm.close();
        }
        return user;

原创粉丝点击