编程语言

img fantasyCoder

JDO makePersistent 的一些补充

发表于2004/10/31 21:25:00  865人阅读

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;

阅读全文
0 0

相关文章推荐

  • Hibernate与JDO肩并肩
    hongtashan11 2011-05-13 11:26 266
  • 项目小结 spring mvc 和jdo
    liuzhigang1237 2011-06-03 13:31 613
  • 如何用JDO查询
    RongALong 2011-02-16 14:14 765
  • JDO数据库编程
    windconvolute 2007-07-29 17:24 90
  • spring-jdo.jar
    alice_0812 2009-10-28 16:22 92
  • jdo2-api-2.3-ec hive hdfs 所需jar
    knight_black_bob 2016-12-30 10:21 2
  • JDO官方文档
    luoyan102 2013-06-19 15:34 11
  • JDO-1
    onlyg 2011-11-30 22:11 997
img
取 消
img