I have the following code:
public class Test {
public static void main(String[] args) {
String[] contextPaths = new String[] {"com/test/jpa-spring2-service.xml"};
ApplicationContext context = new ClassPathXmlApplicationContext(contextPaths);
MessageDAO service = (MessageDAO)context.getBean("messageService");
Message message = new Message("test");
service.save(message);
}
}
public class MessageDAO extends JpaDaoSupport implements MessageService {
public Message save(Message message) {
getJpaTemplate().persist(message);
return message;
}
}
But once the save is called there is no new object in the database. My MessageService also has a findAll() which does work. If I change the MessageDAO to:
public class MessageDAO extends JpaDaoSupport implements MessageService {
public Message save(Message message) {
getJpaTemplate().setEntityManager(getJpaTemplate().getEntityManagerFactory().createEntityManager());
getJpaTemplate().getEntityManager().getTransaction().begin();
getJpaTemplate().persist(message);
getJpaTemplate().getEntityManager().getTransaction().commit();
return message;
}
}
it starts to work. But this is not the way it should be. Spring should have loaded the entitymanager which should handle the transaction.
Junit tests do work correctly so my service.xml is ok, and it is a direct copy from the IBM Spring JPA tutorial.
Could anyone explain what I do wrong? I followed the IBM tutorial:
http://www.ibm.com/developerworks/edu/j-dw-java-spring2-i.html
Perhaps this question should be posted in another forum, if so: please advise where to post it.
[Message sent by forum member 'rmorrien' (rmorrien)]
http://forums.java.net/jive/thread.jspa?messageID=228366