在文档数据库中继承? [英] inheritance in document database?

查看:105
本文介绍了在文档数据库中继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,因为我搜索的pdfxxx确定指南和开始xxx为单词继承,但我没有找到什么?我错过了什么?因为我用hibernate和mysql做了一个tablePerHierarchy继承,在xxx中是否会因为某种原因而弃用?



(用你不喜欢的数据库替换xxx )

解决方案

我知道这个回答有点晚了,但对于MongoDB,你可能看起来有些不同。 / p>

Mongo是无模式的,因此tablePerHierarchy的概念不一定有用。



p>

  class A 
属性X
属性Y
属性Z

类B继承自A
属性W

在RDMS中,你可能会有这样的

 表A:列X,Y,Z 
表B:列X,Y,Z,W

但MongoDB没有模式。所以你不需要这样结构数据。相反,你将有一个集合包含所有类型A或B(或C ...)的对象(或文档)。



是这样的一系列对象:

  {_ id:1,X:1,Y :2,Z:3} 
{_id:2,X:5,Y:6,Z:7,W:6}

你会注意到我在类型B的对象的旁边存储类型A的对象。MongoDB使这很容易。



但是,如果你有数据对象或实体

  {_ id:1,type: A,X:1,Y:2,Z:3} 
{_id:2,type:B,X:5,Y :6,Z:7,W:6}

编写用于加载对象的工厂类。


i am wondering because i searched the pdf "xxx the definitive guide" and "beginning xxx" for the word "inheritance" but i didn't find anything? am i missing something? because i am doing a tablePerHierarchy inheritance with hibernate and mysql, does that become deprecated for some reason in xxx?

(replace xxx with the "not only sql" database you like)

解决方案

I know this answer is a little late, but for MongoDB, you're probably looking at something slightly different.

Mongo is schemaless, so the concept of "tablePerHierarchy" is not necessarily useful.

Assume the following

class A
  property X
  property Y
  property Z

class B inherits from A
  property W

In an RDMS you would probably have something like this

table A: columns X, Y, Z
table B: columns X, Y, Z, W

But MongoDB does not have a schema. So you do not need to structure data in this way. Instead, you would have a "collection" containing all objects (or "documents") of type A or B (or C...).

So your collection would be a series of objects like this:

{"_id":"1", "X":1, "Y":2, "Z":3}
{"_id":"2", "X":5, "Y":6, "Z":7, "W":6}

You'll notice that I'm storing objects of type A right beside objects of type B. MongoDB makes this very easy. Just pull up a Document from the Collection and it "magically" has all of the appropriate fields/properties.

However, if you have "data objects" or "entities", you can make your life easier by adding a type.

{"_id":"1", "type":"A", "X":1, "Y":2, "Z":3}
{"_id":"2", "type":"B", "X":5, "Y":6, "Z":7, "W":6}

This makes it easier to write a factory class for loading your objects.

这篇关于在文档数据库中继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