Spring-data-mongo on Play


is there a simple way to use spring-data-mongo under Play?

If someone use MongoDB, which library/plugin do you use under Play?
I tried play-jongo (but for the moment isn’t updated), play-morphia (I wrote a plugin to use with Play but morphia seems not to be maintained). So, I would use spring-data-mongo that seems to be the only one really maintained and updated. I founded a way to use it with Play, but it’s very complex and in the next Play versions I’m not sure it’s continue to work.
So I would like to find a way to use it in a more easy way and I’m happy if someone could explain me.

1 Like

I have used both Morphia and JPA (with Hibernate OGM). Both works well and are straightforward to set up, and I did not need any special plugins to make them work, so I was able to upgrade Play from 2.5 to 2.7 (milestone) without any major hassles.

I have also looked at Jongo, but it did not fit my needs.

Thank you for your answer. I like Morphia but its developing seems to be stopped. Can you explain me how to use Hibernate OGM under Play?

There is also ReactiveMongo and Play-ReactiveMongo if you are using Scala.

For Hibernate OGM, it is very simple. Since Hibernate is compliant with JPA and Play has support for JPA out of box, all you need to do is follow the Play JPA documentation on enabling and APIs to use (namely JPAApi). Some notable changes include:

  • include Hibernate OGM (with MongoDB) dependencies instead of normal Hibernate ORM
  • instead of having your database connection details in your application.conf, you will need to have them as parameters in the persistence.xml file (there might be a way to have them in the application.conf, but persistence.xml works and you can have env var with it too, so I never bothered)
  • consult the MongoDB section of Hibernate OGM about the specific values to use in persistence.xml file, such as provider class, dialect, etc…

That’s pretty much it. The only thing you should keep an eye out (which you might already know), is since JPA needs a persistence session to work, sometimes it will get in the way if you have lazy loading.

Also as @marcospereira mentioned, if you are using Scala, probably look at the Scala options, since the Scala drivers are fully non-blocking, so lets your application to be fully non-blocking. You can, of course, also try the non-blocking Java drivers, but those I find a bit convoluted to use.

Thank you very much. I’m using Java so I’ll try to use Hibernate OGM.