How to use hibernate in CQ6? | Community
Skip to main content
Level 5
Apr 28, 2016 12:03 PM UTC
Solved

How to use hibernate in CQ6?

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Ratna_Kumar
Ratna_KumarAccepted solution
Level 10
Apr 28, 2016 3:45 PM UTC

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.

17 replies

New Participant
Dec 27, 2019 1:34 PM UTC

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-experience-manager-6-4-4/ 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.

Translate
New Participant
Dec 27, 2019 1:20 PM UTC

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-experience-manager-6-4-4/ 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.

Translate
smacdonald2008
Level 10
Sep 25, 2018 8:04 PM UTC

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.

Translate
joerghoh
Employee
Sep 25, 2018 7:58 PM UTC

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

Translate
fabianh12876403
Level 2
Sep 25, 2018 3:34 PM UTC

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)

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

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

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

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

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

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

Here The Entity Class Author

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

Translate
fabianh12876403
Level 2
Sep 24, 2018 2:24 PM UTC

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?

Translate
Ratna_Kumar
Level 10
Sep 24, 2018 1:39 PM UTC

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.

Translate
fabianh12876403
Level 2
Sep 17, 2018 8:21 AM UTC

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.

Translate
Level 2
Aug 1, 2018 2:08 PM UTC

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

Translate
smacdonald2008
Level 10
Aug 1, 2018 1:59 PM UTC

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.

Translate