JDO makePersistent 的一些补充

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;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章