ClassNotFoundException:尝试加载 class 时检测到循环 - 在实体 类(在 Karaf 中启动 JPA 包时)

ClassNotFoundException: Cycle detected while trying to load class - at Entity Classes (When starting JPA bundle in Karaf)

我的问题与启动 jpa-only bundle 时的 "Cycle detected while trying to load class" 错误有关。

我用 maven 原型创建了一个 JPA 包,并修改它的 pom.xml 以与 Karaf (4.0.1), Aries Blueprint, MySQL, OpenJPA, OpenJDK 1.8. 实体 class 一起使用,其中使用 eclipse-tooling 生成也添加到 persistence.xml

我能够编译它并在 Karaf 上安装它而不会出错。启动捆绑包时出现错误。这 classes 运行 在 Karaf 2.x (Geronimo App Server).

我怀疑它可能与 classes 中的 @OneToMany/@ManyToOne 注释字段有关,因为如果我只生成一个没有连接的 class,捆绑将完美启动。我也尝试了 pax-jdbc-mysql 方式(不使用蓝图),但除了成功创建的 dataSource 之外,结果相同。我的 classes 只有 jpa 个注释。

包结构如下:

bse.jpa\
   - src/main/java/mycustom-path/entities
   - src/main/resources/META-INF
       MANIFEST.MF
       persistence.xml
   - src/main/resources/OSGI-INF/blueprint
       datasource.xml

Class定义示例:

