starzero: an ideacandi playground — SmartFox Server and Hibernate

SmartFox Server and Hibernate

Written by admin. Posted at 6:08 am on October 16th, 2009

I have been working with SmartFox Server Pro for about 3 months now.  In those 3 months, I have had many ups and downs dealing with a relatively steep learning curve with respect to some of SmartFox’s quirks.  I wanted to pass on what I have learned thus far to the community in hopes that it may help to facilitate more growth in this area with what seems to be a stellar product.

So let’s start with Hibernate

As many of you know, Hibernate is an ORM (Object Relational Mapper).  My purpose here is not to discuss how to use Hibernate (their community has done a phenomenal job of doing this for you already … I also recommend the fantastic book, Java Persistence with Hibernate … I would not have been able to learn Hibernate with out it), but to discuss what I have done to get it working with SmartFox.  I want to start the discussion by explaining how I have SmartFox set up and it will help to understand why things need to go where I am going to tell you to put them.

SmartFox has two ways that it can be started, either through start.sh in the /Server folder or via the sfs start command.  The sfs start command will use the Java Service Wrapper with the configuration file contained in /Server/config/wrapper.conf.  I choose the latter simply because of the power that the Wrapper allows for (including debugging, which is indispensable when developing a SmartFox application).  If you open this file and look around a bit, or are familiar with the Java Service Wrapper, you will find that it is chiefly responsible for defining Java’s classpath that is used when SmartFox starts up.  The wrapper.java.classpath directive is extremely important.  This brings us to the first configuration note that begins our journey to Hibernate happiness.  There are 4 library (jar) files that are required to utilize all of Hibernate’s glorious features.  Essentially you will make 4 separate entries in wrapper.conf that look like below:

wrapper.java.classpath.41=/opt/SFS_PRO_1.6.6/Server/lib/hibernate3.jar
wrapper.java.classpath.43=/opt/SFS_PRO_1.6.6/Server/lib/log4j-1.2.15.jar
wrapper.java.classpath.47=/opt/SFS_PRO_1.6.6/Server/lib/hibernate-annotations.jar
wrapper.java.classpath.48=/opt/SFS_PRO_1.6.6/Server/lib/javax.persistence_1.99.0.v200906021518.jar
wrapper.java.classpath.49=/opt/SFS_PRO_1.6.6/Server/lib/hibernate-commons-annotations.jar
wrapper.java.classpath.50=/opt/SFS_PRO_1.6.6/Server/lib/ejb3-persistence.jar

I must apologize at this point because I want to go off on a tangent for a moment.  If you will notice there is a fifth entry there for log4j.  I have not, despite countless hours and most of my hair now missing, been able to get log4j to properly work with Hibernate (or at all, for that matter).  If anyone could leave a comment or contact me on the SmartFox server forums (starzero, is my name) on how to do this, I would be eternally grateful.

Please note that each wrapper.java.classpath entry has an integer value that is incrementing by 1.  This is very important as it is a Wrapper convention (so, please follow it).  Now, as you are no doubt aware, you will restart the SmartFox server with the command sfs restart (if you are new to SmartFox development and utilize a large body of Java based extensions, you will get used to restarting the server).  Once the server is restarted each of those libraries will be available to you within your Zone or Room based extension.  You are now free to create your Entities fire up your Sessions and control Transaction Demarcation to your database (sounds exciting, doesn’t it?)!

This has been a very short post, but thank you for reading it.  It is my hope you learned something.  I hope to put together many more of these including my development environment / methodology with SmartFox as well as a brief tutorial (with pictures) on how to debug your extensions (yes, this is covered in the forums, but may be helped with a bit more explanation).

11 comments.

  1. Excellent post, thanks a lot. I’m fairly new to Java and is just about to integrate Hibernate with my Smartfox Server project. This helps a lot.


  2. Hey, I’m new the Java world, so sorry for newbie question.

    I have Hibernate hooked up and trying a simple test.

    I get this exception

    Exception in ExtensionHandler: org.hibernate.MappingNotFoundException: resource: net/pubpals/data/user/User.hbm.xml not found

    Exception type: org.hibernate.MappingNotFoundException
    org.hibernate.MappingNotFoundException: resource: net/pubpals/data/user/User.hbm.xml not found

    Basically, I have a class called User in it’s package in javaExtensions together with the User.hbm.xml but somehow it seems it doesn’t find the mapping file.

    It does load the hibernate.cfg.xml in which I have

    Am I not suposed to keep my DAO classes in the javaExtensions folder? Just as the other java code for the extensions?


  3. sorry, it stripped out


  4. mapping resource=”net/pubpals/data/user/User.hbm.xml”


  5. Ok, I now got this working, very nice…

    It seems I had to put my classes in

    Server/mypackage…

    Rather then in javaExtensions/

    Which I guess is correct then?

    Any guidelines from someone experienced on as where to put stuff would be much appreciated.


  6. Hi,

    Firstly, thanks for commenting!

    In your wrapper.conf file you should have a classpath delcaration similar to wrapper.java.classpath.3=/opt/SFS_PRO_1.6.6/Server/javaExtensions/. If not, then place that line in your file (noting the classpath.# format and the full path to your extensions folder).

    The way you have it now is fine, but if you want to further separate your extension code from the smartfox server resources, then you can specify any path you would like in your wrapper. The important thing is that it is accessible from your classpath.

    paul


  7. [...] favor of the built in JDBC interface that comes, by default, with Smartfox (please see my earlier post to see how that is set up … not too terribly difficult).  The main reason behind that [...]


  8. Hi! Thanks for this post. I try to do as you’ve wrote here.
    The Question: Where can i get this all jars?

    hibernate3.jar
    log4j-1.2.15.jar
    hibernate-annotations.jar
    javax.persistence_1.99.0.v200906021518.jar
    hibernate-commons-annotations.jar
    ejb3-persistence.jar


  9. Hello,

    I would recommend doing some additional research into the projects you want to use including hibernate and log4j. The project sites at hibernate.org and log4j’s sourceforge site both have the required jars.

    paul


  10. I can not find the jar files.
    Where it can be found. I’m new to Hibernate and have some navigation problems….. :-)
    Eyal.


  11. Great post , but I have a following question . Don’t i have to include also JDBC Driver in order to connect to the DataBase ? Second question: does it matters where I put mappping configuration files in SmartFox relative to Extention ?
    Thanks


Post a comment.

Links