9 Replies Latest reply on Jul 1, 2010 6:44 AM by simonheckmann

    What to use as persistence api?

    simonheckmann

      Hello everyone,

       

      This might look like a stupid question, but I am honestly stuck and do not know where to look for solutions, so I thought I just ask:

       

      I have developed a little JSF-based task list Portlet, which runs fine. Now I want to persist some tasks, which can be loaded the next time the user logs in. My main question is just: How should I best persist the tasks. Does GateIn offer any services for that? I see that it comes with Hibernate, so I am wondering if I can easily use Hibernate through GateIn. Or is it best to use some other JPA framework?

       

      It would be nice if you could share your ideas on this and it would be highly appreciated if you could point me to corresponding documents or tutorials.

       

      Thank you very much in advance!

       

      Kind regards,

      Simon Heckmann

        • 1. Re: What to use as persistence api?
          simonheckmann

          Hello everyone,

           

          I played around a bit trying to use Hibernate to persist my objects. I created a Task.java which is supposed to serve as the model and I also created a Task.hbm.xml to let Hibernate know about the mapping between the database and Java. Now I started implementing the appropriate manager class and I am stuck again. How do I create get a SessionFactory? I tried

           

          Configuration().configure().buildSessionFactory();
          

           

          But this always is null, because no hibernate.cfg.xml is found.

          Is there a way to use GateIn's configuration and access it from my code?

           

          I found something like

           

          HibernateServiceContainer hibernateContainer = (HibernateServiceContainer) PortalContainer.getInstance().getComponentInstanceOfType(HibernateServiceContainer.class);
          HibernateService hibernateService = hibernateContainer.getHibernateService(dsName);
          hibernateService.getSessionFactory();
          

           

          but this does not compile because HibernateServiceContainer is not available.

           

          For my first steps in Hibernate I used this tutorial: http://docs.jboss.org/hibernate/stable/core/reference/en/html/tutorial.html#tutorial-firstapp

           

          Any hint would be highly appreciated!

           

          Kind regards,

          Simon Heckmann

          • 2. Re: What to use as persistence api?
            trong.tran

            as another choice if you mind to use JCR which is used to persist  data in GateIn portal. And the starting point is the RepositoryService service,  you can retrieve it by :

             

            RepositoryService repositoryService = (RepositoryService) PortalContainer.getInstance().getComponentInstanceOfType(RepositoryService.class);
            
            
            1 of 1 people found this helpful
            • 3. Re: What to use as persistence api?
              nscavell

              For simplicity sake, you should be able to just use the HibernateService as such:

               

              HibernateService hs = (HibernateService) ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(HibernateService.class);
              Session session = hibernateService.openSession();
              

               

              To add your own hibernate mappings just add the following inside a configuration xml file of your conf/portal/configuration.xml file as such:

               

               

              <external-component-plugins>
                  <target-component>org.exoplatform.services.database.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.mapping</name>
                              <value>mappings/SomeObject.hbm.xml</value>
                          </values-param>
                      </init-params>
                  </component-plugin>
              </external-component-plugins>
              

               

              Hope this helps.

              1 of 1 people found this helpful
              • 4. Re: What to use as persistence api?
                simonheckmann

                Hello,

                 

                This helps a lot, although I am not 100% sure were to put all the information. I edited the following config file:

                 

                {PATH}/GateIn-3.1.0-GA/webapps/portal/WEB-INF/conf/configuratuion.xml

                 

                And added what you proposed:

                 

                <external-component-plugins>
                    <target-component>org.exoplatform.services.database.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.mapping</name>
                                <value>Task.hbm.xml</value>
                            </values-param>
                        </init-params>
                    </component-plugin>
                </external-component-plugins>
                

                 

                Then I put the file "Task.hbm.xml" in the folder {PATH}/GateIn-3.1.0-GA/webapps/portal/WEB-INF/

                 

                This does not seem to work. So two questions remain:

                 

                Was it the right configuration file I updated? Where do I have to put the "Task.hbm.xml" file?

                 

                Kind regards,

                Simon Heckmann

                • 5. Re: What to use as persistence api?
                  nscavell

                  I'm working with an extension portal which I would recommend doing anyways, but if your modifying the gatein distribution then I think you can place your mapping file under the {PATH}/GateIn-3.1.0-GA/webapps/portal/WEB-INF/classes folder.  The path you specify in the xml is relative to the classpath.

                   

                  If you look at {PATH}/GateIn-3.1.0-GA/webapps/portal/WEB-INF/conf/organization/exo/hibernate-configuration.xml directory you can see the same thing.  You can probably even just add your mapping file there if you wish and remove the need to add another hibernate mapping plugin.

                  • 6. Re: What to use as persistence api?
                    simonheckmann

                    Hello,

                     

                    Sadly I cannot make it work this way. I modified {GateIn}/webapps/portal/WEB-INF/conf/organization/exo/hibernate-configuration.xml and added the following:

                     

                    <value>Task.hbm.xml</value>
                    

                     

                    Then I put the file "Task.hbm.xml" into {GateIn}/webapps/portal/WEB-INF/classes but all I get is "org.hibernate.MappingException: Unknown entity: com.heckmann.tasklist.model.Task"

                     

                    Another approach that I thought of was adding the mapping in the source code, but this does not seem to work either:

                     

                    HibernateService hs = (HibernateService) ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(HibernateService.class);
                    Configuration cfg = hs.getHibernateConfiguration();
                    cfg.addClass(com.heckmann.tasklist.model.Task.class);
                    Session session = cfg.buildSessionFactory().openSession();
                    

                     

                    It this even possible? And if yes, what am I missing?

                     

                    Again, thank you very much!

                     

                    Kind regards,

                    Simon Heckmann

                    • 7. Re: What to use as persistence api?
                      nscavell

                      Your java class the hibernate mapping is referring to is not in the classpath of the portal application, which is why I recommended you to deploy this as an extension portal application like the gatein-sample-extension example.  Not sure what you're setup is but if you just want to make this work just drop your jar that has these classes in the WEB-INF/lib folder of portal the app.

                      • 8. Re: What to use as persistence api?
                        prabhat.jha

                        I have not gone through the complete thread but it seems like you are having problem creating a hibernate session. We just built a poral .war which has its own datasource ( you deploy your own -ds.xml in server/default/deploy) and its own hibernate configuration and it worked without any problem. Here is relevant piece of code:

                         

                        SessionFactory sessionFactory = new AnnotationConfiguration().configure("/conf/hibernate.cfg.xml").buildSessionFactory();

                         

                        where /conf/hibernate.cfg.xml is in WEB-INF/classes in the .war archive. You refer to your datasource in this hibernate.cfg.xml.

                         

                        Hope this helps,

                        Prabhat

                        • 9. Re: What to use as persistence api?
                          simonheckmann

                          Hi everyone,

                           

                          this worked for me! I have to use a different configuration file now, but at least it gets the job done!

                           

                          Thank you everyone for you help!

                           

                          Kind regards,

                          Simon Heckmann