Expand my Community achievements bar.

SOLVED

How to use hibernate in CQ6?

Avatar

Level 7

Hi,

I am using hibernate 4.0.1 for CRUD operations upon page replication. For this i have written eventhandler, in handleEvent method i am writing logic to persist page property in database using hibernate.

I am getting class not found error in hibernate. added all dependencies in POM file, showing all jars in classpath too, bundle is deployed successfully and it is in active state. 

hibernate.cfg.xml i have put in src/main/resources folder and added this folder in classpath.

java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

Could anyone please help me to resolve the issue, below piece of code is causing the problem 

Configuration configuration = new Configuration().configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
                    configuration.getProperties()). buildServiceRegistry();
            SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            Session session = sessionFactory.openSession();

 

Thanks,

Pradeep

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Pradeep,

As Scott stated, the best practice to connect AEM with database is injecting a DataSourcePool.

But Yes, AEM may support hibernate too(http://mvnrepository.com/artifact/org.hibernate/hibernate-osgi), but there is no documentation/community articles related to this topic.

Thanks,
Ratna Kumar.

View solution in original post

17 Replies

Avatar

Level 10

When working with AEM and databases - the best practice is injecting a DataSourcePool - not using Hibernate. 

WHen using a DataSourcePool - you specify the DB properties in the Felix Console config view. 

See this article for more information: 

https://helpx.adobe.com/experience-manager/using/datasourcepool.html

Avatar

Level 7

The problem with data source pool is that we have to write sql queries for all crud operations. In my case we have very complex table structure, so writing sql is very tedious task. If hibernate would have supported in AEM my job will become easy and manageable. I am assuming AEM supports hibernate. Please share some links or examples where hibernate has been used along with AEM.

Avatar

Level 10

It may support Hibernate - however - I am not seeing any community articles or documentation that states it does.

Also - I wanted to know why you were not using DataSoucePool - which is best practice.  You answered my question.

I will check within Adobe to see if anyone has examples (assuming it does). 

I will post back my findings. 

Avatar

Correct answer by
Level 10

Hi Pradeep,

As Scott stated, the best practice to connect AEM with database is injecting a DataSourcePool.

But Yes, AEM may support hibernate too(http://mvnrepository.com/artifact/org.hibernate/hibernate-osgi), but there is no documentation/community articles related to this topic.

Thanks,
Ratna Kumar.

Avatar

Level 10

Some of the AEM people state: 

they can try out the bundle and see what happens. :)

In theory it should work.

Avatar

Level 2

Hi,

Any luck with Hibernate inside the AEM?

Thanks,

Andrii

Avatar

Level 10

Database interaction  still has not changed since this thread was created. Using DataSourcePool is still best practice. We have a newer article here:

Adobe Experience Manager Help | Submitting Adaptive Form Data to MySQL using a DataSourcePool

Now there appears to be 3rd party OSGi bundles for this here - Maven Repository: org.hibernate » hibernate-osgi

I have never tried this nor seen any community article that show its usage.

Avatar

Level 2

Hi Scott,

Thanks for the response, I was also not able to find any articles or info how to configure it or at list resolve dependencies for this bundle.

Maybe Pradeep or someone from the community has some experience.

Best regards,

Andrii

Avatar

Level 3

this is like bullshit. the Datasourcepool has nothing common with hibernate, it just connect your application with the database.

Why is there no support for config hibernate with AEM CQ?

I think many companies would like to use hibernate with CQ.

Avatar

Level 10

Hi,

Hibernate is also a way to connect to database, but when it comes to AEM, the out of the box(OOTB) way is Data Sourcepool.

Thanks,

Ratna Kumar.

Avatar

Level 3

Ok so to build a complex database diagram I have to work like in 1950?

Or is there any other possibility for complex datastructures? With a lot of dependencies between the tables, and a lot of attributes, to generate them automatically?

Avatar

Level 3

I've found the solution!!!!!!!!!!!!!!!

In my Situation I use AEM 6.4 with Servicepack 1 and AEM Forms, in the backend I have an Oracle Database.

So I need the dependency for

  1. ojdbc (for oracle 12, 12.1.0.2)
  2. hibernate-osgi (5.3.6.Final)
  3. hibernate-entitymanager (5.3.6.Final)

1579295_pastedImage_2.png

You should Embedd thos dependencies in your Bundle project

Next I had to configure a hibernate.cfg.xml and save it in the resources Folder in the bundle project. All of the Entity classes need to be listed in the cfg.xml

Here the sample

1579296_pastedImage_3.png

So far so Good. Now we need some magic.

I made an Activator and an HibernateUtil Klass. The Activator is calling the HibernateUtil Class on Startup to setup the connection to your database. I also made a Transaction and commit an entry.

Attention: add the Activator in your bundle pom

1579297_pastedImage_4.png

Your Activator class hass to implement the BundleActivator class. In the start method you have to call your HibernateUtil class to get the SessionFactory

1579301_pastedImage_5.png

And in the HibernateUtil class you have to read the hibernate config from resource and work with your sessionFactory

1579305_pastedImage_10.png

After calling this Method, you see now the Generated Tables from the Entity classes.

1579303_pastedImage_7.png

1579304_pastedImage_8.png

This is how far I am now. But I think it looks not so bad.

Here The Entity Class Author

1579306_pastedImage_11.png

This should be marked as the right answer, and not the answer from the people who told you just to use Datasource -.-

Avatar

Employee Advisor

Hi @

looks interesting. I wonder how this works together with the way OSGI manages classloaders. I could imagine that there are cases where you end up in classloader hell...  Can you share some experience?

Jörg

Avatar

Level 10

DataSourcePool is the solution that the AEM Eng team has created to interact with relational databases. That is why its marked as correct.

The AEM Eng team have not created an OOTB OSGI solution for hibernate.

If you think this is a proper solution, you can write up a Word doc and we will submit to the AEM Eng team to get their thoughts. Once they approve this approach, we will gladly publish this solution.

Avatar

Level 1

We have integrated Hibernate 5.4.5 and JPA 2.2 into Adobe Experience Manager 6.4.4 using two approaches below. Please, check https://soft-werke.com/en/archives/news-en/how-to-install-hibernate-5-4-5-jpa-2-2-in-adobe-experienc... for more details.

  • Classic approach - install zip package with all embedded Hibernate dependencies in AEM Package Manager with content-package-maven-plugin. Below is an example of Maven profile which can be later excluded to avoid installing Hibernate in each AEM deployment.

 

<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>

<dependencies>
    <!-- Other dependencies ... -->
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</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>
    <!-- Other dependencies ... -->
</dependencies>​

 

  • Another approach - install all Hibernate bundles in AEM Felix Console one-by-one in predefined order with sling-maven-plugin called by Java Maven Invoker API. Below is an example of Maven profile.

 

<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>​

 

Java code for Hibernate bundle installation with Maven Invoker API:

 

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);
        }
    }
}​

 

