【问题标题】:Camel JPA XML Configuration with EntityManagersCamel JPA XML 配置与 EntityManagers
【发布时间】:2015-01-19 05:47:09
【问题描述】:

我正在尝试使用 JPA 将实体保存到使用 Camel 的数据库中。

我的 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="my-pu">
     <description>My Persistence Unit</description>
     <class>org.bencompany.camel.JabberMessage</class>
     <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
     <properties>
       <property name="openjpa.ConnectionURL"   value="mysql://localhost/jabber"/>
       <property name="openjpa.ConnectionDriverName" value="org.mysql.jdbc.Driver"/>
       <property name="javax.persistence.jdbc.user" value="root"/>
       <property name="javax.persistence.jdbc.password" value="root"/>
       <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
        <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
     </properties>
   </persistence-unit>

</persistence>

我的骆驼/豆类 .xml 是这样的:

<?xml version="1.0"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
             http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="my-pu" />
    </bean>

    <bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="myProcessor" class="org.bencompany.camel.JabberProcessor" />

    <camelContext xmlns="http://camel.apache.org/schema/blueprint"
        xmlns:order="http://fusesource.com/examples/order/v7" id="cbr-example-context">

        <route id="sendMessage">
            <from uri="file:work/cbr/input" />
            <log message="Sending Message: ${body}" />
            <to uri="xmpp://benco@xxx.com/?room=benco@conference.xxx.com&amp;password=xx&amp;nickname=bencamelbot" />
        </route>

        <route id="recieveMessage">
            <from uri="xmpp://benco@xxx.com/?room=benco@conference.xxx.com&amp;password=xx&amp;nickname=bencamelbot" />
            <to uri="myProcessor" />
            <to uri="jpa://" />
        </route>

    </camelContext>
</blueprint>

我正在使用蓝图,因为我试图将它部署到 JBoss Fuse 上。我一直在使用以下链接作为参考,并一直关注它:https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.0/html/EIP_Component_Reference/files/_IDU_JPA.html

但是当我尝试部署我的应用程序时,我收到了这个错误。

org.osgi.service.blueprint.container.ComponentDefinitionException: Error setting property: PropertyDescriptor <name: entityManagerFactory, getter: class org.apache.camel.component.jpa.JpaComponent.getEntityManagerFactory(), setter: [class org.apache.camel.component.jpa.JpaComponent.setEntityManagerF
actory(interface javax.persistence.EntityManagerFactory)]
Caused by: java.lang.Exception: Unable to convert value org.springframework.orm.jpa.LocalEntityManagerFactoryBean@3ce0f4c8 to type javax.persistence.EntityManagerFactory

LocalEntityManagerFactoryBean 应该创建一个 EntityManagerFactory,我正在按照 JBoss / Camel 文档所说的那样做,但是这个错误即将出现。

有什么想法吗?

【问题讨论】:

  • 你是如何在课堂上注入 EM(F) 的?
  • 我还没有注入它——我正在尝试使用 Camel,所以不需要上课。处理器只是将其转换为实体。错误来自创建 org.apache.camel.component.jpa.JpaComponent
  • 我在这里遇到了同样的问题。看来文档中的示例是错误的。你找到解决办法了吗?

标签: java spring jpa jboss blueprint


【解决方案1】:

我不熟悉 Apache Camel + Blueprint。 Springs LocalEntityManagerFactoryBean 本身并没有实现 javax.persistence.EntityManager ,而是提供了获取它的方法。 http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-orm/4.1.1.RELEASE/org/springframework/orm/jpa/LocalEntityManagerFactoryBean.java#LocalEntityManagerFactoryBean

由于我的研究,我发现这个 stackoverflow 问题可能是重复的:ServiceMix / JPA Integration - LocalContainerEntityManagerFactoryBean to type EntityManagerFactory

您的 OSGi 容器内似乎应该有机制(JPA 和 JTA 功能)应该为您完成工作。

【讨论】:

    【解决方案2】:

    the camel JPA documentation

    在 Camel 2.3 中,JpaComponent 将自动从注册表中查找 EntityManagerFactory,这意味着您无需在 JpaComponent 上进行配置

    所以你不需要&lt;bean id="jpa"... 标签。

    另外,您使用&lt;to uri="jpa://" /&gt; 作为端点。根据the camel JPA documentation,完全限定的类名是可选的。但是我发现指定它是个好主意。

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 2013-04-22
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 2011-02-19
      • 2010-12-11
      • 2016-10-24
      • 2014-09-22
      相关资源
      最近更新 更多