Use Spring and Hibernate with MongoDB

One of the requirements we have for one of our Mule ESB implementations is to log the messages that went through the ESB (especially handy during development and testing). There has been written a lot already about logging with Mule so I am not going to discuss that again. In this post I’ll only show you how you can use Spring/Hibernate and MongoDB to store your messages in the MongoDB, because a document-oriented database like MongoDB is a nice fit for this requirement.

The first thing to do is to install MongoDB on your machine (unfortunately it is not yet possible to run it embedded so we use a local instance instead). For installation see this.

After MongoDB is installed we can create a new standard Maven project and modify the generated pom.xml to the following pom.xml.

Next step is the creation of the Hibernate Entity class like this:

package net.pascalalma.mongo.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.bson.types.ObjectId;

/**
 * Model object 
 * @author pascal
 */
@Entity
@Table(name = "logItems")
public class LogItem implements Serializable {

    @Id
    private ObjectId id;
    private String message;
    private String timestamp;

    public ObjectId getId() {
        return id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(final String timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public String toString() {
        return "LogItem [id=" + id + ", message=" + message + ", timestamp=" + timestamp + "]";
    }
}

As you might notice I have used the ObjectId as primary Id. I was forced to this by bug DATADOC-176 which only recently got fixed.

The Dao and Service object classes that are used to access the MongoDB are:

Now wire this all together with the following spring config files:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <import resource="spring-mongodb-config.xml"/>
  
    <bean id="logService" class="net.pascalalma.mongo.services.MongoDBLoggerServiceImpl">
        <property name="dao" ref="logDao"/>
    </bean>
    <bean id="logDao" class="net.pascalalma.mongo.dao.LogItemDaoImpl">
        <property name="mongoTemplate" ref="mongoTemplate"/>
    </bean>
</beans>

and the ‘spring-mongodb-config.xml’ (as described here):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/data/mongo
                http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

    <!-- Default bean name is 'mongo' -->
    <mongo:mongo host="localhost" port="27017">
        <!-- OPTIONAL: configure <mongo:options /> -->
    </mongo:mongo>

    <mongo:db-factory dbname="pascalalma" mongo-ref="mongo"/>

    <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
        <constructor-arg ref="mongoDbFactory"/>
    </bean>
</beans>

That’s it. Now you have a foundation to start using MongoDB with Spring and Hibernate classes. In the next post I will show how you can test this setup without manually having to start and stop MongoDB.

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in General, MongoDB, Spring Framework and tagged , . Bookmark the permalink.

1 Response to Use Spring and Hibernate with MongoDB

  1. Pedro says:

    Hi

    That is very useful, I was using a previous version of Spring Data for mongodb and struggled migrating to the new one.

Comments are closed.