I am using Mule JPA module for retrieving and insert/updating data. I can able retrieve data but not able to update the DB. Its giving no errors and as per the log it seems that
Finally could able to commit transaction using MULE JPA transport, by creating CustomTransactionFactory which starts the transaction.
public class CustomTransactionFactory implements TransactionFactory{
@Override
public Transaction beginTransaction(MuleContext muleContext)
throws TransactionException {
EntityManagerFactory emf = muleContext.getRegistry().lookupObject("entityManagerFactory");
TransactionFactory tf = new JPATransactionFactory();
Transaction tx = tf.beginTransaction(muleContext);
tx.bindResource(emf, emf.createEntityManager());
tx.begin();
return tx;
}
@Override
public boolean isTransacted() {
return true;
}
}
By referring custom transaction manager in In-bound endpoint as below, we can achieve flow level transactions.
<flow name="jpa_exampleFlow1" doc:name="jpa_exampleFlow1">
<http:inbound-endpoint exchange-pattern="request-response" doc:name="HTTP" address="http://localhost:9090/jpa_example">
<custom-transaction action="ALWAYS_BEGIN" factory-ref="transctionManager"/>
Note: Transactional blocks are no more required.
Here your console shows :- Hibernate: insert into contact (EMAIL, FIRSTNAME, LASTNAME) values (?, ?, ?) and your payload is :- {"firstName":"king","lastName":"verma","email":"vermaS@xxx.com","id":9} ... where this id:"9" will be accommodated ? your query has 3 fields firstname,lastname and email ..where as your payload contains firstname,lastname,email and id