6 Replies Latest reply on Oct 11, 2012 4:16 PM by rodrigobviana

    Custom Hibernate Dialect - Jboss 7.1

    rathm1

      Hi there,

      We are in the process of moving our application from Jboss 6 to Jboss7.1.

      We are using hibernate and DB2 and have created a Custom DB2 Dialect by extending org.hibernate.DB2Dialect.

      Our Custom DB2 Dialect is inside a jar that is inside our ear. 

      We define the hibernate.cfg.xml to use the custom dialect

       

      <property name="hibernate.dialect">myApp.custom.CustomDB2Dialect</property>

       

      The ear deploys successfully but when trying to launch the application and build the SessionFactory, it seems as though hibernate cannot access our Custom Dialect inside the ear.

      We have a dependency set up in our MANIFEST.MF on

      Dependencies: org.hibernate, org.hibernate.envers, org.javassist, javaee.api, org.apache.commons.lang

       

      My question is, how can I add a dependency to hibernate for our CustomDialect class?

       

      The stacktrace of the error thrown is:

      ...

      Caused by: org.hibernate.HibernateException: Dialect class not found: myApp.custom.CustomDB2Dialect

          at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:76) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:64) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:148) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2270) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2266) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1735) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at myApp.custom.Utility.<clinit>(Utility.java:35) [myApp.jar:]

          ... 50 more

      Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [myApp.custom.CustomDB2Dialect]

          at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:141) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:73) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          ... 60 more

      Caused by: java.lang.ClassNotFoundException: Could not load requested class : myApp.custom.CustomDB2Dialect

          at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.findClass(ClassLoaderServiceImpl.java:99) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          at java.lang.ClassLoader.loadClass(ClassLoader.java:306) [rt.jar:1.6.0_26]

          at java.lang.ClassLoader.loadClass(ClassLoader.java:247) [rt.jar:1.6.0_26]

          at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]

          ... 61 more

       

      Thanks a lot.

        • 1. Re: Custom Hibernate Dialect - Jboss 7.1
          jje

          The only solution that i've found to this feels like a hack:

           

          - package up your dialect class in a jar

          - copy the jar into the modules/org/hibernate/main dir

          - add it as a <resource-root> to the module.xml

           

          Posting this in the hope that someone will come along and explain how it should be done!

          • 2. Re: Custom Hibernate Dialect - Jboss 7.1
            rathm1

            Thanks for the reply. 

            I found that solution as well and didn't like it so looked for another and found that you can set overrides of the Types when the SessionFactory is created instead of overriding the Dialect.

            ie

            Configuration configuration = new Configuration().configure();

            configuration.registerTypeOverride(new MyCustomDateUserType(),

                                new String[] { "timestamp" });

            configuration.registerTypeOverride(new MyCustomBooleanUserType(),

                                new String[] { "boolean" });

            configuration.registerTypeOverride(new MyCustomStringUserType(),

                                new String[] { "customString" });

             

            I am still hoping to have a better solution one day but this has been working for me.

            • 3. Re: Custom Hibernate Dialect - Jboss 7.1
              jje

              Ah, ok. Yep, i need to register functions, and therefore have to live with the module hack.

              • 4. Re: Custom Hibernate Dialect - Jboss 7.1
                stliu

                I believe this is caused due to you're using native hibernate and building session factory by yourself, I bet if you're using JPA then there won't such exception since AS would inject correct class loader to the hibernate classloading service, could you give it a try?

                • 5. Re: Custom Hibernate Dialect - Jboss 7.1
                  jje

                  Suspect you are correct. However, due to other complications in the (legacy) application that we're bringing to JBoss7 we're stuck with building the session factory ourselves. In the longer term we'll move to using JPA, and hopefully that will make this issue go away.

                  • 6. Re: Custom Hibernate Dialect - Jboss 7.1
                    rodrigobviana

                    I found a similar issue in https://community.jboss.org/message/755971

                    The fix doesn't require a module and worked for me.

                    But I don't know if this is best way to fix this.

                    Does anyone see any disadvantages?


                    Thanks in advance

                     

                    Rodrigo