问题描述
我认为该是时候看看OO数据库了,并决定在我的下一个小项目-一个小图书馆中使用db4o.
I thought it was about time to have a look at OO databases and decided to use db4o for my next little project - a small library.
请考虑以下对象:书籍,类别.
Consider the following objects: Book, Category.
一本书可以归为0-n个类别,而一个类别可以应用于0-m个书籍.
A Book can be in 0-n categories and a Category can be applied to 0-m Books.
我的第一个想法是要拥有一个类似BookCatecory的连接对象,但是经过一番谷歌搜索之后,我发现这不适用于"Real OO".
My first thought is to have a joining object such as BookCatecory but after a bit of Googling I see that this is not appropriate for 'Real OO'.
所以另一种方法(很多人推荐)是在两个对象中都有一个列表:Book.categories和Category.books.一侧处理关系:Book.addCategory将Category添加到Book.categories,并将Book添加到Category.books.在一个方法调用中更改2个对象时,如何处理提交和回滚?
So another approach (recommended by many) is to have a list in both objects: Book.categories and Category.books. One side handles the relationship: Book.addCategory adds Category to Book.categories and Book to Category.books. How to handle commits and rollbacks when 2 objects are been altered within one method call?
您有什么想法?第二种方法具有明显的优势,但至少对我而言,第一种感觉"权利(更好地规范).
What are your thoughts? The second approach has obvious advantages but, for me at least, the first 'feels' right (better normed).
推荐答案
如果使用对象数据库,则无需关心关系在数据库中的存储方式.您定义类和它们之间的关系.请阅读针对您的数据库的参考指南.关系示例:
If you use object database you don't need to care how relations are stored in database. You define classes and relationships between them. Please read the reference guided to your database. Examples of relationships:
n:n attribute, referencing from the parent
------------------------------------------------------------------
class Person {
List addresses;
}
class Address {
}
n:n attribute, referencing from the child
------------------------------------------------------------------
class Person {
}
class Address {
List persons
}
n:n attribute, bidirectional references
------------------------------------------------------------------
class Person {
List addresses;
}
class Address {
List persons
}
这篇关于如何在对象数据库中设计多对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!