Mule JPA持久化不插入或更新

时间:2014-05-25 18:14:05

标签: mule mule-studio mule-module-jpa

我正在使用Mule JPA模块来检索和插入/更新数据。我可以检索数据但无法更新数据库。它没有给出任何错误,根据日志,记录似乎得到了更新。但是:(如果我检查DB没有记录插入。 我相信交易没有得到承诺。

请帮我解决这个问题。

这是我的mflow

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jpa="http://www.mulesoft.org/schema/mule/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jpa http://www.mulesoft.org/schema/mule/jpa/current/mule-jpa.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">

  <spring:beans>
           <spring:import resource="classpath:applicationContext.xml" />
    </spring:beans>


        <jpa:config name="Java_Persistence_API" entityManagerFactory-ref="entityManagerFactory" doc:name="Java Persistence API"/>
    <flow name="jpa-exampleFlow1" doc:name="jpa-exampleFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <logger message="payload ----&gt;&gt;&gt;&gt; #[payload]" level="INFO" doc:name="Logger"/>
     <logger message="#[message.inboundProperties.get(&quot;http.relative.path&quot;)]" level="INFO" doc:name="Logger"/>
        <choice doc:name="Choice">
            <when expression="#[message.inboundProperties.get(&quot;http.relative.path&quot;)==&quot;getContact&quot;]">
                <logger message="in GetContact" level="INFO" doc:name="Logger"/>
                <component class="com.test.test.GetContact" doc:name="Java"/>
                <jpa:query statement="from ContactEO contact where contact.firstName = :firstName" queryParameters-ref="#[payload:]"></jpa:query>
                    <!-- <jpa:query statement="from ContactEO contact where contact.id = :id" queryParameters-ref="#[payload:]"/> -->
            </when>
            <when expression="#[message.inboundProperties.get(&quot;http.relative.path&quot;)==&quot;addContact&quot;]">
                <logger message="In AddContact" level="INFO" doc:name="Logger"/>
                  <component class="com.test.test.AddContact" doc:name="Java"/>
               <transactional action="ALWAYS_BEGIN" doc:name="Transactional">
                 <jpa:persist  entity-ref="#[payload:]" config-ref="Java_Persistence_API" />
               </transactional>
            </when>
        </choice>
         <json:object-to-json-transformer doc:name="Object to JSON"/>
        <logger message="payload ----&gt;&gt;&gt;&gt; #[payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

这是我的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
        version="1.0">

    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">

    </persistence-unit>

</persistence>

这是我的控制台日志

2014-05-25 23:41:38,533 INFO [org.mule.api.processor.LoggerMessageProcessor] - <payload ---->>>> /addContact>
2014-05-25 23:41:38,568 INFO [org.mule.api.processor.LoggerMessageProcessor] - <addContact>
2014-05-25 23:41:38,586 INFO [org.mule.api.processor.LoggerMessageProcessor] - <In AddContact>
Hibernate: insert into contact (EMAIL, FIRSTNAME, LASTNAME) values (?, ?, ?)
2014-05-25 23:41:38,811 INFO [org.mule.api.processor.LoggerMessageProcessor] - <payload ---->>>> {"firstName":"king","lastName":"verma","email":"vermaS@xxx.com","id":9}>

2 个答案:

答案 0 :(得分:0)

这里你的控制台显示: - Hibernate:插入联系人(EMAIL,FIRSTNAME,LASTNAME)值(?,?,?),你的有效负载是: - {&#34; firstName&#34;:&#34; king& #34;&#34; lastName的&#34;:&#34; Verma的&#34;&#34;电子邮件&#34;:&#34; vermaS@xxx.com",&#34; ID&# 34;:9} ......这个id:&#34; 9&#34;会被接纳吗?您的查询有3个字段名字,姓氏和电子邮件 ..其中,您的有效负载包含名字,姓氏,电子邮件和ID

答案 1 :(得分:0)

最后能够通过创建启动事务的CustomTransactionFactory来使用MULE JPA传输提交事务。

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;
}

}

通过在In-bound端点中引用自定义事务管理器,如下所示,我们可以实现流级别事务。

 <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"/>

注意:不再需要交易块。