How to use hibernate in CQ6?

Avatar

Avatar

pradeepdubey82

Avatar

pradeepdubey82

pradeepdubey82

28-04-2016

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

View Entire Topic

Avatar

Avatar

fabianh12876403

Avatar

fabianh12876403

fabianh12876403

25-09-2018

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