P.S.

In case you once decide to switch to another ORM framework, it's better to prefer JPA EntityManagerFactory approach instead of Hibernate-specific SessionFactory - check https://stackoverflow.com/a/5640796/12547140.

Avatar

Level 1

We have integrated Hibernate 5.4.5 and JPA 2.2 into Adobe Experience Manager 6.4.4 using two approaches below. Please, check https://soft-werke.com/en/archives/news-en/how-to-install-hibernate-5-4-5-jpa-2-2-in-adobe-experienc... for more details.

  • Classic approach - install zip package with all embedded Hibernate dependencies in AEM Package Manager with content-package-maven-plugin. Below is an example of Maven profile which can be later excluded to avoid installing Hibernate in each AEM deployment.

 

<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>

<dependencies>
    <!-- Other dependencies... -->
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</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>
    <!-- Other dependencies... -->
</dependencies>​

 

  • Another approach - install all Hibernate bundles in AEM Felix Console one-by-one in predefined order with sling-maven-plugin called by Java Maven Invoker API. Below is an example of Maven profile.

 

<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>​

Java code for Hibernate bundle installation with Maven Invoker API:

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);
        }
    }
}​

 

P.S.

In case you once decide to switch to another ORM framework, it's better to prefer JPA EntityManagerFactory approach instead of Hibernate-specific SessionFactory - check https://stackoverflow.com/a/5640796/12547140.