I am just learning how to implement the Nested Set Model but still have confusion with a certain aspect of it involving items that may be part of multiple categories. Given the example below that was pulled from HERE and mirrors many other examples I have come across...
我只是学习如何实现嵌套集模型,但仍然混淆了它涉及可能属于多个类别的项目的某个方面。鉴于下面的示例是从HERE中提取的,并且反映了我遇到的许多其他示例......
How do you avoid duplication in the DB when you add Apples since they are multi-colored (i.e. Red, Yellow, Green)?
添加苹果时,如何避免数据库中的重复,因为它们是多色的(即红色,黄色,绿色)?
4 个解决方案
#1
5
You do not avoid duplications and the apple (or a reference to the apple) will be placed twice in your tree otherwise it won't be a tree but rather a graph. Your question is equally applicable if you build a... Swing JTree or an HTML tree ;).
你不会避免重复,苹果(或对苹果的引用)将在你的树中放置两次,否则它将不是一棵树而是一张图。如果你构建一个... Swing JTree或HTML树,你的问题同样适用;)。
The nested set model is just an efficient way to push and traverse a tree structure in a relational DB.It is not a data structure itself. It's more popular among MySQL users since MySQL lacks functionality for processing tree structures (e.g. like the one that Oracle provides).
嵌套集模型只是在关系数据库中推送和遍历树结构的有效方式。它本身不是数据结构。它在MySQL用户中更受欢迎,因为MySQL缺乏处理树结构的功能(例如Oracle提供的功能)。
Cheers!
干杯!
#2
3
Nested set model is a structure for 1:N (one-to-many) relationships, you want to use M:N (many to many) relationship (many items can have apple as parent, but can have more than one parent).
嵌套集模型是1:N(一对多)关系的结构,您希望使用M:N(多对多)关系(许多项可以将apple作为父项,但可以有多个父项)。
看到这篇文章
*
But you should be aware, that hierarchical M:N relationships can get quite complex really fast!
但是你应该知道,分层的M:N关系可以非常快速地变得非常复杂!
#3
3
Thinking out loud here, but perhaps it would be helpful to view some attributes (like Red, Yellow and Green) as 'tags' instead of 'categories' and handle them with separate logic. That would let you keep the Nested Set model and avoid unnecessary duplication. Plus, it would allow you to keep your categories simpler.
在这里大声思考,但也许将一些属性(如红色,黄色和绿色)视为“标签”而不是“类别”并使用单独的逻辑处理它们会很有帮助。这样可以保留嵌套集模型并避免不必要的重复。此外,它还可以让您的类别更简单。
It's all in how you think about the information. Categories are just another way of representing attributes. I understand your example was just for illustrative purposes, but if you're going to categorize fruit by color, why would you not also categorize meat the same way, i.e., white meat and red meat? Most likely you would not. So my point is it's probably not necessary to categorize fruit by color, either.
这就是你如何看待这些信息。类别只是表示属性的另一种方式。我知道你的例子只是为了说明目的,但是如果你要按颜色对水果进行分类,你为什么不以同样的方式对肉类进行分类,即白肉和红肉?很可能你不会。所以我的观点是,可能没有必要按颜色对水果进行分类。
Instead, some attributes are better represented in other ways. In fact, in its simplest form, it could be recorded as a column in the 'food' table labeled 'color'. Or, if it's a very common attribute and you find yourself duplicating the value significantly, it could be split off to a separate table named 'color' and mapped to each food item from a third table. Of course, the more abstract approach would be to generalize the table as 'tags' and include each color as an individual tag that can then be mapped to any food item. Then you can map any number of tags (colors) to any number of food items, giving you a true many-to-many relationship and freeing up your category designations to be more generalized as well.
相反,某些属性可以通过其他方式更好地表示。实际上,在最简单的形式中,它可以被记录为标记为“颜色”的“食物”表中的列。或者,如果它是一个非常常见的属性,并且您发现自己重复显示该值,则可以将其拆分为名为“color”的单独表格,并将其映射到第三个表格中的每个食物项目。当然,更抽象的方法是将表格概括为“标签”,并将每种颜色包含为单个标签,然后可以将其映射到任何食品。然后,您可以将任意数量的标签(颜色)映射到任意数量的食品,为您提供真正的多对多关系,并释放您的类别名称,以便更加通用化。
I know there's ongoing debate about whether tags are categories or categories are tags, etc., but this appears to be one instance in which they could be complimentary and create a more abstract and robust system that's easier to manage.
我知道关于标签是类别还是类别是标签等的问题一直在争论,但这似乎是一个可以互补的例子,并且创建了一个更易于管理的更抽象,更健壮的系统。
#4
0
Old thread, but I found a better answer to this problem.
老线程,但我找到了一个更好的答案来解决这个问题。
Since apple can have different color, your structure is a graph,not a tree. The nested set model is not the right structure for that.
由于苹果可以有不同的颜色,因此您的结构是图形,而不是树。嵌套集模型不是正确的结构。
Since you mention in a comment that you're using Mysql, a better solution is to use the Open Query Graph engine (http://openquery.com/graph/doc) which is a mysql plugin that lets you create a special table where you put the relationships, basically parentId and childId. The magic is that you query this table with a special column latch depending of the value passed in the query will tell the OQGRAPH engine which command to execute. See the docs for details.
既然您在评论中提到您正在使用Mysql,更好的解决方案是使用Open Query Graph引擎(http://openquery.com/graph/doc)这是一个mysql插件,可以让您创建一个特殊的表你把关系,基本上是parentId和childId。神奇的是,您使用特殊的列锁存器查询此表,具体取决于查询中传递的值将告诉OQGRAPH引擎执行哪个命令。有关详细信息,请参阅文档。
#1
5
You do not avoid duplications and the apple (or a reference to the apple) will be placed twice in your tree otherwise it won't be a tree but rather a graph. Your question is equally applicable if you build a... Swing JTree or an HTML tree ;).
你不会避免重复,苹果(或对苹果的引用)将在你的树中放置两次,否则它将不是一棵树而是一张图。如果你构建一个... Swing JTree或HTML树,你的问题同样适用;)。
The nested set model is just an efficient way to push and traverse a tree structure in a relational DB.It is not a data structure itself. It's more popular among MySQL users since MySQL lacks functionality for processing tree structures (e.g. like the one that Oracle provides).
嵌套集模型只是在关系数据库中推送和遍历树结构的有效方式。它本身不是数据结构。它在MySQL用户中更受欢迎,因为MySQL缺乏处理树结构的功能(例如Oracle提供的功能)。
Cheers!
干杯!
#2
3
Nested set model is a structure for 1:N (one-to-many) relationships, you want to use M:N (many to many) relationship (many items can have apple as parent, but can have more than one parent).
嵌套集模型是1:N(一对多)关系的结构,您希望使用M:N(多对多)关系(许多项可以将apple作为父项,但可以有多个父项)。
看到这篇文章
*
But you should be aware, that hierarchical M:N relationships can get quite complex really fast!
但是你应该知道,分层的M:N关系可以非常快速地变得非常复杂!
#3
3
Thinking out loud here, but perhaps it would be helpful to view some attributes (like Red, Yellow and Green) as 'tags' instead of 'categories' and handle them with separate logic. That would let you keep the Nested Set model and avoid unnecessary duplication. Plus, it would allow you to keep your categories simpler.
在这里大声思考,但也许将一些属性(如红色,黄色和绿色)视为“标签”而不是“类别”并使用单独的逻辑处理它们会很有帮助。这样可以保留嵌套集模型并避免不必要的重复。此外,它还可以让您的类别更简单。
It's all in how you think about the information. Categories are just another way of representing attributes. I understand your example was just for illustrative purposes, but if you're going to categorize fruit by color, why would you not also categorize meat the same way, i.e., white meat and red meat? Most likely you would not. So my point is it's probably not necessary to categorize fruit by color, either.
这就是你如何看待这些信息。类别只是表示属性的另一种方式。我知道你的例子只是为了说明目的,但是如果你要按颜色对水果进行分类,你为什么不以同样的方式对肉类进行分类,即白肉和红肉?很可能你不会。所以我的观点是,可能没有必要按颜色对水果进行分类。
Instead, some attributes are better represented in other ways. In fact, in its simplest form, it could be recorded as a column in the 'food' table labeled 'color'. Or, if it's a very common attribute and you find yourself duplicating the value significantly, it could be split off to a separate table named 'color' and mapped to each food item from a third table. Of course, the more abstract approach would be to generalize the table as 'tags' and include each color as an individual tag that can then be mapped to any food item. Then you can map any number of tags (colors) to any number of food items, giving you a true many-to-many relationship and freeing up your category designations to be more generalized as well.
相反,某些属性可以通过其他方式更好地表示。实际上,在最简单的形式中,它可以被记录为标记为“颜色”的“食物”表中的列。或者,如果它是一个非常常见的属性,并且您发现自己重复显示该值,则可以将其拆分为名为“color”的单独表格,并将其映射到第三个表格中的每个食物项目。当然,更抽象的方法是将表格概括为“标签”,并将每种颜色包含为单个标签,然后可以将其映射到任何食品。然后,您可以将任意数量的标签(颜色)映射到任意数量的食品,为您提供真正的多对多关系,并释放您的类别名称,以便更加通用化。
I know there's ongoing debate about whether tags are categories or categories are tags, etc., but this appears to be one instance in which they could be complimentary and create a more abstract and robust system that's easier to manage.
我知道关于标签是类别还是类别是标签等的问题一直在争论,但这似乎是一个可以互补的例子,并且创建了一个更易于管理的更抽象,更健壮的系统。
#4
0
Old thread, but I found a better answer to this problem.
老线程,但我找到了一个更好的答案来解决这个问题。
Since apple can have different color, your structure is a graph,not a tree. The nested set model is not the right structure for that.
由于苹果可以有不同的颜色,因此您的结构是图形,而不是树。嵌套集模型不是正确的结构。
Since you mention in a comment that you're using Mysql, a better solution is to use the Open Query Graph engine (http://openquery.com/graph/doc) which is a mysql plugin that lets you create a special table where you put the relationships, basically parentId and childId. The magic is that you query this table with a special column latch depending of the value passed in the query will tell the OQGRAPH engine which command to execute. See the docs for details.
既然您在评论中提到您正在使用Mysql,更好的解决方案是使用Open Query Graph引擎(http://openquery.com/graph/doc)这是一个mysql插件,可以让您创建一个特殊的表你把关系,基本上是parentId和childId。神奇的是,您使用特殊的列锁存器查询此表,具体取决于查询中传递的值将告诉OQGRAPH引擎执行哪个命令。有关详细信息,请参阅文档。