@Entity
@Table(name="business_entities")
@NamedQuery(name="BusinessEntity.findAll", query="SELECT b FROM BusinessEntity b")
public class BusinessEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(unique=true, nullable=false)
    private Integer identity;

    @Column(length=1000)
    private String address;

    @Column(length=150)
    private String city;

    @Column(length=1000)
    private String comments;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="date_created")
    private Date dateCreated;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="date_updated")
    private Date dateUpdated;

    @Column(length=200)
    private String description;

    @Column(length=75)
    private String email;

    @Column(length=45)
    private String fax;

    private Integer idcompany;

    private Integer idcountry;

    private Integer iduser;

    @Column(name="iduser_updated")
    private Integer iduserUpdated;

    @Column(length=200)
    private String name;

    @Column(length=45)
    private String phone;

    @Column(length=45)
    private String postalcode;

    @Column(name="tax_id", length=20)
    private String taxId;

    @Column(length=500)
    private String website;

    //bi-directional many-to-one association to BusinessEntityContact
    @OneToMany(mappedBy="businessEntity")
    private List<BusinessEntityContact> businessEntityContacts;

    //bi-directional many-to-one association to CustomerExtInfo
    @OneToMany(mappedBy="businessEntity")
    private List<CustomerExtInfo> customerExtInfos;

    //bi-directional many-to-many association to EntityCategory
    @ManyToMany(mappedBy="businessEntities")
    private List<EntityCategory> entityCategories;

    //bi-directional many-to-one association to EntityAccount
    @OneToMany(mappedBy="businessEntity")
    private List<EntityAccount> entityAccounts;

    //bi-directional many-to-one association to ProviderExtInfo
    @OneToMany(mappedBy="businessEntity")
    private List<ProviderExtInfo> providerExtInfos;

    public BusinessEntity() {
    }

    public Integer getIdentity() {
        return this.identity;
    }

    public void setIdentity(Integer identity) {
        this.identity = identity;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getComments() {
        return this.comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public Date getDateCreated() {
        return this.dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public Date getDateUpdated() {
        return this.dateUpdated;
    }

    public void setDateUpdated(Date dateUpdated) {
        this.dateUpdated = dateUpdated;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFax() {
        return this.fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public Integer getIdcompany() {
        return this.idcompany;
    }

    public void setIdcompany(Integer idcompany) {
        this.idcompany = idcompany;
    }

    public Integer getIdcountry() {
        return this.idcountry;
    }

    public void setIdcountry(Integer idcountry) {
        this.idcountry = idcountry;
    }

    public Integer getIduser() {
        return this.iduser;
    }

    public void setIduser(Integer iduser) {
        this.iduser = iduser;
    }

    public Integer getIduserUpdated() {
        return this.iduserUpdated;
    }

    public void setIduserUpdated(Integer iduserUpdated) {
        this.iduserUpdated = iduserUpdated;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return this.phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPostalcode() {
        return this.postalcode;
    }

    public void setPostalcode(String postalcode) {
        this.postalcode = postalcode;
    }

    public String getTaxId() {
        return this.taxId;
    }

    public void setTaxId(String taxId) {
        this.taxId = taxId;
    }

    public String getWebsite() {
        return this.website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public List<BusinessEntityContact> getBusinessEntityContacts() {
        return this.businessEntityContacts;
    }

    public void setBusinessEntityContacts(List<BusinessEntityContact> businessEntityContacts) {
        this.businessEntityContacts = businessEntityContacts;
    }

    public BusinessEntityContact addBusinessEntityContact(BusinessEntityContact businessEntityContact) {
        getBusinessEntityContacts().add(businessEntityContact);
        businessEntityContact.setBusinessEntity(this);

        return businessEntityContact;
    }

    public BusinessEntityContact removeBusinessEntityContact(BusinessEntityContact businessEntityContact) {
        getBusinessEntityContacts().remove(businessEntityContact);
        businessEntityContact.setBusinessEntity(null);

        return businessEntityContact;
    }

    public List<CustomerExtInfo> getCustomerExtInfos() {
        return this.customerExtInfos;
    }

    public void setCustomerExtInfos(List<CustomerExtInfo> customerExtInfos) {
        this.customerExtInfos = customerExtInfos;
    }

    public CustomerExtInfo addCustomerExtInfo(CustomerExtInfo customerExtInfo) {
        getCustomerExtInfos().add(customerExtInfo);
        customerExtInfo.setBusinessEntity(this);

        return customerExtInfo;
    }

    public CustomerExtInfo removeCustomerExtInfo(CustomerExtInfo customerExtInfo) {
        getCustomerExtInfos().remove(customerExtInfo);
        customerExtInfo.setBusinessEntity(null);

        return customerExtInfo;
    }

    public List<EntityCategory> getEntityCategories() {
        return this.entityCategories;
    }

    public void setEntityCategories(List<EntityCategory> entityCategories) {
        this.entityCategories = entityCategories;
    }

    public List<EntityAccount> getEntityAccounts() {
        return this.entityAccounts;
    }

    public void setEntityAccounts(List<EntityAccount> entityAccounts) {
        this.entityAccounts = entityAccounts;
    }

    public EntityAccount addEntityAccount(EntityAccount entityAccount) {
        getEntityAccounts().add(entityAccount);
        entityAccount.setBusinessEntity(this);

        return entityAccount;
    }

    public EntityAccount removeEntityAccount(EntityAccount entityAccount) {
        getEntityAccounts().remove(entityAccount);
        entityAccount.setBusinessEntity(null);

        return entityAccount;
    }

    public List<ProviderExtInfo> getProviderExtInfos() {
        return this.providerExtInfos;
    }

    public void setProviderExtInfos(List<ProviderExtInfo> providerExtInfos) {
        this.providerExtInfos = providerExtInfos;
    }

    public ProviderExtInfo addProviderExtInfo(ProviderExtInfo providerExtInfo) {
        getProviderExtInfos().add(providerExtInfo);
        providerExtInfo.setBusinessEntity(this);

        return providerExtInfo;
    }

    public ProviderExtInfo removeProviderExtInfo(ProviderExtInfo providerExtInfo) {
        getProviderExtInfos().remove(providerExtInfo);
        providerExtInfo.setBusinessEntity(null);

        return providerExtInfo;
    }

}

我得到的错误是:

2017-12-08 07:30:22,514 | ERROR | lixDispatchQueue | container                        | 108 - org.apache.aries.jpa.container - 2.6.1 | FrameworkEvent ERROR - org.apache.aries.jpa.container
org.osgi.framework.hooks.weaving.WeavingException: Weaving failure on class cbn.bse.jpa.persistence.entities.EntityAccount in bundle bse.jpa/1.0.0.SNAPSHOT using transformer org.apache.openjpa.persistence.PersistenceProviderImpl$ClassTransformerImpl@4d3cfedd
    at org.apache.aries.jpa.container.weaving.impl.JPAWeavingHook.transformClass(JPAWeavingHook.java:101)[108:org.apache.aries.jpa.container:2.6.1]
    at org.apache.aries.jpa.container.weaving.impl.JPAWeavingHook.weave(JPAWeavingHook.java:65)[108:org.apache.aries.jpa.container:2.6.1]
    at org.apache.felix.framework.util.SecureAction.invokeWeavingHook(SecureAction.java:1203)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.transformClass(BundleWiringImpl.java:2370)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2057)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1518)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:79)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1958)[org.apache.felix.framework-5.6.4.jar:]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)[:1.8.0_151]
    at java.lang.Class.forName0(Native Method)[:1.8.0_151]
    at java.lang.Class.forName(Class.java:348)[:1.8.0_151]
    at org.apache.openjpa.meta.MetaDataRepository.classForName(MetaDataRepository.java:1559)[165:org.apache.openjpa:2.4.1]
    at org.apache.openjpa.meta.MetaDataRepository.loadPersistentTypesInternal(MetaDataRepository.java:1535)[165:org.apache.openjpa:2.4.1]
    at org.apache.openjpa.meta.MetaDataRepository.loadPersistentTypes(MetaDataRepository.java:1513)[165:org.apache.openjpa:2.4.1]
    at org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:279)[165:org.apache.openjpa:2.4.1]
    at org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:235)[165:org.apache.openjpa:2.4.1]
    at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:211)[165:org.apache.openjpa:2.4.1]
    at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:154)[165:org.apache.openjpa:2.4.1]
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:226)[165:org.apache.openjpa:2.4.1]
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:153)[165:org.apache.openjpa:2.4.1]
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.getProperties(EntityManagerFactoryImpl.java:108)[165:org.apache.openjpa:2.4.1]
    at org.apache.aries.jpa.support.osgi.impl.EMFTracker.getTransactionType(EMFTracker.java:96)[109:org.apache.aries.jpa.support:2.6.1]
    at org.apache.aries.jpa.support.osgi.impl.EMFTracker.addingService(EMFTracker.java:78)[109:org.apache.aries.jpa.support:2.6.1]
    at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:941)[org.osgi.core-6.0.0.jar:]
    at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:870)[org.osgi.core-6.0.0.jar:]
    at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:256)[org.osgi.core-6.0.0.jar:]
    at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:229)[org.osgi.core-6.0.0.jar:]
    at org.osgi.util.tracker.ServiceTracker$Tracked.serviceChanged(ServiceTracker.java:901)[org.osgi.core-6.0.0.jar:]
    at org.apache.felix.framework.EventDispatcher.invokeServiceListenerCallback(EventDispatcher.java:990)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.EventDispatcher.fireEventImmediately(EventDispatcher.java:838)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.EventDispatcher.fireServiceEvent(EventDispatcher.java:545)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.Felix.fireServiceEvent(Felix.java:4579)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.Felix.registerService(Felix.java:3571)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.BundleContextImpl.registerService(BundleContextImpl.java:348)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.felix.framework.BundleContextImpl.registerService(BundleContextImpl.java:355)[org.apache.felix.framework-5.6.4.jar:]
    at org.apache.aries.jpa.container.impl.ManagedEMF.createAndPublishEMF(ManagedEMF.java:132)[108:org.apache.aries.jpa.container:2.6.1]
    at org.apache.aries.jpa.container.impl.ManagedEMF.updated(ManagedEMF.java:125)[108:org.apache.aries.jpa.container:2.6.1]
    at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updated(ManagedServiceTracker.java:189)[3:org.apache.felix.configadmin:1.8.14]
    at org.apache.felix.cm.impl.helper.ManagedServiceTracker.updateService(ManagedServiceTracker.java:152)[3:org.apache.felix.configadmin:1.8.14]
    at org.apache.felix.cm.impl.helper.ManagedServiceTracker.provideConfiguration(ManagedServiceTracker.java:85)[3:org.apache.felix.configadmin:1.8.14]
    at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.provide(ConfigurationManager.java:1463)[3:org.apache.felix.configadmin:1.8.14]
    at org.apache.felix.cm.impl.ConfigurationManager$ManagedServiceUpdate.run(ConfigurationManager.java:1419)[3:org.apache.felix.configadmin:1.8.14]
    at org.apache.felix.cm.impl.UpdateThread.run0(UpdateThread.java:141)[3:org.apache.felix.configadmin:1.8.14]
    at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:109)[3:org.apache.felix.configadmin:1.8.14]
    at java.lang.Thread.run(Thread.java:748)[:1.8.0_151]
