简体   繁体   English

JBoss保险丝+ JPA /休眠

[英]JBoss Fuse +JPA/Hibernate

I have OSGI bundle in JBoss Fuse 6.2 that imports jar with entities and dao classes. 我在JBoss Fuse 6.2中有OSGI包,该包导入带有实体和dao类的jar。
Jar contains: 罐子包含:
Spring.xml Spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:component-scan base-package="com.company"/>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="jpaData" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <bean id="jpaVendorAdapter"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="POSTGRESQL" />
        <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />


    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> 
        <property name="driverClass" value="org.postgresql.Driver" />  
        <property name="url" value="jdbc:postgresql://localhost:5432/MyDB" />
        <property name="username"  value="postgres" />       
        <property name="password"  value="123456" />        
    </bean> 
</beans>

persistence.xml persistence.xml中

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    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">


   <persistence-unit name="jpaData" transaction-type="RESOURCE_LOCAL" >
       <provider>org.hibernate.ejb.HibernatePersistence</provider>

       <class>com.company.model.entity.Entity</class>          

       <properties>
           <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
           <property name="hibernate.archive.autodetection" value="class"/>
           <property name="hibernate.show_sql" value="true"/>
           <property name="hibernate.format_sql" value="true"/>
           <property name="hbm2ddl.auto" value="update"/>
       </properties>
   </persistence-unit>
</persistence>

Dao class 道课

@Repository
@Transactional
public class JpaEntityDao implements EntityDao {

    @PersistenceContext(unitName="jpaData")
    private EntityManager em;

    //methods that use EntityManager
}

Entity class 实体类

@Entity
@Table(name="entity", schema="public")
public class Entity {
   //fields with setters and getters
}

I want to use JpaEntityDao in my bundle. 我想在我的捆绑包中使用JpaEntityDao。 I imported spring.xml from jar to bundle's spring xml 我从jar导入spring.xml到bundle的spring xml

When I install bundle in Karaf some error appears: 当我在Karaf中安装捆绑软件时,出现一些错误:

16:27:30,684 | ERROR | ExtenderThread-8 | ContextLoaderListener            | 174 - org.springframework.osgi.extender - 1.2.1 | Application context refresh failed (OsgiBundleXmlApplicationContext(bundle=myBundle, config=osgibundle:/META-INF/spring/*.xml))
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [bundle://749.0:1/META-INF/spring/spring.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: jpaData] class or package not found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1514)[167:org.apache.servicemix.bundles.spring-beans:3.2.12.RELEASE_1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)[167:org.apache.servicemix.bundles.spring-beans:3.2.12.RELEASE_1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)[167:org.apache.servicemix.bundles.spring-beans:3.2.12.RELEASE_1]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)[167:org.apache.servicemix.bundles.spring-beans:3.2.12.RELEASE_1]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)[167:org.apache.servicemix.bundles.spring-beans:3.2.12.RELEASE_1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)[167:org.apache.servicemix.bundles.spring-beans:3.2.12.RELEASE_1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)[167:org.apache.servicemix.bundles.spring-beans:3.2.12.RELEASE_1]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1119)[169:org.apache.servicemix.bundles.spring-context:3.2.12.RELEASE_1]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:924)[169:org.apache.servicemix.bundles.spring-context:3.2.12.RELEASE_1]
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:69)[173:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:355)[173:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)[173:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)[173:org.springframework.osgi.core:1.2.1]
    at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132)[174:org.springframework.osgi.extender:1.2.1]
    at java.lang.Thread.run(Thread.java:745)[:1.8.0_65]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: jpaData] class or package not found
    at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1431)[668:org.hibernate.entitymanager:4.2.19.Final-redhat-1]
    at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:1214)[668:org.hibernate.entitymanager:4.2.19.Final-redhat-1]
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1058)[668:org.hibernate.entitymanager:4.2.19.Final-redhat-1]
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:703)[668:org.hibernate.entitymanager:4.2.19.Final-redhat-1]
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:75)[668:org.hibernate.entitymanager:4.2.19.Final-redhat-1]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:290)[716:org.apache.servicemix.bundles.spring-orm:3.2.12.RELEASE_1]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)[716:org.apache.servicemix.bundles.spring-orm:3.2.12.RELEASE_1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1573)[167:org.apache.servicemix.bundles.spring-beans:3.2.12.RELEASE_1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1511)[167:org.apache.servicemix.bundles.spring-beans:3.2.12.RELEASE_1]
    ... 14 more
Caused by: java.lang.ClassNotFoundException: com.company.model.Entity not found by org.hibernate.entitymanager [668]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1556)[org.apache.felix.framework-4.4.1.jar:]
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:77)[org.apache.felix.framework-4.4.1.jar:]
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1993)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)[:1.8.0_65]
    at java.lang.Class.forName0(Native Method)[:1.8.0_65]
    at java.lang.Class.forName(Class.java:348)[:1.8.0_65]
    at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:170)[670:org.hibernate.core:4.2.19.Final-redhat-1]
    at org.hibernate.ejb.Ejb3Configuration.classForName(Ejb3Configuration.java:1348)[668:org.hibernate.entitymanager:4.2.19.Final-redhat-1]
    at org.hibernate.ejb.Ejb3Configuration.addNamedAnnotatedClasses(Ejb3Configuration.java:1420)[668:org.hibernate.entitymanager:4.2.19.Final-redhat-1]
    ... 22 more

It seems that Karaf finds class definition in persistence.xml but can't load Entity class. 似乎Karaf在persistence.xml中找到了类定义,但是无法加载Entity类。
Any suggestions are welcome. 欢迎任何建议。

It seems that spring is bad choice for osgi, and it was confirmed there Using both Blueprint and Spring (Not spring-dm) with OSGi . 对于osgi而言,spring似乎不是一个不错的选择,并且在OSGi中同时使用Blueprint和Spring(不是spring-dm)得到了证实。

So it is necessary to use Apache Aries. 因此有必要使用Apache Aries。 You can find working example jpa+fuse there https://github.com/pires/fabric8-persistence-hibernate 您可以在这里找到工作示例jpa + fuse https://github.com/pires/fabric8-persistence-hibernate
https://github.com/FuseByExample . https://github.com/FuseByExample

As the entity is imported from another jar and loaded by the bundle via the EntityManager, you will probably need to build the jar as a bundle and export the packages and then import the packages using the `maven-bundle-plugin' eg 由于实体是从另一个jar导入的,并通过EntityManager通过捆绑包加载,因此您可能需要将jar打包为捆绑包并导出软件包,然后使用“ maven-bundle-plugin”导入软件包,例如

Jar: 罐:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Import-Package />
            <DynamicImport-Package>*</DynamicImport-Package>
            <Export-Package>com.company.model.*</Export-Package>
        </instructions>
    </configuration>
</plugin>

OSGi Bundle: OSGi捆绑包:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Import-Package>com.company.model.entity</Import-Package>
            <DynamicImport-Package>*</DynamicImport-Package>
            <Export-Package />
        </instructions>
    </configuration>
</plugin>

You can check the bundle imports, exports using the packages:imports , packages:exports . 您可以使用packages:importspackages:exports检查捆绑包的导入和packages:exports It would be even better to create a feature that defines the bundles that it is dependent upon. 创建定义它所依赖的包的功能会更好。

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

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