Friday, December 2, 2011

SmartFox Server + Unity3d + Hibernate + JPA

This is intended as a post for anyone who's familiar with Hibernate & JPA, as its not very obvious as to where everything goes, you can find all the info, but its spread across numerous posts that don't seem to include all of the relevant information.

So.. here's the quick and dirty of how to set this up to get it to work in SmartFox 2x.

1. Track down the following jars and place them in your SFS2X/lib directory.

antlr-2.7.6.jar
dom4j-1.6.1.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar

2. Add your jdbc driver to the SFS2X/lib directory

3. Create a new project to hold your entities and add your entities to it.

4. Export the Entity project as a jar to the SFS2X/lib directory.


5. Create a META-INF/Persistence.xml file under the SFS2X directory

6. Ensure that you add the jar-file attribute to the persistence.xml. It is referenced from theSFS2X root.. not the META-INF directory, so you simply need to reference the lib/foo.jar file in the persistence.xml in order for Hibernate to scan your classes.

<jar-file>lib/gameEntities.jar</jar-file>

7. Add your game project jar to the SFS2X/extensions/{project} directory.

8. Add an appender for hibernate to the log4j properties file at SFS2X/config/

log4j.category.org.hibernate=INFO,consoleAppender

It's really that simple... Hopefully, this will save a few others some time when working with this configuration.

If you notice a magic number exception.. you are probably running RC3, which seems to have a classfile conflict. Use RC1b and that should solve the problem..

6 comments:

  1. Hello Shannon Pettit
    Thank for sample
    but have some error
    this first: Unable to configure EntityManagerFactory

    Please help me

    ReplyDelete
    Replies
    1. This is usually related to not having the hibernate jars on the class path. Did you include the hibernate3.jar in the SFS2X/lib directory?

      Delete
  2. Hey Shane,

    Thanks for the post, it helped me get one step closer to hibernate + JPA + smartfox.

    I know this is an old post, but I'm wondering if you ever got around the "magic number" problem in RC3 of SFS2X, cause I'm hitting the same brick wall in the current build. I've tracked it down and one of the class files in the smartfox lib folder doesn't conform to a standard java class, but the smartfox guys believe this is how its meant to be. So I'm brickwalling at the moment, I may have to revert to using hibernate.cfg.xml files instead.

    I'm currently investigating modifying hibernate EntityManger to skip classes that it cannot read instead of crashing. Yay for learning the hibernate build process!

    ReplyDelete
    Replies
    1. My solution was to just go to a different version of SmartFox. If you know the exact class, you could decompile it to source then recompile it (or fix it).. But that's a bit of a pain to do.

      In my most recent projects, I've actually bypassed JPA in favor of raw hibernate - JPA didn't added enough value to my project to warrant some of it's 'Quirks'..

      Delete
  3. please provide server side code snippet to use hibernate

    ReplyDelete
    Replies
    1. There are quite a few tutorials on how to use hibernate - this blog isn't really the place for that.

      Delete