Caused by: <openjpa-2.4.1-r422266:1730418 nonfatal general error> org.apache.openjpa.util.GeneralException: cbn/bse/jpa/persistence/entities/EntityAccount
    at org.apache.openjpa.enhance.PCClassFileTransformer.transform0(PCClassFileTransformer.java:172)
    at org.apache.openjpa.enhance.PCClassFileTransformer.transform(PCClassFileTransformer.java:128)
    at org.apache.openjpa.persistence.PersistenceProviderImpl$ClassTransformerImpl.transform(PersistenceProviderImpl.java:292)
    at org.apache.aries.jpa.container.weaving.impl.JPAWeavingHook.transformClass(JPAWeavingHook.java:86)[108:org.apache.aries.jpa.container:2.6.1]
    ... 44 more
Caused by: java.lang.NoClassDefFoundError: cbn/bse/jpa/persistence/entities/EntityAccount
    at java.lang.Class.getDeclaredMethods0(Native Method)[:1.8.0_151]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)[:1.8.0_151]
    at java.lang.Class.getDeclaredMethods(Class.java:1975)[:1.8.0_151]
    at org.apache.openjpa.lib.util.J2DoPrivHelper.run(J2DoPrivHelper.java:334)
    at org.apache.openjpa.lib.util.J2DoPrivHelper.run(J2DoPrivHelper.java:332)
    at java.security.AccessController.doPrivileged(Native Method)[:1.8.0_151]
    at org.apache.openjpa.persistence.PersistenceMetaDataDefaults.determineImplicitAccessType(PersistenceMetaDataDefaults.java:366)
    at org.apache.openjpa.persistence.PersistenceMetaDataDefaults.determineAccessType(PersistenceMetaDataDefaults.java:332)
    at org.apache.openjpa.persistence.PersistenceMetaDataDefaults.populate(PersistenceMetaDataDefaults.java:274)
    at org.apache.openjpa.meta.MetaDataRepository.addMetaData(MetaDataRepository.java:921)
    at org.apache.openjpa.meta.MetaDataRepository.addMetaData(MetaDataRepository.java:906)
    at org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.getMetaData(AnnotationPersistenceMetaDataParser.java:756)
    at org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.parseClassAnnotations(AnnotationPersistenceMetaDataParser.java:544)
    at org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.parse(AnnotationPersistenceMetaDataParser.java:414)
    at org.apache.openjpa.persistence.PersistenceMetaDataFactory.load(PersistenceMetaDataFactory.java:259)
    at org.apache.openjpa.meta.MetaDataRepository.getMetaDataInternal(MetaDataRepository.java:587)
    at org.apache.openjpa.meta.MetaDataRepository.getMetaDataInternal(MetaDataRepository.java:397)
    at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:389)
    at org.apache.openjpa.meta.ValueMetaDataImpl.resolveDeclaredType(ValueMetaDataImpl.java:487)
    at org.apache.openjpa.meta.ValueMetaDataImpl.resolve(ValueMetaDataImpl.java:466)
    at org.apache.openjpa.meta.FieldMetaData.resolve(FieldMetaData.java:1883)
    at org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java:1874)
    at org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1808)
    at org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:829)
    at org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository.java:726)
    at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:650)
    at org.apache.openjpa.meta.MetaDataRepository.getMetaDataInternal(MetaDataRepository.java:418)
    at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:389)
    at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:286)
    at org.apache.openjpa.enhance.PCEnhancer.<init>(PCEnhancer.java:257)
    at org.apache.openjpa.enhance.PCClassFileTransformer.transform0(PCClassFileTransformer.java:151)
    ... 47 more
