I think that when you make an object persistent, the persistence manager
automatically sets the objects pk according to the database.

Have you tried printing the department bean id after you committed the
transaction?

Pedro Salgado

-----Original Message-----
From: Philippe Guillard [mailto:[EMAIL PROTECTED] 
Sent: ter�a-feira, 13 de Abril de 2004 4:02
To: ojb-user
Subject: JDO - getObjectId, how to retrieve the id of identity obj
afterinsert

Hi,

I wanna create a new data object in datastore without precising the 
primary id, while letting database naming the id autoincremently. But in
the same time, i need to know the id attributed to this object, so that 
i can reuse it later. 
I wrote the code in an xsp like following, but i 
got an error saying :
  "org.apache.cocoon.ProcessingException: Exception in 
ServerPagesGenerator.generate(): javax.jdo.JDOUserException: No 
transaction in progress."
(If i only execute the lines before  Object oid = pm.getObjectId(bean); 
the object is created successfully in the DB with an id incrementally. )

Any idee? Another solution? 

thanks,

Phil

*******
<?xml version="1.0"?>
<xsp:page language="java" xmlns:xsp="http://apache.org/xsp";>

  <xsp:structure>                // import class
      <xsp:include>javax.jdo.PersistenceManager</xsp:include>
      <xsp:include>javax.jdo.Transaction</xsp:include>
      
<xsp:include>org.apache.cocoon.ojb.jdo.components.JdoPMF</xsp:include>
      <xsp:include>javax.jdo.Transaction</xsp:include>
      
<xsp:include>org.apache.cocoon.ojb.samples.bean.Department</xsp:include>
  </xsp:structure>

  <xsp:init-page>                // declare
      JdoPMF pmf = null;
      PersistenceManager pm = null;
      PersistenceManager pm2 = null;
      int id;
  </xsp:init-page>

  <page>
      <content>
          <xsp:logic>
          try {
              /* Get the PersistenceManager */
              try {
                  pmf = (JdoPMF) manager.lookup(JdoPMF.ROLE);
                  pm = pmf.getPersistenceManager();
              } catch (ComponentException cme) {
                  getLogger().error("Could not look up the 
PersistenceManager", cme);
              }              
              pm.currentTransaction().begin();              
              Department bean = new Department();
              bean.setName("JDO Development");              
              pm.makePersistent(bean);                      // add obj 
in datastore
              pm.currentTransaction().commit();

              Object oid = pm.getObjectId(bean);            // 
getObjectId of the identity object
              pm.close();
              pm2 = pmf.getPersistenceManager();
              pm2.currentTransaction().begin();
              Department bean2 = (Department) pm.getObjectById(oid,
true);
              id = bean2.getId();

          </xsp:logic>
          <p> Inserted data:  <xsp:expr>id</xsp:expr></p>
          <xsp:logic>
          }
          finally {
              manager.release((Component)pmf);
          }
      </xsp:logic>
      </content>
  </page>
</xsp:page>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to