【问题标题】:AEM CQ with JPA (Hibernate)AEM CQ 与 JPA(休眠)
【发布时间】:2019-02-23 09:19:53
【问题描述】:

我正在使用带有 Service Pack 1 的 Adob​​e Experience Manager (AEM) 6.4 和 Forms Package。 我有很多扩展的属性/属性,所以我做了一个数据库图。我不想将所有其他内容保存在 crx 中,我想将其保存在 Oracle 数据库中。

数据库图很复杂,所以我想至少使用 JPA (Hibernate)。如果 Spring 有助于使其更易于使用,那对我来说很好。

我读到很多 OSGI 正在使用蓝图而不是 Spring,但您可以将它结合起来。

我真的在寻找一个很好的例子,如何让 JPA 和 Oracle db 工作。

Adobe 根本没有帮助,他们无法展示如何使用 AEM 和 JPA/Hibernate/Spring/blueprint 的示例。

任何人都可以帮助我使事情正常进行吗?使用 AEM 和 JPA?

我认为我需要的是:

  1. persistence.xml(RESOURCE_LOCAL 还是 JTA?)
  2. 带有@Entity 注释和其他 JPA 注释的实体类
  3. 具有事务控制的服务类和管理器类提交以使用实体类并从查询中获取结果,也许我可以将它们全部放在服务类中
  4. hibernate-osgi(依赖)
  5. ojdbc7 用于连接(依赖)
  6. org.apache.aries.jpa.api(依赖)

但是我必须如何让事情发生呢?没有任何效果。我什至不知道这是否正确。

我应该使用 blueprint 还是 Spring 或两者兼而有之?

我从 apache 中找到了这个 Aries 的东西。 http://aries.apache.org 并穿着不同的样品,我真的不明白它们是如何工作的。 https://github.com/apache/aries-jpa/tree/master/examples

还有一些 OSGI 示例,对于我的情况,它们看起来非常不完整。 https://enroute.osgi.org/tutorial/032-tutorial_microservice-jpa.html

那么有人对 AEM 和 JPA 有一定的经验吗?

