如何使用 datanucleus/jdo 将集合映射到 mongodb?
我正在尝试使用 datanuclues (3.0.0-m6) / JDO 将现有的 mongodb 数据库映射到域对象。
数据库具有如下文档:
collection A: {_id: ..., field1: ...}
collection B: {_id: ..., a: ..., field3: ...}
应该映射到:
class A {
// id ....
String field1;
List<B> bs;
}
class B {
A a;
Double field3;
}
这看起来像 使用外键的 1-N 双向关系,如 datanuclues 文档中所述。
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>
当使用关系数据库(例如Derby)时,它会按预期工作。但是,当使用 mongodb datanucleus 时,A 文档中使用包含链接 B 的 id 的集合:
collection A: {_id: ..., field1: ..., bs: [...]}
这与现有架构不兼容。
如何为 mongodb 配置这种关系? Datanucleus 区分关系的 Set 和 List(我测试了两者),如果 Set 可以使映射更容易,则 Set 可能是可以接受的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当前的 DataNucleus 1-N 行为是我们支持的全部...即将子 id 放入父中。您引用的文档是“ORM”,而 MongoDB 不是关系型的(因此无论如何也没有“外键”)。显然,您可以提出 JIRA 来添加对该模式的支持,并附加一个补丁来提供它。
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.