简体   繁体   English

如何坚持一对一的关系? (没有框架)

[英]How to persist a one-to-one relationship ? (no framework)

Right now I have a DAO for each entity. 现在我为每个实体都有一个DAO。

When I save a certain entity, say Employee, it needs to be linked to corresponding Node entity to show up in a tree. 当我保存某个实体时,比如说Employee,它需要链接到相应的Node实体以显示在树中。

How can this be done while keeping the code as simple as possible ? 如何在保持代码尽可能简单的同时完成这项工作?

Edit: Here's some added detail. 编辑:这是一些额外的细节。 In OO, what I want to achieve is to be able to have a Node refer to an object with the possibility that it could be found under another Node. 在OO中,我想要实现的是能够让Node引用一个对象,并且有可能在另一个Node下找到它。 This way only a Node is necessary to describe a tree while different properties can be fetched from the contained object. 这样,只需要一个节点来描述树,同时可以从包含的对象中获取不同的属性。 Here's how I see things: 这是我看到的东西:

Node 1-1 NodeObject 1-1 Employee 节点1-1 NodeObject 1-1员工

A node creating a tree does not really imply a one-to-one relationship, but rather a one-to-many relationship. 创建树的节点并不真正意味着一对一的关系,而是一对多的关系。

In those terms, your Employee table can have a column that represents the ID of the node each entity in it belongs to. 在这些术语中,您的Employee表可以有一个列,表示其中每个实体所属的节点的ID。

In your DAO, you should then 在你的DAO中,你应该

  • pass the Employee and Node objects you want connected as parameters to your method, 将要连接的EmployeeNode对象作为参数传递给方法,
  • look up the Node object's ID in the database by finding its corresponding entity, 通过查找相应的实体在数据库中查找Node对象的ID,
  • and create the employee entity using the data from the object and the fetched Node ID. 并使用来自对象的数据和获取的Node ID创建员工实体。

Indeed, you should be more specific in your question, though. 实际上,你应该在你的问题中更具体。

EDIT based on comment below: 根据以下评论编辑:

If your Node table represents the tree, and you want each Node to have a one-to-one relationship with Employee , it depends on which references are important to you. 如果您的Node表代表树,并且您希望每个NodeEmployee具有一对一的关系,那么它取决于哪些引用对您很重要。

  1. You want to be able to reference the employee that belongs to a node (probably). 您希望能够引用属于某个节点的员工(可能)。
  2. You want to find which node the employee belongs to (less likely, but possibly). 您想要查找员工所属的节点(不太可能,但可能)。

If you need only number 1, then you will have the ID of the employee in your Node entity. 如果您只需要编号1,那么您将在Node实体中拥有该员工的ID。 If you need both, then you can also put the ID of the Node in your Employee entity. 如果您需要两者,那么您还可以将Node的ID放在Employee实体中。

The logic of the DAO method described above is still applicable. 上述DAO方法的逻辑仍然适用。

Say you have one to one association between employee and employee details. 假设您在员工和员工详细信息之间存在一对一关联。

Employee(Data Object)------>Employee Table
Employee Detail(Data Object)------>Employee Details Table

Employee Details is having the column employee id which is foreign key to Employee table. Employee Details具有列employee name,它是Employee表的外键。

Now you want to persist Employee. 现在你要坚持员工。 First you will persist Employee object and get its primary key. 首先,您将持久保存Employee对象并获取其主键。 Now populate the property employeeId of Employee Detail with primary key Of Employee table.Now Persist the Employee Detail object. 现在使用Employee table的主键填充Employee Detail的属性employeeId。现在保留Employee Detail对象。

To load the both the entities in single go, use inner join between Employee and Employee Details Table 要在单个go中加载两个实体,请使用Employee和Employee Details Table之间的内部联接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM