Hi,
Hibernate OGM is an awesome tool which have an integration with MongoDB, but there is one missing engine and it is Fongo. Fongo is a project which uses Mongo client to create a Java embedded in-memory MongoDB. In fact no server endpoint is provided it is pure Java project (we can think like a Map). So if you want to use Hibernate OGM and write tests using Fongo you need to create your own DataStore provider.
Currently I have done this by:
Code:
public class FongoDBDatastoreProvider extends MongoDBDatastoreProvider {
private static final long serialVersionUID = -4709436439644997081L;
private static final Log log = LoggerFactory.getLogger();
private MongoClient mongo;
private DB mongoDb;
private MongoDBConfiguration config;
private ServiceRegistryImplementor serviceRegistry;
@Override
public void start() {
try {
if (mongo == null) {
mongo = new Fongo("fongo").getMongo();
}
mongoDb = extractDatabase(mongo, config);
} catch (Exception e) {
// Wrap Exception in a ServiceException to make the stack trace more
// friendly
// Otherwise a generic unable to request service is thrown
throw log.unableToStartDatastoreProvider(e);
}
}
@Override
public void stop() {
}
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}
@Override
public void configure(Map configurationValues) {
OptionsService optionsService = serviceRegistry
.getService(OptionsService.class);
ClassLoaderService classLoaderService = serviceRegistry
.getService(ClassLoaderService.class);
ConfigurationPropertyReader propertyReader = new ConfigurationPropertyReader(
configurationValues, classLoaderService);
try {
this.config = new MongoDBConfiguration(propertyReader,
optionsService.context().getGlobalOptions());
} catch (Exception e) {
// Wrap Exception in a ServiceException to make the stack trace more
// friendly
// Otherwise a generic unable to request service is thrown
throw log.unableToConfigureDatastoreProvider(e);
}
}
public DB getDatabase() {
return mongoDb;
}
private DB extractDatabase(MongoClient mongo, MongoDBConfiguration config) {
String databaseName = config.getDatabaseName();
log.connectingToMongoDatabase(databaseName);
if (!mongo.getDatabaseNames().contains(databaseName)) {
if (config.isCreateDatabase()) {
log.creatingDatabase(databaseName);
} else {
throw log.databaseDoesNotExistException(config
.getDatabaseName());
}
}
return mongo.getDB(databaseName);
}
}
I can extend from MongoDBDataStoreProvider because Fongo in fact follows MongoDB driver but overrides it to store all data locally instead of creating a remote connection.
And then in persistence.xml
Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<!-- Use this for Neo4j -->
<!--
<persistence-unit name="hikePu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<properties>
<property name="hibernate.ogm.datastore.provider" value="neo4j_embedded" />
<property name="hibernate.ogm.datastore.database" value="HikeDB" />
<property name="hibernate.ogm.neo4j.database_path" value="target/test_data_dir" />
</properties>
</persistence-unit>
-->
<!-- Use this for MongoDB -->
<persistence-unit name="hikePu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<properties>
<property name="hibernate.ogm.datastore.provider" value="org.hibernate.ogm.demos.ogm101.part1.provider.FongoDBDatastoreProvider" />
<property name="hibernate.ogm.datastore.database" value="HikeDB" />
<property name="hibernate.ogm.datastore.create_database" value="true" />
</properties>
</persistence-unit>
</persistence>
And with these changes I have been able to run Hibernate-OGM Hiker example test using Fongo instead of a remote(although it is a localhost) MongoDB.
My next step is to provide an integration to NoSQLUnit, so you can write applications and test them in NoSQLUnit.