Caused by: java.lang.ClassNotFoundException: Cycle detected while trying to load class: cbn.bse.jpa.persistence.entities.EntityAccount
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1980)[org.apache.felix.framework-5.6.4.jar:]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)[:1.8.0_151]
    ... 78 more

pom.xml,我用的是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <artifactId>bse.persistence</artifactId>
    <groupId>bse.persistence</groupId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>

    <groupId>bse.jpa</groupId>
    <artifactId>bse.jpa</artifactId>
    <packaging>bundle</packaging>

    <name>bse.jpa Bundle</name>
    <description>bse.jpa OSGi bundle project.</description>

    <properties>
        <maven-bundle-plugin.version>3.3.0</maven-bundle-plugin.version>
        <osgi.version>5.0.0</osgi.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>${osgi.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.cmpn</artifactId>
            <version>${osgi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.enterprise</artifactId>
            <version>${osgi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa</artifactId>
            <version>2.4.1</version>
            <type>bundle</type>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-jpa_2.0_spec</artifactId>
            <version>1.1</version>
            <type>bundle</type>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                  <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>           
            </resource>         
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <version>${maven-bundle-plugin.version}</version>
                    <extensions>true</extensions>
                    <configuration>
                        <archive>
                            <manifestEntries>
                                <Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
                                <Bundle-Blueprint>OSGI-INF/blueprint/*.xml</Bundle-Blueprint>
                            </manifestEntries>
                        </archive>                  
                        <manifestLocation>src/main/resources/META-INF</manifestLocation>
                        <rebuildBundle>true</rebuildBundle>
                        <instructions>
                            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                            <Bundle-Version>${project.version}</Bundle-Version>
                            <Bundle-Activator>bse.jpa.Activator</Bundle-Activator>
                            <Import-Package>com.mysql.jdbc.jdbc2.optional,
                                javax.sql, 
                                javax.persistence;version=2.0, 
                                javax.persistence.metamodel,
                                javax.transaction,
                                javax.transaction.xa,                       
                                org.osgi.framework</Import-Package>
                            <Export-Package>cbn.bse.jpa.persistence.entities,
                                bse.jpa
                            </Export-Package>                           
                            <Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
                            <Bundle-ManifestVersion>2</Bundle-ManifestVersion>
                            <Include-Resource>src/main/resources</Include-Resource>
                            <Bundle-Blueprint>OSGI-INF/blueprint/*.xml</Bundle-Blueprint>

                        </instructions>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <maxmem>256M</maxmem>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.openjpa</groupId>
                    <artifactId>openjpa-maven-plugin</artifactId>
                    <version>2.4.1</version>

                    <configuration>
                      <includes>cbn/bse/jpa/persistence/entities/**/*.class</includes>
                    </configuration>                

                    <executions>
                      <execution>
                        <id>enhancer</id>
                        <phase>process-classes</phase>
                        <goals>
                          <goal>enhance</goal>
                        </goals>
                      </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.apache.openjpa</groupId>
                            <artifactId>openjpa</artifactId>
                            <version>2.4.1</version>
                        </dependency>
                    </dependencies>             
                </plugin>

                <plugin>
                   <artifactId>maven-assembly-plugin</artifactId>
                   <executions>
                     <execution>
                       <goals>
                         <goal>assembly</goal>
                       </goals>
                     </execution>
                   </executions>
                </plugin>               

            </plugins>
        </pluginManagement>
    </build>
</project>

蓝图文件:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

    <bean class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" id="sssDataSource" >
        <property name="serverName" value="localhost"></property>
        <property name="databaseName" value="rrrr"></property>
        <property name="port" value="3306"></property>
        <property name="user" value="yyyy"></property>
        <property name="password" value="xxxxx"></property>
    </bean>

    <!-- javax.sql.DataSource -->
    <service id="bseDSService" interface="javax.sql.DataSource" ref="sssDataSource">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="jdbc/rrrr" />
        </service-properties>
    </service>

</blueprint>

提前致谢!!

我喜欢这个问题的解决方案,这不是修复,而是替代方案。我决定使用 EclipseLink 2.6.4(使用 persistence 2.1),并且在 pom.xml 和 persistence.xml 处做一些小改动,我就能够启动我的包。 考虑到我仍在使用 Karaf 4.1 (Apache Felix) 和 Aries(JPA 和 Blueprint),我相信问题可能与我不再为这个包使用的库有关:OpenJPA(最有可能)或 Geronimo JPA( JSR-317)。

很抱歉现在没有更多时间使用 OpenJPA 来解决这个问题,但我要赶快处理这件事。我希望这个解决方案可以帮助遇到同样问题的其他人。

这是我当前的 pom.xml(我添加了 Jackson 的库依赖项,因为我在我的服务中执行了一些 JSON 转换,您可能不需要):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
    <artifactId>xxx.persistence</artifactId>
    <groupId>bse.persistence</groupId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>

    <groupId>bse.jpa</groupId>
    <artifactId>zzz.yyy</artifactId>
    <packaging>bundle</packaging>

    <name>bse.jpa Bundle</name>
    <description>zzz.yyy OSGi bundle project.</description>

    <properties>
        <maven-bundle-plugin.version>3.3.0</maven-bundle-plugin.version>
        <osgi.version>6.0.0</osgi.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>${osgi.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.cmpn</artifactId>
            <version>${osgi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi.enterprise</artifactId>
            <version>${osgi.version}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>


        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.7</version>
            <type>bundle</type>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.7</version>
            <type>bundle</type>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.antlr</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.asm</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.jpql</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.core</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>
                org.eclipse.persistence.jpa.modelgen.processor
            </artifactId>
            <version>2.6.4</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                  <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>           
            </resource>         
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <version>${maven-bundle-plugin.version}</version>
                    <extensions>true</extensions>
                    <configuration>
                        <archive>
                            <manifestEntries>
                                <Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
                                <Bundle-Blueprint>OSGI-INF/blueprint/*.xml</Bundle-Blueprint>
                            </manifestEntries>
                        </archive>                  
                        <manifestLocation>src/main/resources/META-INF</manifestLocation>
                        <rebuildBundle>true</rebuildBundle>
                        <instructions>
                            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                            <Bundle-Version>${project.version}</Bundle-Version>
                            <Bundle-Activator>bse.jpa.Activator</Bundle-Activator>
                            <Import-Package>com.mysql.jdbc.jdbc2.optional,
                                org.apache.felix.service.command,
                                org.apache.karaf.shell.commands,
                                org.apache.karaf.shell.console,
                                org.eclipse.persistence.internal.weaving,
                                org.eclipse.persistence.internal.descriptors,
                                org.eclipse.persistence.queries,
                                org.eclipse.persistence.descriptors.changetracking,
                                org.eclipse.persistence.internal.identitymaps,
                                org.eclipse.persistence.sessions,
                                org.eclipse.persistence.internal.jpa.rs.metadata.model,
                                org.eclipse.persistence.indirection,                            
                                javax.sql, 
                                javax.persistence;version=2.0, 
                                javax.persistence.metamodel,
                                javax.transaction,
                                javax.transaction.xa,                       
                                org.osgi.framework,
                                org.osgi.util.tracker,
                                *</Import-Package>
                            <Export-Package>cbn.bse.jpa.persistence.entities,
                                bse.jpa
                            </Export-Package>                           
                            <Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
                            <Bundle-ManifestVersion>2</Bundle-ManifestVersion>
                            <Include-Resource>src/main/resources</Include-Resource>
                            <Bundle-Blueprint>OSGI-INF/blueprint/*.xml</Bundle-Blueprint>

                        </instructions>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <maxmem>256M</maxmem>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.bsc.maven</groupId>
                    <artifactId>maven-processor-plugin</artifactId>
                    <version>2.2.4</version>
                    <executions>
                        <execution>
                            <id>eclipselink-jpa-metamodel</id>
                            <goals>
                                <goal>process</goal>
                            </goals>
                            <phase>generate-sources</phase>
                            <configuration>
                                <processors>
                                    <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
                                </processors>
                                <compilerArguments>-Aeclipselink.persistencexml=${basedir}/src/test/resources/META-INF/persistence.xml</compilerArguments>
                                <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.eclipse.persistence</groupId>
                            <artifactId>org.eclipse.persistence.jpa.modelgen.processor
                             </artifactId>
                            <version>2.6.4</version>
                        </dependency>
                    </dependencies>
                </plugin>

                <plugin>
                   <artifactId>maven-assembly-plugin</artifactId>
                   <executions>
                     <execution>
                       <goals>
                         <goal>assembly</goal>
                       </goals>
                     </execution>
                   </executions>
                </plugin>               

            </plugins>
        </pluginManagement>
    </build>
</project>

持久性(您可以轻松地将其更改为使用带有 pax-jdbc-mysql 的 JTA):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="rrrr" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <non-jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/rrrr)</non-jta-data-source>
        <class>Account</class>
        <class>Warehouse</class>
        <class>Widget</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.logging.level"                      value="WARNING"/>
            <property name="eclipselink.logging.level.sql"                  value="WARNING"/>
            <property name="eclipselink.logging.parameters"                 value="true"/>
            <property name="eclipselink.logging.timestamp"                  value="true"/>
            <property name="eclipselink.logging.session"                    value="true"/>
            <property name="eclipselink.logging.thread"                     value="true"/>
            <property name="eclipselink.logging.exceptions"                 value="true"/>
            <property name="eclipselink.session.include.descriptor.queries" value="true"/>
            <property name="eclipselink.weaving"                            value="true"/>

        </properties>
    </persistence-unit>
</persistence>

有用的参考资料:

Luca Burgazzoli's Karaf examples (jpa)

Travis Steel's EclipseLink JPA Metamodel Generation in Eclipse with Maven