英文:
External ID Concept?
问题
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String externalID; //<--- why we need this?
}
有人建议我在类中包含一个类似上面这样的外部ID字段?有什么建议,为什么可能需要这样做?
英文:
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String externalID; //<--- why we need this?
}
Someone has suggested me to include an external Id field in a class something like that? Any suggestions why that could be?
答案1
得分: 2
不确定在这里“externalID”究竟是什么意思,因为使用情况不明确。但是,我假设有一些情况:
1. 外部服务
外部 ID 可能用于将您的实体与不同服务中的另一个资源的某个 ID 进行映射。某些可以在另一个系统中标识此实体的东西。
例如:在“externalID”中可能存储个人的 Twitter ID 或银行账户 ID。
2. 安全方面
externalID
被用于保护(封装)内部 id
,以防止其在外部可见,因为这可能会引发一些安全漏洞。
例如:在您的情况下,内部 id
是带有 GenerationType.AUTO
的整数,这意味着所有实体都将具有增量 id:1、2、3、……
有人可能会分析您的 API 调用,并通过 API 轻松迭代所有账户,例如:GET api/person/{id}
。
通常会使用不同类型的 ID 来解决这个问题,比如 UUID,例如:8b9af550-a4c7-4181-b6ba-1a1899109783
。在您的情况下,可以将其用作 externalID
。
因此,我假设这是为您的实体添加额外的 externalID
的原因。
注意:如果您的数据库支持使用 UUID(或将其存储为字符串),您可以简单地将内部 id 类型替换为 UUID
,并且不再需要在此处使用 externalID
。
英文:
Not sure, what exactly meant by externalID
here, since the case of usage is not clear.
But, I assume a couple of cases:
1. External service
External id may be used to map your entity with some id of another resource from different services. Something, that identifies this entity in another system.
For example: in externalID
may be stored person twitter id or bank account id.
2. Security-wise
externalID
is used to protect (encapsulate) internal id
been visible outside, which may cause some security vulnerabilities.
For example:
In your case, internal id
is Integer with GenerationType.AUTO
, that means, all entities will have an incremental id: 1, 2, 3, ...
Knowing that someone may analyze your API calls and easily iterate through all your accounts via API, e.g: GET api/person/{id}
.
Usually, a different type of IDs is used to solve this problem, like UUID, e.g.: 8b9af550-a4c7-4181-b6ba-1a1899109783
. Which can be used as externalID
in your case.
So, I assume this is the reason to add additional externalID
to your entity.
Note: if your Database supports the usage of UUID (or store it as String), you can simply replace your internal id type with UUID
and get rid of externalID
here.
答案2
得分: 0
有可能 externalID 代表了与某个人相关联的另一张表的主键。虽然字符串相当任意,但通常会使用整数、长整数或 UUID 来表示主键。可能需要在问题中提供更多的背景信息。
英文:
It is possible that externalID represents the Primary Key of another table that person is relative to. String is quite arbitrary though, you would generally use an Integer, Long, or UUID to represent a primary key. Might need more context in the question.
答案3
得分: 0
外部ID的目的是将您的实体与来自与您的系统解耦的另一种表示形式相链接。
例如,如果您想为单点登录(SSO)的原因存储Facebook ID,您可以通过一个名为externalId
或类似的字段来实现。另一个例子可能是您从另一个数据库导入了一些帐户,并且您想要存储已导入的源实体的主键。
否则,如果该字段在您的业务逻辑中不代表任何内容,请将其删除。
英文:
The purpose behind an external ID is to link your entity with another representation of it from a system that is decoupled from yours.
For example, if you want the store the Facebook ID for SSO reasons, you would do it through a field that could be called externalId
, or something like that. Another example might be that you imported some accounts from another database, and you want to store the Primary Key from the source entity that has been imported.
Otherwise, if that field does not represent anything in your business logic, get rid of it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论