[2.3.x-Java] Integrate Hibernate and MongoDB

303 views
Skip to first unread message

Francesco

unread,
Nov 23, 2014, 4:11:54 AM11/23/14
to play-fr...@googlegroups.com
Hi,
I should integrate Hibernate with Mongodb and Play 2.3.x  (Java version) but I have some problem.

This is my build.sbt:
name := "mongodb"

version := "1.0.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(play.PlayJava, com.typesafe.sbt.web.SbtWeb)

libraryDependencies ++= Seq(
  play.PlayImport.javaCore,
  play.PlayImport.javaJpa,
  play.PlayImport.javaWs,
  "org.hibernate.ogm" % "hibernate-ogm-core" % "4.1.0.Beta8",
  "org.mongodb" % "mongo-java-driver" % "2.12.4"
)

resolvers ++= Seq(
)

resolvers += Resolver.sonatypeRepo("releases")


This is the persistence.xml file:
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="2.0">

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.ogm.datastore.provider"
                      value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/>
            <property name="hibernate.ogm.mongodb.host" value="127.0.0.1"/>
            <property name="hibernate.ogm.mongodb.port" value="27017"/>
            <property name="hibernate.ogm.mongodb.database" value="db"/>
            <property name="hibernate.ogm.mongodb.safe" value="true"/>

        </properties>
    </persistence-unit>

</persistence>


and the only related info in application.conf is:
jpa.default=defaultPersistenceUnit

When I compile the project I have the following error message:
Could not load requested class : org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider

How can I solve and configure correctly the project?
Thanks,
F

Davide D'Alto

unread,
Nov 24, 2014, 4:53:29 AM11/24/14
to play-fr...@googlegroups.com
Hi,
I think you are missing the following library: hibernate-ogm-mongodb-4.1.0.Beta8.jar

I would also suggest to set the property hibernate.ogm.datastore.provider to mongodb

Cheres,
Davide

Francesco

unread,
Nov 25, 2014, 4:37:58 AM11/25/14
to play-fr...@googlegroups.com
Inserisci qui il cThanks Davide.
Now it works, but I am not sure that everything is correctly configured.

However, my build.sbt is:
name := "mongodb"

version := "1.0.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(play.PlayJava, com.typesafe.sbt.web.SbtWeb)

libraryDependencies ++= Seq(
  play.PlayImport.javaCore,
  play.PlayImport.javaJpa,
  play.PlayImport.javaWs,
  "org.hibernate" % "hibernate-entitymanager" % "4.3.6.Final",
  "org.hibernate.ogm" % "hibernate-ogm-core" % "4.1.0.Beta8",
  "org.hibernate.ogm" % "hibernate-ogm-mongodb" % "4.1.0.Beta8",
  "org.jboss.jbossts" % "jbossjta" % "4.16.6.Final"
)

resolvers ++= Seq(
)

resolvers += Resolver.sonatypeRepo("releases")


and the persistence.xml is:
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="2.0">

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.ogm.datastore.provider" value="mongodb"/>
            <property name="hibernate.ogm.datastore.host" value="127.0.0.1"/>
            <property name="hibernate.ogm.datastore.port" value="27017"/>
            <property name="hibernate.ogm.datastore.database" value="db"/>
            <property name="hibernate.ogm.datastore.safe" value="true"/>
        </properties>
    </persistence-unit>

</persistence>


I have created a dummy model:
@Entity
@Indexed
public class User implements Serializable {

    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("defaultPersistenceUnit");
    private static EntityManager em = emf.createEntityManager();

    private static final long serialVersionUID = 1L;

    @DocumentId
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String id;

    @Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
    public String username;

    public User(){}

    public User(String username) {
        this.username = username;
        this.save();
    }

    public void save() {

        Logger.debug("Saving User ");

        if (em == null || !em.isOpen())
            em = emf.createEntityManager();

        try {
            em.getTransaction().begin();
            em.persist(this);
            em.getTransaction().commit();
        } catch (Exception re) {
            em.getTransaction().rollback();
            throw re;
        } finally {
            if (em != null) {
                em.clear();
                em.close();
                em = null;
            }
        }
    }

    public void update() {

        Logger.debug("Updating User ");

        if (em == null || !em.isOpen())
            em = emf.createEntityManager();

        try {
            em.getTransaction().begin();
            em.merge(this);
            em.getTransaction().commit();
        } catch (Exception re) {
            em.getTransaction().rollback();
            throw re;
        } finally {
            if (em != null) {
                em.clear();
                em.close();
                em = null;
            }
        }
    }
}

so from my controller: 
User u = new User("Pippo"); -> I save the entry Pippo

u.username = "Pluto"; 
u.update(); -> I update the entry with Pluto

I am not sure on transaction management. This document can be useful http://tomee.apache.org/jpa-concepts.html
What do you think about?

Bye,
F

Roshan Kumar

unread,
Jun 3, 2016, 7:32:12 AM6/3/16
to play-framework

If you want to ODM that use java async driver you can use it

https://github.com/raimdtu/PlayAsyncJavaMongoODM

It will give you overview.

Reply all
Reply to author
Forward
0 new messages