6 Replies Latest reply on May 11, 2011 8:12 AM by Trong Tran

    How to extend HibernateService

    Sergey Zubarev Newbie

      Hello!

       

      I need to extend HibernateServiceImpl and add this as service.

       

      I created interface:

       

      public interface HibernateService extends org.exoplatform.services.database.HibernateService{
            
      }
      

       

      and class:

       

       

      package ru.tersys.projects.asdu.repair.core.service.impl;
      
      import ru.tersys.projects.asdu.repair.core.service.HibernateService;
      import org.exoplatform.container.component.ComponentPlugin;
      import org.exoplatform.container.xml.InitParams;
      import org.exoplatform.services.cache.CacheService;
      
      public class HibernateServiceImpl extends org.exoplatform.services.database.impl.HibernateServiceImpl 
                                          implements HibernateService
      {
          public HibernateServiceImpl(InitParams initParams, CacheService cacheService)
          {
              super(initParams, cacheService); 
          }
          
      }
      

       

      conf/portal/configuration.xml:

       

       

      <?xml version="1.0" encoding="UTF-8"?>
      <configuration
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_1.xsd http://www.exoplaform.org/xml/ns/kernel_1_1.xsd"
         xmlns="http://www.exoplaform.org/xml/ns/kernel_1_1.xsd"> 
      
        <component>
          <key>ru.tersys.projects.asdu.repair.core.service.HibernateService</key>
          <type>ru.tersys.projects.asdu.repair.core.service.impl.HibernateServiceImpl</type>
          <init-params>
            <properties-param>
              <name>hibernate.properties</name>
              <description>My Hibernate Service</description>
              <property name="hibernate.show_sql" value="false"/>
              <property name="hibernate.current_session_context_class" value="thread"/>
              <property name="hibernate.cache.use_second_level_cache" value="true"/>
              <property name="hibernate.cache.use_query_cache" value="true"/>
              <!--CHANGEME HashtableCacheProvider shold not be used in production env-->
              <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
              <property name="hibernate.connection.datasource" value="asduRWS${container.name.suffix}"/>
              <property name="hibernate.connection.autocommit" value="true"/>
            </properties-param>
          </init-params>
        </component>
      
        <external-component-plugins>
          <target-component>org.exoplatform.services.naming.InitialContextInitializer</target-component>
          <component-plugin>
            <name>bind.datasource</name>
            <set-method>addPlugin</set-method>
            <type>org.exoplatform.services.naming.BindReferencePlugin</type>
            <init-params>
              <value-param>
                <name>bind-name</name>
                <value>asduRWS${container.name.suffix}</value>
              </value-param>
              <value-param>
                <name>class-name</name>
                <value>javax.sql.DataSource</value>
              </value-param>
              <value-param>
                <name>factory</name>
                <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
              </value-param>
              <properties-param>
                <name>ref-addresses</name>
                <description>ref-addresses</description>
                <property name="driverClassName" value="${portal.container.gatein.jcr.datasource.driver}"/>
                <property name="url" value="${portal.container.gatein.jcr.datasource.url}"/>
                <property name="username" value="${portal.container.gatein.jcr.datasource.username}"/>
                <property name="password" value="${portal.container.gatein.jcr.datasource.password}"/>
              </properties-param>
            </init-params>
          </component-plugin>
        </external-component-plugins>
        
          <external-component-plugins>
          <target-component>ru.tersys.projects.asdu.repair.core.service.HibernateService</target-component>
          <component-plugin>
              <name>add.hibernate.mapping</name>
              <set-method>addPlugin</set-method>
              <type>org.exoplatform.services.database.impl.AddHibernateMappingPlugin</type>
              <init-params>
                  <values-param>
                <name>hibernate.annotations</name>
                       <value>ru.tersys.projects.asdu.repair.planning.model.RepairRequest</value>
                       <value>ru.tersys.projects.asdu.repair.planning.model.RepairRequestHistory</value>                 
                       <value>ru.tersys.projects.asdu.repair.planning.model.dictionary.CpprStatus</value>
                       <value>ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadErrorType</value>
                       <value>ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadStatus</value>
                       <value>ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadUchType</value>
                  </values-param>
              </init-params>
          </component-plugin>
        </external-component-plugins>  
      
      </configuration>  
      

       

      But when server starts there is no tables in database (if switch to "classic" HibernateService - all ok).

       

      In logs I found following:

      2011-05-10 15:17:29,775 DEBUG [exo.kernel.container.ExoContainer] ru.tersys.projects.asdu.repair.core.service.impl.HibernateServiceImpl {hibernate.properties={hibernate.show_sql=false, hibernate.current_session_context_class=thread, hibernate.cache.use_second_level_cache=true, hibernate.cache.use_query_cache=true, hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider, hibernate.connection.datasource=asduRWS, hibernate.connection.autocommit=true}} added to portal
      2011-05-10 15:17:29,882 INFO  [org.hibernate.dialect.Dialect] Using dialect: org.hibernate.dialect.Oracle10gDialect
      2011-05-10 15:17:29,883 DEBUG [exo.kernel.container.ExoContainer] org.exoplatform.services.database.impl.AddHibernateMappingPlugin {hibernate.annotations=ru.tersys.projects.asdu.repair.planning.model.RepairRequest,ru.tersys.projects.asdu.repair.planning.model.RepairRequestHistory,ru.tersys.projects.asdu.repair.planning.model.dictionary.CpprStatus,ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadErrorType,ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadStatus,ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadUchType} added to portal
      2011-05-10 15:17:29,883 ERROR [exo.kernel.container.MX4JComponentAdapter] Failed to instanciate plugin add.hibernate.mappingfor component ru.tersys.projects.asdu.repair.core.service.impl.HibernateServiceImpl@1d416eb: null
      java.lang.NullPointerException
          at org.exoplatform.container.jmx.MX4JComponentAdapter.addComponentPlugin(MX4JComponentAdapter.java:148)
          at org.exoplatform.container.jmx.MX4JComponentAdapter.getComponentInstance(MX4JComponentAdapter.java:106)
          at org.exoplatform.container.management.ManageableComponentAdapter.getComponentInstance(ManageableComponentAdapter.java:68)
          at org.exoplatform.container.ConcurrentPicoContainer.getInstance(ConcurrentPicoContainer.java:400)
          at org.exoplatform.container.ConcurrentPicoContainer.getComponentInstancesOfType(ConcurrentPicoContainer.java:350)
          at org.exoplatform.container.CachingContainer.getComponentInstancesOfType(CachingContainer.java:111)
          at org.exoplatform.container.component.RequestLifeCycleStack.begin(RequestLifeCycleStack.java:66)
          at org.exoplatform.container.component.RequestLifeCycle.begin(RequestLifeCycle.java:107)
          at org.exoplatform.container.component.RequestLifeCycle.begin(RequestLifeCycle.java:141)
          at org.exoplatform.portal.config.UserPortalConfigService.start(UserPortalConfigService.java:694)
          at sun.reflect.GeneratedMethodAccessor321.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:597)
          at org.picocontainer.defaults.LifecycleVisitor.traverse(LifecycleVisitor.java:75)
          at org.picocontainer.defaults.LifecycleVisitor.start(LifecycleVisitor.java:113)
          at org.exoplatform.container.ConcurrentPicoContainer.start(ConcurrentPicoContainer.java:453)
          at org.exoplatform.container.ExoContainer.start(ExoContainer.java:186)
          at org.exoplatform.container.PortalContainer.start(PortalContainer.java:595)
          at org.exoplatform.container.ExoContainer.start(ExoContainer.java:180)
          at org.exoplatform.container.RootContainer.createPortalContainer(RootContainer.java:338)

       

      2011-05-10 15:17:29,775 DEBUG [exo.kernel.container.ExoContainer] ru.tersys.projects.asdu.repair.core.service.impl.HibernateServiceImpl {hibernate.properties={hibernate.show_sql=false, hibernate.current_session_context_class=thread, hibernate.cache.use_second_level_cache=true, hibernate.cache.use_query_cache=true, hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider, hibernate.connection.datasource=asduRWS, hibernate.connection.autocommit=true}} added to portal
      2011-05-10 15:17:29,882 INFO  [org.hibernate.dialect.Dialect] Using dialect: org.hibernate.dialect.Oracle10gDialect
      2011-05-10 15:17:29,883 DEBUG [exo.kernel.container.ExoContainer] org.exoplatform.services.database.impl.AddHibernateMappingPlugin {hibernate.annotations=ru.tersys.projects.asdu.repair.planning.model.RepairRequest,ru.tersys.projects.asdu.repair.planning.model.RepairRequestHistory,ru.tersys.projects.asdu.repair.planning.model.dictionary.CpprStatus,ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadErrorType,ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadStatus,ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadUchType} added to portal
      2011-05-10 15:17:29,883 ERROR [exo.kernel.container.MX4JComponentAdapter] Failed to instanciate plugin add.hibernate.mappingfor component ru.tersys.projects.asdu.repair.core.service.impl.HibernateServiceImpl@1d416eb: null
      java.lang.NullPointerException
           at org.exoplatform.container.jmx.MX4JComponentAdapter.addComponentPlugin(MX4JComponentAdapter.java:148)
           at org.exoplatform.container.jmx.MX4JComponentAdapter.getComponentInstance(MX4JComponentAdapter.java:106)
           at org.exoplatform.container.management.ManageableComponentAdapter.getComponentInstance(ManageableComponentAdapter.java:68)
           at org.exoplatform.container.ConcurrentPicoContainer.getInstance(ConcurrentPicoContainer.java:400)
           at org.exoplatform.container.ConcurrentPicoContainer.getComponentInstancesOfType(ConcurrentPicoContainer.java:350)
           at org.exoplatform.container.CachingContainer.getComponentInstancesOfType(CachingContainer.java:111)
           at org.exoplatform.container.component.RequestLifeCycleStack.begin(RequestLifeCycleStack.java:66)
           at org.exoplatform.container.component.RequestLifeCycle.begin(RequestLifeCycle.java:107)
           at org.exoplatform.container.component.RequestLifeCycle.begin(RequestLifeCycle.java:141)
           at org.exoplatform.portal.config.UserPortalConfigService.start(UserPortalConfigService.java:694)
           at sun.reflect.GeneratedMethodAccessor321.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:597)
           at org.picocontainer.defaults.LifecycleVisitor.traverse(LifecycleVisitor.java:75)
           at org.picocontainer.defaults.LifecycleVisitor.start(LifecycleVisitor.java:113)
           at org.exoplatform.container.ConcurrentPicoContainer.start(ConcurrentPicoContainer.java:453)
           at org.exoplatform.container.ExoContainer.start(ExoContainer.java:186)
           at org.exoplatform.container.PortalContainer.start(PortalContainer.java:595)
           at org.exoplatform.container.ExoContainer.start(ExoContainer.java:180)
           at org.exoplatform.container.RootContainer.createPortalContainer(RootContainer.java:338)
      

       

      Please help!

        • 1. How to extend HibernateService
          Trong Tran Master

          what do you mean by "extend HibernateService" ?

           

          you want to replace the HibernateService implementation in GateIn by your own one OR you just want to create a new service which its java class extends from GateIn one ?

          • 2. How to extend HibernateService
            Sergey Zubarev Newbie

            Hello, Trong Tran.

             

            I need to create new service, which extends existing HibernateService.

            • 3. Re: How to extend HibernateService
              Trong Tran Master

              So let say your service is named as FooHibernateService, you could try to do something like following :

               

              public class FooHibernateService extends org.exoplatform.services.database.impl.HibernateServiceImpl
              {
                 public FooHibernateService(InitParams initParams, CacheService cacheService)
                 {
                    super(initParams, cacheService);
                 }
              
              }
              

               

              and the configuration :

               

              <?xml version="1.0" encoding="UTF-8"?>
              <configuration
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_1.xsd http://www.exoplaform.org/xml/ns/kernel_1_1.xsd"
                 xmlns="http://www.exoplaform.org/xml/ns/kernel_1_1.xsd"> 
              
                <component>
                  <key>ru.tersys.projects.asdu.repair.core.service.FooHibernateService</key>
                  <type>ru.tersys.projects.asdu.repair.core.service.FooHibernateService</type>
                  <init-params>
                    <properties-param>
                      <name>hibernate.properties</name>
                      <description>My Hibernate Service</description>
                      <property name="hibernate.show_sql" value="false"/>
                      <property name="hibernate.current_session_context_class" value="thread"/>
                      <property name="hibernate.cache.use_second_level_cache" value="true"/>
                      <property name="hibernate.cache.use_query_cache" value="true"/>
                      <!--CHANGEME HashtableCacheProvider shold not be used in production env-->
                      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
                      <property name="hibernate.connection.datasource" value="asduRWS${container.name.suffix}"/>
                      <property name="hibernate.connection.autocommit" value="true"/>
                    </properties-param>
                  </init-params>
                </component>
              
                <external-component-plugins>
                  <target-component>org.exoplatform.services.naming.InitialContextInitializer</target-component>
                  <component-plugin>
                    <name>bind.datasource</name>
                    <set-method>addPlugin</set-method>
                    <type>org.exoplatform.services.naming.BindReferencePlugin</type>
                    <init-params>
                      <value-param>
                        <name>bind-name</name>
                        <value>asduRWS${container.name.suffix}</value>
                      </value-param>
                      <value-param>
                        <name>class-name</name>
                        <value>javax.sql.DataSource</value>
                      </value-param>
                      <value-param>
                        <name>factory</name>
                        <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
                      </value-param>
                      <properties-param>
                        <name>ref-addresses</name>
                        <description>ref-addresses</description>
                        <property name="driverClassName" value="${portal.container.gatein.jcr.datasource.driver}"/>
                        <property name="url" value="${portal.container.gatein.jcr.datasource.url}"/>
                        <property name="username" value="${portal.container.gatein.jcr.datasource.username}"/>
                        <property name="password" value="${portal.container.gatein.jcr.datasource.password}"/>
                      </properties-param>
                    </init-params>
                  </component-plugin>
                </external-component-plugins>
              
                <external-component-plugins>
                  <target-component>ru.tersys.projects.asdu.repair.core.service.FooHibernateService</target-component>
                  <component-plugin>
                      <name>add.hibernate.mapping</name>
                      <set-method>addPlugin</set-method>
                      <type>org.exoplatform.services.database.impl.AddHibernateMappingPlugin</type>
                      <init-params>
                          <values-param>
                        <name>hibernate.annotations</name>
                               <value>ru.tersys.projects.asdu.repair.planning.model.RepairRequest</value>
                               <value>ru.tersys.projects.asdu.repair.planning.model.RepairRequestHistory</value>                 
                               <value>ru.tersys.projects.asdu.repair.planning.model.dictionary.CpprStatus</value>
                               <value>ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadErrorType</value>
                               <value>ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadStatus</value>
                               <value>ru.tersys.projects.asdu.repair.planning.model.dictionary.PlanLoadUchType</value>
                          </values-param>
                      </init-params>
                  </component-plugin>
                </external-component-plugins>  
              
              </configuration> 
              
              • 4. Re: How to extend HibernateService
                Sergey Zubarev Newbie

                I do as you write, but no tables is created (hibernate.hbm2ddl.auto=create).

                It seems that plugin add function called before service add.

                • 5. How to extend HibernateService
                  Sergey Zubarev Newbie

                  In HibernateServiceImpl code I see that buildSession method is called when openSession method calls first time. But it should be called due app startup, otherwise exception "org.hibernate.MappingException: Unknown entity:" is thrown.

                  • 6. How to extend HibernateService
                    Trong Tran Master

                    i think the openSession() should be called at least once to create tables.

                     

                    To do that, your service could implement org.picocontainer.Startable interface with start() and stop() methods. The start() method will be called after the service registered to the container and in here you could do a openSession() operation once. You may need to close the session then.