How to map collections to mongodb using datanucleus/jdo?
I'm trying to map an existing mongodb database to domain objects using datanuclues (3.0.0-m6) / JDO.
The db has documents like:
collection A: {_id: ..., field1: ...}
collection B: {_id: ..., a: ..., field3: ...}
That should be mapped to:
class A {
// id ....
String field1;
List<B> bs;
}
class B {
A a;
Double field3;
}
This looks like a 1-N Bidirectional relation using Foreign-Key as described in the datanuclues documentation.
The package.jdo:
<package name="my.domain">
<class name="A" table=开发者_运维技巧"A" identity-type="application">
<field name="id" primary-key="true"/>
<field name="bs" persistence-modifier="persistent" mapped-by="a">
<collection element-type="my.domain.B" />
<element column="a"/>
</field>
</class>
<class name="B" table="B" identity-type="datastore" >
<datastore-identity strategy="identity" />
<field name="a" persistence-modifier="persistent">
<column name="a"/>
</field>
</class>
</package>
This works as expected when using a relational DB (e.g. Derby). However, when using mongodb datanucleus uses a collection in the A document containing the ids of the linked Bs:
collection A: {_id: ..., field1: ..., bs: [...]}
which is not compatible with the existing schema.
How to I configure this kind of relation for mongodb? Datanucleus distinguishes Set and List for relations (I tested both), a Set might be acceptable if this would make the mapping easier.
Current DataNucleus 1-N behaviour is all that we support ... i.e put the child ids in the parent. The doc you refer to is "ORM", and MongoDB is not relational (so doesn't have "foreign keys" as such anyway). Obviously you could raise a JIRA to add support for that mode, and also attach a patch to provide it.
精彩评论