Skip to content

Commit

Permalink
OGM-725 Adds section documenting Fongo usage
Browse files Browse the repository at this point in the history
  • Loading branch information
lordofthejars authored and gunnarmorling committed Feb 26, 2015
1 parent 762f7b3 commit 46ac8d5
Showing 1 changed file with 67 additions and 20 deletions.
Expand Up @@ -133,6 +133,53 @@ Common properties shared between stores are declared on +OgmProperties+
For maximum portability between stores, use the most generic interface possible.
====

==== FongoDB Provider

Fongo is an in-memory java implementation of MongoDB.
It intercepts calls to the standard mongo-java-driver for finds, updates, inserts, removes and other methods.
The primary use is for lightweight unit testing where you don't want to spin up a +mongod+ process.

Hibernate OGM provides a FongoDB provider so during tests it can be used instead of MongoDB driver.
Note that you don't need to change your business code to adapt to FongoDB because all adaptations are done under the cover by Hibernate OGM.

To start using FongoDB provider, you should do two things:

The first one is register the provider by using +hibernate.ogm.datastore.provider+ and setting to +fongodb+.

[source, XML]
[subs="verbatim,attributes"]
----
<persistence-unit name="ogm-jpa-tutorial" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<properties>
<property name="hibernate.ogm.datastore.provider" value="fongodb"/>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
</properties>
</persistence-unit>
----

The second one is adding FongoDB and SLF4J dependencies in your project.

[source, XML]
[subs="verbatim,attributes"]
----
<dependency>
<groupId>com.github.fakemongo</groupId>
<artifactId>fongo</artifactId>
<scope>test</scope>
<version>${fongoDBVersion}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4jVersion}</version>
<scope>test</scope>
</dependency>
----

You can read more about FongoDB project and its limitations at https://github.com/fakemongo/fongo

[[ogm-mongodb-annotation-configuration]]
==== Annotation based configuration

Expand Down Expand Up @@ -502,7 +549,7 @@ public class Bookmark {
[source, JSON]
----
{
"_id" : "bookmark_1"
"_id" : "bookmark_1"
"title" : "Hibernate OGM documentation"
}
----
Expand Down Expand Up @@ -611,7 +658,7 @@ The preferable strategy, Hibernate OGM will create the identifier upon insertion
To apply this strategy the id must be one of the following:

* annotated with +@Type(type="objectid")+
* [classname]+org.bson.types.ObjectId+
* [classname]+org.bson.types.ObjectId+

like in the following examples:

Expand Down Expand Up @@ -703,7 +750,7 @@ hibernate_sequences collection
[source, JSON]
----
{
{
"_id" : "GuitarPlayer",
"next_val" : 101
}
Expand Down Expand Up @@ -787,7 +834,7 @@ hibernate_sequences collection
[source, JSON]
----
{ "_id" : "song_sequence_name", "next_val" : 21 }
{ "_id" : "song_sequence_name", "next_val" : 21 }
----
====

Expand Down Expand Up @@ -829,7 +876,7 @@ hibernate_sequences collection
[source, JSON]
----
{ "_id" : "song_seq", "next_val" : 42 }
{ "_id" : "song_seq", "next_val" : 42 }
----
====

Expand Down Expand Up @@ -863,7 +910,7 @@ If the property +hibernate.id.new_generator_mappings+ is set to +true+,
----
@Entity
public class DistributedRevisionControl {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
Expand Down Expand Up @@ -895,7 +942,7 @@ hibernate_sequences collection
----
@Entity
public class Comedian {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private ObjectId id;
Expand Down Expand Up @@ -1502,7 +1549,7 @@ Member collection as JSON in MongoDB
----
====

.Bidirectional many-to-one
.Bidirectional many-to-one
====
[source, JAVA]
----
Expand Down Expand Up @@ -1821,7 +1868,7 @@ User collection as JSON in MongoDB
{
"_id" : "user_001",
"addresses" : [
{
{
"addresses_KEY" : "work",
"addresses_id" : "address_001"
},
Expand All @@ -1837,8 +1884,8 @@ Address collection as JSON in MongoDB
[source, JSON]
----
{ "_id" : "address_001", "city" : "Rome" }
{ "_id" : "address_002", "city" : "Paris" }
{ "_id" : "address_001", "city" : "Rome" }
{ "_id" : "address_002", "city" : "Paris" }
----
====

Expand Down Expand Up @@ -1879,7 +1926,7 @@ User collection as JSON in MongoDB
{
"_id" : "user_001",
"addresses" : [
{
{
"addressType" : "work",
"addresses_id" : "address_001"
},
Expand All @@ -1895,8 +1942,8 @@ Address collection as JSON in MongoDB
[source, JSON]
----
{ "_id" : "address_001", "city" : "Rome" }
{ "_id" : "address_002", "city" : "Paris" }
{ "_id" : "address_001", "city" : "Rome" }
{ "_id" : "address_002", "city" : "Paris" }
----
====

Expand Down Expand Up @@ -1968,7 +2015,7 @@ ClassRoom collection
----
====

.Bidirectional many-to-many
.Bidirectional many-to-many
====
[source, JAVA]
----
Expand Down Expand Up @@ -2117,7 +2164,7 @@ For example, if the [classname]+BankAccount+ and [classname]+Owner+ are related,
the collection used to store will be named +associations_Owner_BankAccount+. You can rename
The prefix is useful to quickly identify the association collections from the entity collections.
You can also decide to rename the collection representing the association using +@JoinTable+
(see <<mongodb-one-collection-strategy-join-table, an example>>)
(see <<mongodb-one-collection-strategy-join-table, an example>>)

Each document of an association collection has the following structure:

Expand Down Expand Up @@ -2438,15 +2485,15 @@ BankAccount collection
}
----
associations_AccountOwner_BankAccount collection
associations_AccountOwner_BankAccount collection
[source, JSON]
----
{
"_id" : {
"bankAccounts_id" : "account_1"
},
"rows" : [ "owner_1" ]
"rows" : [ "owner_1" ]
}
{
"_id" : {
Expand Down Expand Up @@ -2524,7 +2571,7 @@ OwnerBankAccount
"_id" : {
"bankAccounts_id" : "account_1"
},
"rows" : [ "owner_1" ]
"rows" : [ "owner_1" ]
}
{
"_id" : {
Expand All @@ -2550,7 +2597,7 @@ that the current instance is related to.
====
This strategy won't affect *-to-one associations or embedded collections.
Generally, you should not make use of this strategy
Generally, you should not make use of this strategy
unless embedding the association information proves to be too big for your document
and you wish to separate them.
====
Expand Down

0 comments on commit 46ac8d5

Please sign in to comment.