Hibernate设计困境;带有地址链接的父子关系
我有一个现有的实体设计。有一个Individual
实体。 监护人
或未成年人
存储为个人
。如果是Minor
,则您分配了零个或多个监护人。这种关系存储在不同的实体中。
还有一个额外的要求;添加一个标志,以显示如果设置了关系,则应将 Guardian
Address
用于 Minor
。人们可以稍后清除该标志。如果该标志被清除,则将使用现有地址,并且监护人地址的任何更改都不会覆盖未成年人的地址。
Minor
和 Guardian
之间的关系存储为
(minor_individual_id, Guardian_individual_id, 关系类型)
其中 type_of_the_relationship 可以具有“合法”等值。
是否与向关系表添加新标志一样简单?添加后,未成年人的地址需要更改为监护人的地址。地址信息存储在另一个实体中(Address
)。这可以通过 Hibernate 内置功能来完成吗?或者我们应该添加一个层来处理此功能? Hibernate 中是否有任何内容可以让我知道标志已更新并且需要进行一组新的更新?
地址实体在单个表中包含个人 ID 和个人地址。
一旦设置了该标志,每个未成年人只能设置一次,因为如果未成年人有多个监护人,则为每个监护人打开地址级联会导致混乱。 谢谢。
数据库结构:
个人(表/实体)
身份证号
年龄(根据年龄,您被视为未成年人)
GuardianRelationShip(表/实体)
未成年人_个人_ID
Guardian_Individual_Id
地址(表/实体)
个人 ID
地址行1
城市
状态
如果 ura Minor
的 Individual
id 为 2,并且有一个 Guardian
与 Individual
id of 1。这看起来像
Id Age (Individual)
1 40 Guardian
2 5 Minor
Minor Guardian (GuardianRelationShip)
2 1
IndividualId City State (Address)
1 LA CA
2 NY NY
一旦这些被链接起来,想法是次要的“individual id of 2”的地址将更改为 NY/NY。
如果有要求。上面说使用未成年人的监护人地址并允许取消链接地址,您会怎么做?
I have an existing entity design. There is an Individual
entity. A Guardian
or a Minor
are stored as Individual
. If u r a Minor
, u r assigned zero or more guardians. This relationship is stored in a different entity.
There is an additional requirement; to add a flag to show that Guardian
Address
should be used for the Minor
if the relationship is set. One can clear that flag at a later time. If the flag is cleared, the existing address will be used and no changes to guardian's address will overwrite the minor's address.
The relationship between Minor
and Guardian
is stored as
(minor_individual_id, guardian_individual_id,
type_of_the_relationship)where type_of_the_relationship can have values such "legal" etc..
Is it as simple as adding a new flag to the relationship table ? Once added, the minor's address needs to be changed to the guardian's address. The address information is stored in another entity (Address
). Can this be done via Hibernate built-in functionality ? Or should we add a layer to take care of this functionality ? Is there anything in Hibernate that lets me know the flag has been updated and a new set of updates need to happen ?
The address entity contains the individual id and the address for the individual in a single table.
Once the flag is set, it can only be set once per minor since it will cause confusion to have minor having multiple guardians to have address cascade to be turned on for each of its guardians.
Thanks.
Database Structure:
Individual ( Table/Entity)
Id
Age ( Depending on Age, you are treated as minor )
GuardianRelationShip ( Table/Entity)
Minor_Individual_Id
Guardian_Individual_Id
Address (Table/Entity)
Individual_Id
AddressLine1
City
State
If u r a Minor
with Individual
id of 2 and a have a Guardian
with Individual
id of 1. This will look like
Id Age (Individual)
1 40 Guardian
2 5 Minor
Minor Guardian (GuardianRelationShip)
2 1
IndividualId City State (Address)
1 LA CA
2 NY NY
Once those are linked, the idea is that the address for the minor "individual id of 2" will be changed to NY/NY.
If there is a req. that says use guardian address for minor and allow for unlinking address, how would you do that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有地址实体。我将创建IndividualEntity.address,未成年人的地址可以引用与监护人之一相同的AddressEntity(顺便说一句,几个监护人也可以住在同一个地址!)。之后,您可以检查未成年人的地址是否与其中一位监护人的地址相同。
我不明白该标志解决了哪个问题。请提供更多信息以及一些代码来说明您的实体和关系。
You have the AddressEntity. I would create IndividualEntity.address and minor's address could reference the same AddressEntity as one of guardians (BTW, several guardians could live at the same address too!). Afterwards, you could check if minor's address equals to one of guardian's address.
I don't understand which problem the flag solves. Please provide more info, along with some code to illustrate your entities and relations.
允许没有相应
地址
条目的Individual
条目。如果您希望将其限制为仅监护人,则仅当Individual
表的条目在GuardianRelationship.Minor
列中没有相应的匹配项时才允许这种情况。如果地址为空,则查找监护人并使用该地址。如果您确实执行上述规则,请确保考虑改变结构的边缘情况,例如个人不再拥有列出的监护人并且没有地址。
Allow an
Individual
entry without a correspondingAddress
entry. If you want to limit it to just guardians, only permit that case when the entry for theIndividual
table doesn't have a corresponding match in theGuardianRelationship.Minor
column. If address is null, lookup guardian and use that address.If you do enforce that rule above, make sure you consider edge cases that change the structure such as an individual no longer having a listed guardian and having no address.