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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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) {
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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;
}
});
}
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