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;

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