{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

Use Spring and Hibernate with MongoDB

Published by

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
@Table(name = "logItems")
public class LogItem implements Serializable {

    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;

    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"

    <import resource="spring-mongodb-config.xml"/>
    <bean id="logService" class="net.pascalalma.mongo.services.MongoDBLoggerServiceImpl">
        <property name="dao" ref="logDao"/>
    <bean id="logDao" class="net.pascalalma.mongo.dao.LogItemDaoImpl">
        <property name="mongoTemplate" ref="mongoTemplate"/>
and the ‘spring-mongodb-config.xml’ (as described here):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

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

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

    <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
        <constructor-arg ref="mongoDbFactory"/>
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.


Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB.

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks