Camel JPA XML Configuration with EntityManagers



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

我有我的坚持.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文档所说的,但是这个错误出现了。

有什么想法吗?

我不熟悉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键入EntityManagerFactory

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

根据骆驼JPA文档:

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

因此,您不需要<bean id="jpa"...标签。

此外,你已使用 <to uri="jpa://" /> 作为终结点。 根据 Camel JPA 文档,完全限定的类名是可选的。 但是,我发现指定它是一个好主意。

最新更新