DataNucleus supports persisting/retrieving objects to/from MongoDB datastores (using the datanucleus-mongodb plugin) which utilises the Mongo Java driver. Simply specify your "connectionURL" as follows datanucleus.ConnectionURL=mongodb:[{server}][/{dbName}] [,{server2}[,server3}]]For example, to connect to a local server, with database called "myMongoDB" datanucleus.ConnectionURL=mongodb:/myMongoDB If you just specify the URL as mongodb then you have a local MongoDB datastore called "DataNucleus", otherwise it tries to connect to the datastore {dbName} at {server} . The multiple {server} option allows you to run against MongoDB replica sets. You then create your PMF/EMF as normal and use JDO/JPA as normal. There are tutorials available for use of DataNucleus with MongoDB for JDO and for JPA Things to bear in mind with MongoDB usage :-
When you have a field in a class that is of a persistable type you sometimes want to store it with the owning object. In this case you can use JDO/ JPA embedding of the field. DataNucleus offers two ways of performing this embedding
@PersistenceCapable
public class A
{
@Embedded
B b;
...
}This example uses the default embedding, using a nested document within the owner document, and could look something like this
{ "name" : "A Name" ,
"id" : 1 ,
"b" : { "name" : "B name" ,
"description" : "the description"}
}The alternative would be
@PersistenceCapable
public class A
{
@Embedded
@Extension(vendorName="datanucleus", key="nested", value="false")
B b;
...
}and this will use flat embedding , looking something like this
{ "name" : "A Name" ,
"id" : 1 ,
"b_name" : "B name" ,
"b_description" : "the description"
}
When you have a field in a class that is of a Collection type you sometimes want to store it with the owning object. In this case you can use JDO/ JPA embedding of the field. So if we have
@PersistenceCapable
public class A
{
@Element(embedded="true")
Collection<b> bs;
...
}and would look something like this
{ "name" : "A Name" ,
"id" : 1 ,
"bs" :
[
{ "name" : "B Name 1" ,
"description" : "desc 1"} ,
{ "name" : "B Name 2" ,
"description" : "desc 2"} ,
{ "name" : "B Name 3" ,
"description" : "desc 3"}
]
} |