[ 
https://issues.apache.org/activemq/browse/CAMEL-2980?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=60925#action_60925
 ] 

Christian Mueller edited comment on CAMEL-2980 at 7/26/10 5:30 PM:
-------------------------------------------------------------------

Unfortunately not. I have fixed 
[CAMEL-2982|https://issues.apache.org/activemq/browse/CAMEL-2982] with the new 
'usePersist' option. What I have to change for this issue is something like 
this:

is:
{code}
entityManager.merge(entity); 
{code}

shall:
{code}
Object managedEntity = entityManager.merge(entity);
exchange.getIn().setBody(managedEntity);
{code}

But this is not so trivial, because the input could be not only an annotated 
entity, it could also be a list, map, ... and in the current implementation we 
iterate over a collection (which may be only have one entry):

{code}
public void process(Exchange exchange) {
  exchange.getIn().setHeader(JpaConstants.JPA_TEMPLATE, endpoint.getTemplate());
  final Object values = expression.evaluate(exchange, Object.class);
  if (values != null) {
    template.execute(new JpaCallback() {
      public Object doInJpa(EntityManager entityManager) throws 
PersistenceException {
        Iterator iter = ObjectHelper.createIterator(values);
          Object value = iter.next();
          if (endpoint.isUsePersist()) {
            entityManager.persist(value);
          } else {
            entityManager.merge(value);
          }
        }
        if (endpoint.isFlushOnSend()) {
          entityManager.flush();
        }
        return null;
      }
    });
  }
  exchange.getIn().removeHeader(JpaConstants.JPA_TEMPLATE);
} 
{code}

Christian

      was (Author: muellerc):
    Unfortunately not. I have fixed 
[CAMEL-2982|https://issues.apache.org/activemq/browse/CAMEL-2982] with the new 
'usePersist' option. What I have to change for this issue is something like 
this:

is:
{code}
entityManager.persist(entity); 
{code}

shall:
{code}
Object managedEntity = entityManager.persist(entity);
exchange.getIn().setBody(managedEntity);
{code}

But this is not so trivial, because the input could be not only an annotated 
entity, it could also be a list, map, ... and in the current implementation we 
iterate over a collection (which may be only have one entry):

{code}
public void process(Exchange exchange) {
  exchange.getIn().setHeader(JpaConstants.JPA_TEMPLATE, endpoint.getTemplate());
  final Object values = expression.evaluate(exchange, Object.class);
  if (values != null) {
    template.execute(new JpaCallback() {
      public Object doInJpa(EntityManager entityManager) throws 
PersistenceException {
        Iterator iter = ObjectHelper.createIterator(values);
          Object value = iter.next();
          if (endpoint.isUsePersist()) {
            entityManager.persist(value);
          } else {
            entityManager.merge(value);
          }
        }
        if (endpoint.isFlushOnSend()) {
          entityManager.flush();
        }
        return null;
      }
    });
  }
  exchange.getIn().removeHeader(JpaConstants.JPA_TEMPLATE);
} 
{code}

Christian
  
> camel-jpa doesn't use EntityManager.merge(entity) in the right way
> ------------------------------------------------------------------
>
>                 Key: CAMEL-2980
>                 URL: https://issues.apache.org/activemq/browse/CAMEL-2980
>             Project: Apache Camel
>          Issue Type: Bug
>          Components: camel-jpa
>    Affects Versions: 2.4.0
>         Environment: All
>            Reporter: Christian Mueller
>            Assignee: Christian Mueller
>             Fix For: 2.5.0
>
>
> The EntityManager.merge(entity) method returns the merged entity. The 
> exchange in message body should be updated with the merged entity (what 
> camel-jpa currently not do).
> I running into this problem, because after saving an entity in the database, 
> the id field was still null. So, we don't know, which entity in the database 
> is the corresponding to this entity... :-(
> Have a look in the process method of 
> [JpaProducer|http://svn.apache.org/viewvc/camel/trunk/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java?view=markup]
>  and the Java doc from the 
> [EntityManager|http://download.oracle.com/docs/cd/E17477_01/javaee/5/api/javax/persistence/EntityManager.html#merge%28T%29].
> Christian

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to