【问题讨论】:

    标签: java hibernate jpa osgi aem


    【解决方案1】:

    让我描述一下它是如何在我们的项目中实现的。我们正在使用带有 SP2 的 AEM 6.3。 我们在根 pom.xml 中有下一个依赖项:

        <!-- JPA -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>5.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>com.ibm.db2.jcc</groupId>
            <artifactId>db2jcc4</artifactId>
            <version>11.1</version>
            <scope>system</scope>
            <systemPath>${project.root.path}/lib/db2jcc4.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.3.2.Final</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml</groupId>
            <artifactId>classmate</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>
        <!-- local development database -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.194</version>
        </dependency>
        <!-- /JPA-->
    

    然后我们有了提供 JPA 依赖项的包和允许获取休眠会话的 OSGI 服务。包 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>
        <groupId>com.myproject</groupId>
        <artifactId>myproject-bundles</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
      <artifactId>com.myproject.db</artifactId>
      <packaging>bundle</packaging>
      <name>myproject - DB bundle</name>
      <description>OSGI bundle to work with a database</description>
    
      <properties>
        <bundle.export>
          com.myproject.db.*,
          javax.persistence,
          org.hibernate,
          org.hibernate.cfg,
          org.hibernate.proxy,
          org.hibernate.boot.registry,
          org.hibernate.annotations,
          org.hibernate.service,
          org.hibernate.criterion,
          org.hibernate.transform
        </bundle.export>
        <bundle.import>*;resolution:=optional</bundle.import>
        <!-- Import JDBC driver dynamically -->
        <bundle.dynamic.import>com.ibm.*,javassist.util.*</bundle.dynamic.import>
        <bundle.embed>
          hibernate-jpa-2.1-api,hibernate-core,hibernate-entitymanager,hibernate-commons-annotations,jboss-logging,antlr
        </bundle.embed>
        <project.root.path>${project.basedir}/../..</project.root.path>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.hibernate.javax.persistence</groupId>
          <artifactId>hibernate-jpa-2.1-api</artifactId>
        </dependency>
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-entitymanager</artifactId>
        </dependency>
        <dependency>
          <groupId>org.hibernate.common</groupId>
          <artifactId>hibernate-commons-annotations</artifactId>
        </dependency>
        <dependency>
          <groupId>org.jboss.logging</groupId>
          <artifactId>jboss-logging</artifactId>
        </dependency>
        <dependency>
          <groupId>com.ibm.db2.jcc</groupId>
          <artifactId>db2jcc4</artifactId>
        </dependency>
        <dependency>
          <groupId>antlr</groupId>
          <artifactId>antlr</artifactId>
        </dependency>
        <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
        </dependency>
          <dependency>
              <groupId>com.fasterxml.jackson.core</groupId>
              <artifactId>jackson-annotations</artifactId>
          </dependency>
          <dependency>
              <groupId>com.fasterxml.jackson.core</groupId>
              <artifactId>jackson-databind</artifactId>
          </dependency>
          <dependency>
              <groupId>com.myproject</groupId>
              <artifactId>com.myproject.common</artifactId>
              <version>1.0-SNAPSHOT</version>
              <scope>compile</scope>
          </dependency>
      </dependencies>
    </project>
    

    在捆绑包中我们有下一个服务:

    package com.myproject.db;
    
    import org.hibernate.Session;
    
    public interface JPASessionFactory {
        Session openSession();
    
        void closeSession(Session session);
    }
    

    实现:

    package com.myproject.db.impl;
    
    import com.myproject.db.JPASessionFactory;
    import org.apache.felix.scr.annotations.*;
    import org.apache.sling.commons.osgi.PropertiesUtil;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.cfg.Environment;
    
    import javax.sql.DataSource;
    import java.util.Map;
    
    @Service
    @Component(metatype = true, policy = ConfigurationPolicy.REQUIRE)
    @Properties({
            @Property(label = "Hibernate SQL dialect", name = Environment.DIALECT),
            @Property(label = "Show SQL", name = Environment.SHOW_SQL, boolValue = false),
            @Property(label = "Bulk ID Strategy", name = Environment.HQL_BULK_ID_STRATEGY)
    })
    public class JPASessionFactoryImpl implements JPASessionFactory {
    
        @Reference(target = "(datasource.name=myproject)")
        private DataSource dataSource;
    
        private SessionFactory sessionFactory;
    
        @Activate
        protected void activate(Map<String, Object> properties) {
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                    .configure()
                    .applySetting(Environment.DIALECT, PropertiesUtil.toString(properties.get(Environment.DIALECT), ""))
                    .applySetting(Environment.SHOW_SQL, PropertiesUtil.toBoolean(properties.get(Environment.SHOW_SQL), false))
                    .applySetting(Environment.DATASOURCE, dataSource);
    
            String bulkIdStrategy = PropertiesUtil.toString(properties.get(Environment.HQL_BULK_ID_STRATEGY), "");
            if (!bulkIdStrategy.isEmpty()) {
                builder.applySetting(Environment.HQL_BULK_ID_STRATEGY, bulkIdStrategy);
            }
            sessionFactory = new Configuration().buildSessionFactory(builder.build());
        }
    
        @Deactivate
        protected void deactivate() {
            if (sessionFactory != null) {
                sessionFactory.close();
            }
        }
    
        @Override
        public Session openSession() {
            return sessionFactory.openSession();
        }
    
        @Override
        public void closeSession(Session session) {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }
    

    服务的 osgi 配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
              jcr:primaryType="sling:OsgiConfig"
              hibernate.dialect="org.hibernate.dialect.H2Dialect"
              hibernate.show_sql="{Boolean}true"/>
    

    DataSourceFactory 应用程序的配置/myproject-forms/configuration/config.local/org.apache.sling.datasource.DataSourceFactory-localh2.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
              jcr:primaryType="sling:OsgiConfig"
              datasource.name="myproject"
              driverClassName="org.h2.Driver"
              url="jdbc:h2:./myprojectlocal;AUTO_SERVER=TRUE"
              username="sa"
              password=""
              testOnBorrow="{Boolean}true"
              testOnReturn="{Boolean}true"
              testWhileIdle="{Boolean}true"
              validationQuery="SELECT 1"/>
    

    我们还有 Hibernate 配置文件 hibernate.cfg.xml 在 bundle 的“resources”文件夹中

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
      <session-factory>
        <property name="hbm2ddl.auto">update</property>
        <property name="current_session_context_class">thread</property>
        <mapping class="com.myproject.db.Entity1"/>
        <mapping class="com.myproject.db.Entity2"/>
      </session-factory>
    </hibernate-configuration>
    

    【讨论】:

    • 感谢您的解决方案。你用什么hibernate.cfg.xml?这里看不到。 hibernate 是如何知道应该生成哪些类的?
    • 我们正在使用该文件,请检查我回答中的最后一句话。它用于让 hibernate 知道在哪里搜索 JPA 实体。添加文件示例
    • 我们没有看清楚。据我了解,它是由 Hibernate 在内部完成的。
    【解决方案2】:

    我找到了解决方案并在网站上发了一个帖子。

    https://forums.adobe.com/message/10640295#10640295

    我觉得我是世界上第一个将 AEM 与 JPA/Hibernate 相结合的人。现在我可以检查使用 spring 是否有好处,使用 Transactions 进行操作。

    那两个投票否决问题的人,我确定你来自 adobe --> 谢谢,现在我觉得你自己的 cms 比你更专业,因为在我发帖之前,这个问题没有解决方案。

    更新:Maven 依赖项。如果您对依赖项有问题,则必须嵌入它们并添加属性“Embed-Transitive to true”

     <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <inherited>true</inherited>
                <configuration>
                    <instructions>
                        <Embed-Dependency>
                            *;scope=compile|runtime
                        </Embed-Dependency>
                        <Embed-Transitive>true</Embed-Transitive>
                        <Export-Package>
                            <!--TODO export packages -->
    
                        </Export-Package>
                        <Private-Package>todo</Private-Package>
                        <Import-Package>
                            <!-- Attention this is really sensitive!!. -->
                            *;resolution:="optional"
                        </Import-Package>
                        <Bundle-Activator>path.to.Activator</Bundle-Activator>
                    </instructions>
                </configuration>
            </plugin>
    

    【讨论】:

    • 你不是第一个 :) 。最近不得不将 AEM 与 JPA 集成。我们正在使用 DB2。大多数问题都与未解决的 Hibernate 依赖关系有关。
    • 您能分享一下您的经验吗?
    • 在下面查看我的答案
    【解决方案3】:

    有一些博客文章描述了如何在 AEM/CQ 中实现 JPA 持久性。在using-jpa-to-write-database-applications-in-an-osgi-container-e-g-felix-or-adobe-aem-cq5 中,他们描述了如何使用 PersistenceUnit 设置 JNDI 数据源以及如何@Reference EntityManagerFactory。

    【讨论】:

    • 我已经在这个链接上,这根本没有太大帮助。乍一看,它看起来非常简单和紧凑,但如果您尝试使用它,您会发现它不完整,而且有点旧,可能是 aem 6.1 和从 aem 6.1 到 6.4 的巨大变化。
    • 您有更多信息吗?也许在这里会有帮助的错误跟踪。或者您的环境中似乎缺少哪个服务?
    【解决方案4】:

    我们使用以下两种方法将 Hibernate 5.4.5 和 JPA 2.2 集成到 Adob​​e Experience Manager 6.4.4 中。详情请查看https://soft-werke.com/en/archives/news-en/how-to-install-hibernate-5-4-5-jpa-2-2-in-adobe-experience-manager-6-4-4/

    1. 经典方法 - 使用 content-package-maven-plugin 在 AEM 包管理器中安装包含所有嵌入式 Hibernate 依赖项的 zip 包。下面是一个 Maven 配置文件示例,稍后可以将其排除,以避免在每个 AEM 部署中安装 Hibernate。
    <profiles>
        <profile>
            <id>installHibernate</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.day.jcr.vault</groupId>
                        <artifactId>content-package-maven-plugin</artifactId>
                        <extensions>true</extensions>
                        <configuration>
                            <embeddedTarget>/apps/hibernate/install</embeddedTarget>
                            <embeddeds>
                                <embedded>
                                    <groupId>org.apache.servicemix.bundles</groupId>
                                    <artifactId>org.apache.servicemix.bundles.antlr</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>com.fasterxml</groupId>
                                    <artifactId>classmate</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>com.sun.activation</groupId>
                                    <artifactId>javax.activation</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>javax.activation</groupId>
                                    <artifactId>javax.activation-api</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>javax.persistence</groupId>
                                    <artifactId>javax.persistence-api</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>javax.xml.bind</groupId>
                                    <artifactId>jaxb-api</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>net.bytebuddy</groupId>
                                    <artifactId>byte-buddy</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.apache.servicemix.bundles</groupId>
                                    <artifactId>org.apache.servicemix.bundles.dom4j</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>com.sun.istack</groupId>
                                    <artifactId>istack-commons-runtime</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>com.sun.xml.fastinfoset</groupId>
                                    <artifactId>FastInfoset</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.apache.servicemix.bundles</groupId>
                                    <artifactId>org.apache.servicemix.bundles.jaxb-runtime</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.javassist</groupId>
                                    <artifactId>javassist</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.jboss.logging</groupId>
                                    <artifactId>jboss-logging</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.hibernate.common</groupId>
                                    <artifactId>hibernate-commons-annotations</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.jboss</groupId>
                                    <artifactId>jandex</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>javax.el</groupId>
                                    <artifactId>javax.el-api</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.apache.servicemix.bundles</groupId>
                                    <artifactId>org.apache.servicemix.bundles.javax-inject</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>javax.interceptor</groupId>
                                    <artifactId>javax.interceptor-api</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>javax.enterprise</groupId>
                                    <artifactId>cdi-api</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.jboss.spec.javax.interceptor</groupId>
                                    <artifactId>jboss-interceptors-api_1.2_spec</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.jboss.spec.javax.transaction</groupId>
                                    <artifactId>jboss-transaction-api_1.2_spec</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>mysql</groupId>
                                    <artifactId>mysql-connector-java</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.hibernate</groupId>
                                    <artifactId>hibernate-core</artifactId>
                                </embedded>
                                <embedded>
                                    <groupId>org.hibernate</groupId>
                                    <artifactId>hibernate-osgi</artifactId>
                                </embedded>
                            </embeddeds>
                        </configuration>
                        <executions>
                            <execution>
                                <id>install-package-hibernate</id>
                                <goals>
                                    <goal>install</goal>
                                </goals>
                                <configuration>
                                    <targetURL>http://${aem.host}:${aem.port}/crx/packmgr/service.jsp</targetURL>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    
    <!-- ====================================================================== -->
    <!-- D E P E N D E N C I E S                                                -->
    <!-- ====================================================================== -->
    <dependencies>
        <dependency>
            <groupId>org.apache.servicemix.bundles</groupId>
            <artifactId>org.apache.servicemix.bundles.antlr</artifactId>
            <version>2.7.7_5</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml</groupId>
            <artifactId>classmate</artifactId>
            <version>1.3.4</version>
        </dependency>
        <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>javax.activation</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>javax.activation-api</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy</artifactId>
            <version>1.9.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.servicemix.bundles</groupId>
            <artifactId>org.apache.servicemix.bundles.dom4j</artifactId>
            <version>2.1.1_1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.istack</groupId>
            <artifactId>istack-commons-runtime</artifactId>
            <version>3.0.7</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.fastinfoset</groupId>
            <artifactId>FastInfoset</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.servicemix.bundles</groupId>
            <artifactId>org.apache.servicemix.bundles.jaxb-runtime</artifactId>
            <version>2.3.1_1</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.24.0-GA</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.3.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss</groupId>
            <artifactId>jandex</artifactId>
            <version>2.0.5.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>2.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.servicemix.bundles</groupId>
            <artifactId>org.apache.servicemix.bundles.javax-inject</artifactId>
            <version>1_2</version>
        </dependency>
        <dependency>
            <groupId>javax.interceptor</groupId>
            <artifactId>javax.interceptor-api</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.interceptor</groupId>
            <artifactId>jboss-interceptors-api_1.2_spec</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.2_spec</artifactId>
            <version>1.1.1.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-osgi</artifactId>
            <version>5.4.5.Final</version>
        </dependency>
        <dependency>
            <groupId>com.adobe.aem</groupId>
            <artifactId>uber-jar</artifactId>
            <classifier>apis</classifier>
        </dependency>
        <dependency>
            <groupId>javax.jcr</groupId>
            <artifactId>jcr</artifactId>
        </dependency>
    </dependencies>
    
    1. 另一种方法 - 在 AEM Felix Console 中以预定义的顺序将所有 Hibernate 捆绑包安装到由 Java 调用的 sling-maven-plugin Maven Invoker API。下面是一个 Maven 配置文件示例。
    <profiles>
        <profile>
            <id>installHibernate</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
    
            <dependencies>
                <dependency>
                    <groupId>rg</groupId>
                    <artifactId>com.softwerke.jpa.hibernate.bundles</artifactId>
                    <version>1.0-SNAPSHOT</version>
                    <scope>import</scope>
                    <type>pom</type>
                </dependency>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>
                                        ${basedir}/src/main/content/jcr_root/apps/hibernate/install/
                                    </outputDirectory>
                                    <includeArtifactIds>
                                        org.apache.servicemix.bundles.antlr,
                                        classmate,
                                        javax.activation,
                                        javax.activation-api,
                                        javax.persistence-api,
                                        jaxb-api,
                                        byte-buddy,
                                        org.apache.servicemix.bundles.dom4j,
                                        istack-commons-runtime,
                                        FastInfoset,
                                        org.apache.servicemix.bundles.jaxb-runtime,
                                        javassist,
                                        jboss-logging,
                                        hibernate-commons-annotations,
                                        jandex,
                                        javax.el-api,
                                        org.apache.servicemix.bundles.javax-inject,
                                        javax.interceptor-api,
                                        cdi-api,
                                        jboss-interceptors-api_1.2_spec,
                                        jboss-transaction-api_1.2_spec,
                                        mysql-connector-java,
                                        hibernate-core,
                                        hibernate-osgi
                                    </includeArtifactIds>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.6.0</version>
                        <executions>
                            <execution>
                                <phase>install</phase>
                                <goals>
                                    <goal>java</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <mainClass>com.softwerke.build.utils.HibernateInstaller</mainClass>
                            <classpathScope>compile</classpathScope>
                            <arguments>
                                <!-- Path to load Hibernate OSGi dependencies from -->
                                <argument>${basedir}/src/main/content/jcr_root/apps/hibernate/install</argument>
                                <!-- Path to XML file with a list of Hibernate bundles -->
                                <argument>${basedir}/src/main/content/META-INF/resources/hibernate-bundles.xml</argument>
                                <!-- User login to install bundle in AEM -->
                                <argument>${sling.user}</argument>
                                <!-- User password to install bundle in AEM -->
                                <argument>${sling.password}</argument>
                                <!-- Path to AEM Felix Console -->
                                <argument>http://${aem.host}:${aem.port}/system/console</argument>
                            </arguments>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    使用 Maven Invoker API 安装 Hibernate 包的 Java 代码:

    public class HibernateInstaller {
        public static void main(String[] args) throws Exception {
            String bundleFolder,
                   xmlFilePath,
                   slingUser,
                   slingPassword,
                   url;
            if (args != null && args.length == 5) {
                bundleFolder = args[0];
                xmlFilePath = args[1];
                slingUser = args[2];
                slingPassword = args[3];
                url = args[4];
            } else {
                throw new Exception("Can't execute Hibernate install!\nPlease specify five arguments.");
            }
    
            StringBuilder mvnCmd = new StringBuilder();
            mvnCmd.append("org.apache.sling:sling-maven-plugin:2.4.2:install-file");
            mvnCmd.append(" -Dsling.user=" + slingUser);
            mvnCmd.append(" -Dsling.password=" + slingPassword);
            mvnCmd.append(" -Dsling.url=" + url);
            mvnCmd.append(" -Dsling.deploy.method=WebConsole");
            mvnCmd.append(" -Dsling.file=");
            mvnCmd.append(bundleFolder);
            mvnCmd.append("/");
    
            // Read XML file with a list of the Hibernate OSGi dependencies (bundles)
            File file = new File(xmlFilePath);
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(file);
            NodeList bundlesList = document.getElementsByTagName("bundle");
    
            final InvocationRequest invocationRequest = new DefaultInvocationRequest();
            // invocationRequest.setDebug(true);
            final Invoker invoker = new DefaultInvoker();
    
            for (int i = 0; i < bundlesList.getLength(); i++) {
                String goal = mvnCmd.toString() + bundlesList.item(i).getTextContent();
                installBundle(invocationRequest, invoker, goal);
            }
        }
    
        private static void installBundle(InvocationRequest invocationRequest, Invoker invoker, String goal)
                throws MavenInvocationException, CommandLineException {
            invocationRequest.setGoals(Collections.singletonList(goal));
            final InvocationResult invocationResult = invoker.execute(invocationRequest);
            if (invocationResult.getExitCode() != 0) {
                String msg = "Invocation Exception";
                if (invocationResult.getExecutionException() != null) {
                    msg = invocationResult.getExecutionException().getMessage();
                }
                throw new CommandLineException(msg);
            }
        }
    }
    

    附言

    1. 如果您不需要对 AEM 实例具有相同依赖项的不同版本,最好避免使用 OSGi &lt;Embed-Transitive&gt;true&lt;/Embed-Transitive&gt;,因为它可能会引入安全/性能/OSGi 问题 - 有关详细信息,请参阅 Embedding_Transitive_Dependencies_Into_a_Mega-Bundle_With_Maven-Bundle-Plugin
    2. 如果您一旦决定切换到另一个 ORM 框架,最好选择 JPA EntityManagerFactory 方法而不是特定于 Hibernate 的 SessionFactory - 检查 https://stackoverflow.com/a/5640796/12547140

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 2016-01-19
      • 2013-06-19
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多