Menu

JdoCallback usage in JdoTemplate.execute()

adamle
2004-03-25
2004-06-30
  • adamle

    adamle - 2004-03-25

    I am currently trying to port an application that uses Spring and JDBC to use Spring and JDO.  I have extended JdoDaoSupport and gotten a JdoTemplate using getJdoTemplate().  I am having trouble writing actual JDO code to write or delete objects using JdoCallback.

    I would appreciate a few lines of code showing how to do this.  Just the simplest example to sort things out.

    Thanks in advance.

     
    • Anonymous

      Anonymous - 2004-06-30

      Something like this:

          public User addUser(User newUser) {
              JdoTemplate jdo = getJdoTemplate();
              final User user = newUser;
             
              return (User) jdo.execute(new JdoCallback() {
                  public Object doInJdo(PersistenceManager pm) {
                     
                      pm.currentTransaction().begin();
                     
                      user.setID((int)getMaxID(pm)+1);

                      pm.makePersistent(user);
                      pm.currentTransaction().commit();
                     
                      return user;
                  }
             
              });
          }

       
      • Juergen Hoeller

        Juergen Hoeller - 2004-06-30

        I would move the transaction demarcation out of the DAO, i.e. not do currentTransaction().begin() and currentTransaction().commit() here.

        You can use Spring's generic transaction management features to demarcate transactions at the business facade level, with JdoTransactionManager as transaction strategy. DAOs will automatically participate in such transactions.

        BTW, Spring 1.1 RC1 (currently scheduled for July 11th) will introduce significant enhancements to the JDO support, including convenience operations on JdoTemplate (like makePersistent, deletePersistent, etc) and support for Open PersistenceManager in View.

        Out of curiosity, which JDO implementation are you using?

        Juergen

         

Log in to post a comment.

MongoDB Logo MongoDB