数据库表和面向对象类之间是否存在关系? [英] Is there a relationship between Database Tables and Object Oriented Classes?

查看:146
本文介绍了数据库表和面向对象类之间是否存在关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我的程序,我认识到类和表之间的这种关系,或者我想象它。

Every time I program I recognize this relationship between classes and tables, or am I imagining it.

你可以有一个类每个数据库表或每个类的表即:

You can have a class per database table or a table per class i.e. :

    tables: customer, products, order.

    classes: customer, products, order, may have methods such as addRecord, deleteRecord, updateRecord.

这是什么叫做?对象关系?我不是DBA。

what is this called? Object-Relational? I am not a DBA.

推荐答案

这取决于你使用的数据库类型。如果您使用面向对象数据库(OODB),则没有关系,因为对象和持久化数据是相同的东西。例如,如果您有一个 Customer 类,并将其保存在OODB中,则客户实例

It all depends on the type of database you're using. If you're using an object oriented database (OODB), then there is no relationship, as the objects and the persisted data are the same thing. For example, if you have a Customer class, and you save it in an OODB, then that instance of the customer is what is stored in the DB.

如果您使用的是关系数据库,那么类实例和它们在数据库中的持久表示可以是相同的,但很多时候它们不是。这是因为大多数人使用规范化以有效的方式表示其数据(在关系数据库中) 。这意味着,而不是每个类有一个表,你可以有一个类由多个表表示。在 Customer 示例中,表现在可以是 Customer (包含名称,出生日期和其他属性)和 Order (顺序指向另一个表中的产品)。原因与基数以及客户的能力有关拥有多个订单。当您的业务逻辑需要来自DB的此信息时,数据访问层的任务是映射数据(称为 ORM )从DB到你的类。

If you are using a relational database, then the class instances, and the persisted representation of them in the DB, can be the same thing, but many times they aren't. This is because most folks use normalization to represent their data in an efficient way (in a relational DB). This means, instead of having a table per class, you can have a class represented by more than one table. In the Customer example, the tables might now be Customer (with Name, date of birth, and other properties), and Order (with order pointing to products in yet another table). The reason for this has to do with cardinality, and the ability for Customers to have more than one order. When your business logic needs this information from the DB, the data access layer's job is to map the data (called ORM) from the DB into your classes.

如果你使用另一种类型的DB,那么类之间会有一个不同的关系模型)以及数据库中保存的内容。

If you are using yet another type of DB, then there will be a different relationship between the classes (domain model) and what's persisted in the DB.

但是,只要有这个关系的名称?不,没有名字。

But, as far as having a name for this relationship? No, there is no name.

这篇关于数据库表和面向对象类之间是否存在关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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