JDO makePersistent 的一些补充

类别:Java 点击:0 评论:0 推荐:

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;

本文地址:http://com.8s8s.com/it/it14038.htm