简体   繁体   English

Mule JPA持久性未插入或更新

[英]Mule JPA persist is not inserting or updating

I am using Mule JPA module for retrieving and insert/updating data. 我正在使用Mule JPA模块来检索和插入/更新数据。 I can able retrieve data but not able to update the DB. 我可以检索数据,但不能更新数据库。 Its giving no errors and as per the log it seems that the record got updated. 它没有给出任何错误,并且根据日志,似乎记录已更新。 But :( If I check DB no recorded is getting inserted. I believe that the transaction is not getting committed. 但是:(如果我检查数据库,则不会插入任何记录。我认为该事务未提交。

Please help me how to achieve this issue. 请帮助我如何解决此问题。

Here is my mflow 这是我的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>

Here is my persistence.xml 这是我的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>

Here is my console log 这是我的控制台日志

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

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 ? 您的控制台在这里显示:-休眠:插入联系人(EMAIL,FIRSTNAME,LASTNAME)值(?,?,?),并且您的有效载荷为:-{“ firstName”:“ king”,“ lastName”:“ verma”,“ email“:” vermaS@xxx.com“,” id“:9} ...将在其中容纳此id:” 9“吗? your query has 3 fields firstname,lastname and email ..where as your payload contains firstname,lastname,email and id 您的查询包含3个字段firstname,lastname和email ..,其中您的有效载荷包含firstname,lastname,email和id

Finally could able to commit transaction using MULE JPA transport, by creating CustomTransactionFactory which starts the transaction. 最后,通过创建启动事务的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;
}

} }

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. 注意:不再需要事务块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM