I am working on a requirement in which user can send messages to multiple people, I have to save those messages in a message table.
I am using JPA entityManager for persisting objects and Hibernate as a persistence provider and Spring declarative transaction management.
Even I am executing the persist() method for three times it is saving only one row in the table. I don't know what should I do to save all the messages. It is not displaying any exception message.
Following is my declarative transaction management configuration in applicationContext.xml
<tx:advice id="txAdvice" >
<tx:attributes>
<tx:method
name="*"
propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut
id="messageServiceOperation"
expression="execution(* com.abhinow.message.services.*.*(..))" />
<aop:advisor
advice-ref="txAdvice"
pointcut-ref="messageServiceOperation" />
</aop:config>
Following is code in my service class MessageService.java
private void saveMultipleMessages(SentMessage message) {
String[] toAddresses = message.getMultipleDestinations().split(",");
for(String to: toAddresses) {
message.setTo(to);
saveMessage(message);
}
}
public void saveMessage(SentMessage message) {
messageRepository.saveSentMessage(message);
}
Following is code in my MessageRepository.java
@Repository
public class MessageRepository {
@PersistenceContext
EntityManager entityManagerFactory;
public void saveSentMessage(SentMessage message) {
entityManagerFactory.persist(message);
}
}
Any help would be appreciated. Thanks in advance